aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tests/Common/Mock/TestGroupsDataPlugin.cs
blob: 8e2d8e6c9891bf65aa230cdf82e5447a6393cd6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using OpenMetaverse;
using OpenSim.Data;

namespace OpenSim.Tests.Common.Mock
{
    public class TestGroupsDataPlugin : IGroupsData
    {
        class CompositeKey
        {
            private readonly string _key;
            public string Key
            {
                get { return _key;  }
            }

            public CompositeKey(UUID _k1, string _k2)
            {
                _key = _k1.ToString() + _k2;
            }

            public CompositeKey(UUID _k1, string _k2, string _k3)
            {
                _key = _k1.ToString() + _k2 + _k3;
            }

            public override bool Equals(object obj)
            {
                if (obj is CompositeKey)
                {
                    return Key == ((CompositeKey)obj).Key;
                }
                return false;
            }

            public override int GetHashCode()
            {
                return base.GetHashCode();
            }

            public override string ToString()
            {
                return Key;
            }
        }

        private Dictionary<UUID, GroupData> m_Groups;
        private Dictionary<CompositeKey, MembershipData> m_Membership;
        private Dictionary<CompositeKey, RoleData> m_Roles;
        private Dictionary<CompositeKey, RoleMembershipData> m_RoleMembership;
        private Dictionary<UUID, InvitationData> m_Invites;
        private Dictionary<UUID, NoticeData> m_Notices;
        private Dictionary<string, PrincipalData> m_Principals;

        public TestGroupsDataPlugin(string connectionString, string realm)
        {
            m_Groups = new Dictionary<UUID, GroupData>();
            m_Membership = new Dictionary<CompositeKey, MembershipData>();
            m_Roles = new Dictionary<CompositeKey, RoleData>();
            m_RoleMembership = new Dictionary<CompositeKey, RoleMembershipData>();
            m_Invites = new Dictionary<UUID, InvitationData>();
            m_Notices = new Dictionary<UUID, NoticeData>();
            m_Principals = new Dictionary<string, PrincipalData>();
        }

        #region groups table
        public bool StoreGroup(GroupData data)
        {
            return false;
        }

        public GroupData RetrieveGroup(UUID groupID)
        {
            if (m_Groups.ContainsKey(groupID))
                return m_Groups[groupID];

            return null;
        }

        public GroupData RetrieveGroup(string name)
        {
            return m_Groups.Values.First(g => g.Data.ContainsKey("Name") && g.Data["Name"] == name);
        }

        public GroupData[] RetrieveGroups(string pattern)
        {
            if (string.IsNullOrEmpty(pattern))
                pattern = "1";

            IEnumerable<GroupData> groups = m_Groups.Values.Where(g => g.Data.ContainsKey("Name") && (g.Data["Name"].StartsWith(pattern) || g.Data["Name"].EndsWith(pattern)));

            return (groups != null) ? groups.ToArray() : new GroupData[0];
        }

        public bool DeleteGroup(UUID groupID)
        {
            return m_Groups.Remove(groupID);
        }

        public int GroupsCount()
        {
            return m_Groups.Count;
        }
        #endregion

        #region membership table
        public MembershipData RetrieveMember(UUID groupID, string pricipalID)
        {
            CompositeKey dkey = new CompositeKey(groupID, pricipalID);
            if (m_Membership.ContainsKey(dkey))
                return m_Membership[dkey];

            return null;
        }

        public MembershipData[] RetrieveMembers(UUID groupID)
        {
            IEnumerable<CompositeKey> keys = m_Membership.Keys.Where(k => k.Key.StartsWith(groupID.ToString()));
            return keys.Where(m_Membership.ContainsKey).Select(x => m_Membership[x]).ToArray();
        }

        public MembershipData[] RetrieveMemberships(string principalID)
        {
            IEnumerable<CompositeKey> keys = m_Membership.Keys.Where(k => k.Key.EndsWith(principalID.ToString()));
            return keys.Where(m_Membership.ContainsKey).Select(x => m_Membership[x]).ToArray();
        }

        public MembershipData[] RetrievePrincipalGroupMemberships(string principalID)
        {
            return RetrieveMemberships(principalID);
        }

        public MembershipData RetrievePrincipalGroupMembership(string principalID, UUID groupID)
        {
            CompositeKey dkey = new CompositeKey(groupID, principalID);
            if (m_Membership.ContainsKey(dkey))
                return m_Membership[dkey];
            return null;
        }

        public bool StoreMember(MembershipData data)
        {
            CompositeKey dkey = new CompositeKey(data.GroupID, data.PrincipalID);
            m_Membership[dkey] = data;
            return true;
        }

        public bool DeleteMember(UUID groupID, string principalID)
        {
            CompositeKey dkey = new CompositeKey(groupID, principalID);
            if (m_Membership.ContainsKey(dkey))
                return m_Membership.Remove(dkey);

            return false;
        }

        public int MemberCount(UUID groupID)
        {
            return m_Membership.Count;
        }
        #endregion

        #region roles table
        public bool StoreRole(RoleData data)
        {
            CompositeKey dkey = new CompositeKey(data.GroupID, data.RoleID.ToString());
            m_Roles[dkey] = data;
            return true;
        }

        public RoleData RetrieveRole(UUID groupID, UUID roleID)
        {
            CompositeKey dkey = new CompositeKey(groupID, roleID.ToString());
            if (m_Roles.ContainsKey(dkey))
                return m_Roles[dkey];

            return null;
        }

        public RoleData[] RetrieveRoles(UUID groupID)
        {
            IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString()));
            return keys.Where(m_Roles.ContainsKey).Select(x => m_Roles[x]).ToArray();
        }

        public bool DeleteRole(UUID groupID, UUID roleID)
        {
            CompositeKey dkey = new CompositeKey(groupID, roleID.ToString());
            if (m_Roles.ContainsKey(dkey))
                return m_Roles.Remove(dkey);

            return false;
        }

        public int RoleCount(UUID groupID)
        {
            return m_Roles.Count;
        }
        #endregion

        #region rolememberhip table
        public RoleMembershipData[] RetrieveRolesMembers(UUID groupID)
        {
            IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString()));
            return keys.Where(m_RoleMembership.ContainsKey).Select(x => m_RoleMembership[x]).ToArray();
        }

        public RoleMembershipData[] RetrieveRoleMembers(UUID groupID, UUID roleID)
        {
            IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString() + roleID.ToString()));
            return keys.Where(m_RoleMembership.ContainsKey).Select(x => m_RoleMembership[x]).ToArray();
        }

        public RoleMembershipData[] RetrieveMemberRoles(UUID groupID, string principalID)
        {
            IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString()) && k.Key.EndsWith(principalID));
            return keys.Where(m_RoleMembership.ContainsKey).Select(x => m_RoleMembership[x]).ToArray();
        }

        public RoleMembershipData RetrieveRoleMember(UUID groupID, UUID roleID, string principalID)
        {
            CompositeKey dkey = new CompositeKey(groupID, roleID.ToString(), principalID);
            if (m_RoleMembership.ContainsKey(dkey))
                return m_RoleMembership[dkey];

            return null;
        }

        public int RoleMemberCount(UUID groupID, UUID roleID)
        {
            return m_RoleMembership.Count;
        }

        public bool StoreRoleMember(RoleMembershipData data)
        {
            CompositeKey dkey = new CompositeKey(data.GroupID, data.RoleID.ToString(), data.PrincipalID);
            m_RoleMembership[dkey] = data;
            return true;
        }

        public bool DeleteRoleMember(RoleMembershipData data)
        {
            CompositeKey dkey = new CompositeKey(data.GroupID, data.RoleID.ToString(), data.PrincipalID);
            if (m_RoleMembership.ContainsKey(dkey))
                return m_RoleMembership.Remove(dkey);

            return false;
        }

        public bool DeleteMemberAllRoles(UUID groupID, string principalID)
        {
            List<CompositeKey> keys = m_RoleMembership.Keys.Where(k => k.Key.StartsWith(groupID.ToString()) && k.Key.EndsWith(principalID)).ToList();
            foreach (CompositeKey k in keys)
                m_RoleMembership.Remove(k);
            return true;
        }
        #endregion

        #region principals table
        public bool StorePrincipal(PrincipalData data)
        {
            m_Principals[data.PrincipalID] = data;
            return true;
        }

        public PrincipalData RetrievePrincipal(string principalID)
        {
            if (m_Principals.ContainsKey(principalID))
                return m_Principals[principalID];

            return null;
        }

        public bool DeletePrincipal(string principalID)
        {
            if (m_Principals.ContainsKey(principalID))
                return m_Principals.Remove(principalID);
            return false;
        }
        #endregion

        #region invites table
        public bool StoreInvitation(InvitationData data)
        {
            return false;
        }

        public InvitationData RetrieveInvitation(UUID inviteID)
        {
            return null;
        }

        public InvitationData RetrieveInvitation(UUID groupID, string principalID)
        {
            return null;
        }

        public bool DeleteInvite(UUID inviteID)
        {
            return false;
        }

        public void DeleteOldInvites()
        {
        }
        #endregion

        #region notices table
        public bool StoreNotice(NoticeData data)
        {
            return false;
        }

        public NoticeData RetrieveNotice(UUID noticeID)
        {
            return null;
        }

        public NoticeData[] RetrieveNotices(UUID groupID)
        {
            return new NoticeData[0];
        }

        public bool DeleteNotice(UUID noticeID)
        {
            return false;
        }

        public void DeleteOldNotices()
        {
        }
        #endregion

    }
}