1
0

TestGroupsDataPlugin.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using OpenMetaverse;
  6. using OpenSim.Data;
  7. namespace OpenSim.Tests.Common.Mock
  8. {
  9. public class TestGroupsDataPlugin : IGroupsData
  10. {
  11. class CompositeKey
  12. {
  13. private readonly string _key;
  14. public string Key
  15. {
  16. get { return _key; }
  17. }
  18. public CompositeKey(UUID _k1, string _k2)
  19. {
  20. _key = _k1.ToString() + _k2;
  21. }
  22. public CompositeKey(UUID _k1, string _k2, string _k3)
  23. {
  24. _key = _k1.ToString() + _k2 + _k3;
  25. }
  26. public override bool Equals(object obj)
  27. {
  28. if (obj is CompositeKey)
  29. {
  30. return Key == ((CompositeKey)obj).Key;
  31. }
  32. return false;
  33. }
  34. public override int GetHashCode()
  35. {
  36. return base.GetHashCode();
  37. }
  38. public override string ToString()
  39. {
  40. return Key;
  41. }
  42. }
  43. private Dictionary<UUID, GroupData> m_Groups;
  44. private Dictionary<CompositeKey, MembershipData> m_Membership;
  45. private Dictionary<CompositeKey, RoleData> m_Roles;
  46. private Dictionary<CompositeKey, RoleMembershipData> m_RoleMembership;
  47. private Dictionary<UUID, InvitationData> m_Invites;
  48. private Dictionary<UUID, NoticeData> m_Notices;
  49. private Dictionary<string, PrincipalData> m_Principals;
  50. public TestGroupsDataPlugin(string connectionString, string realm)
  51. {
  52. m_Groups = new Dictionary<UUID, GroupData>();
  53. m_Membership = new Dictionary<CompositeKey, MembershipData>();
  54. m_Roles = new Dictionary<CompositeKey, RoleData>();
  55. m_RoleMembership = new Dictionary<CompositeKey, RoleMembershipData>();
  56. m_Invites = new Dictionary<UUID, InvitationData>();
  57. m_Notices = new Dictionary<UUID, NoticeData>();
  58. m_Principals = new Dictionary<string, PrincipalData>();
  59. }
  60. #region groups table
  61. public bool StoreGroup(GroupData data)
  62. {
  63. return false;
  64. }
  65. public GroupData RetrieveGroup(UUID groupID)
  66. {
  67. if (m_Groups.ContainsKey(groupID))
  68. return m_Groups[groupID];
  69. return null;
  70. }
  71. public GroupData RetrieveGroup(string name)
  72. {
  73. return m_Groups.Values.First(g => g.Data.ContainsKey("Name") && g.Data["Name"] == name);
  74. }
  75. public GroupData[] RetrieveGroups(string pattern)
  76. {
  77. if (string.IsNullOrEmpty(pattern))
  78. pattern = "1";
  79. IEnumerable<GroupData> groups = m_Groups.Values.Where(g => g.Data.ContainsKey("Name") && (g.Data["Name"].StartsWith(pattern) || g.Data["Name"].EndsWith(pattern)));
  80. return (groups != null) ? groups.ToArray() : new GroupData[0];
  81. }
  82. public bool DeleteGroup(UUID groupID)
  83. {
  84. return m_Groups.Remove(groupID);
  85. }
  86. public int GroupsCount()
  87. {
  88. return m_Groups.Count;
  89. }
  90. #endregion
  91. #region membership table
  92. public MembershipData RetrieveMember(UUID groupID, string pricipalID)
  93. {
  94. CompositeKey dkey = new CompositeKey(groupID, pricipalID);
  95. if (m_Membership.ContainsKey(dkey))
  96. return m_Membership[dkey];
  97. return null;
  98. }
  99. public MembershipData[] RetrieveMembers(UUID groupID)
  100. {
  101. IEnumerable<CompositeKey> keys = m_Membership.Keys.Where(k => k.Key.StartsWith(groupID.ToString()));
  102. return keys.Where(m_Membership.ContainsKey).Select(x => m_Membership[x]).ToArray();
  103. }
  104. public MembershipData[] RetrieveMemberships(string principalID)
  105. {
  106. IEnumerable<CompositeKey> keys = m_Membership.Keys.Where(k => k.Key.EndsWith(principalID.ToString()));
  107. return keys.Where(m_Membership.ContainsKey).Select(x => m_Membership[x]).ToArray();
  108. }
  109. public MembershipData[] RetrievePrincipalGroupMemberships(string principalID)
  110. {
  111. return RetrieveMemberships(principalID);
  112. }
  113. public MembershipData RetrievePrincipalGroupMembership(string principalID, UUID groupID)
  114. {
  115. CompositeKey dkey = new CompositeKey(groupID, principalID);
  116. if (m_Membership.ContainsKey(dkey))
  117. return m_Membership[dkey];
  118. return null;
  119. }
  120. public bool StoreMember(MembershipData data)
  121. {
  122. CompositeKey dkey = new CompositeKey(data.GroupID, data.PrincipalID);
  123. m_Membership[dkey] = data;
  124. return true;
  125. }
  126. public bool DeleteMember(UUID groupID, string principalID)
  127. {
  128. CompositeKey dkey = new CompositeKey(groupID, principalID);
  129. if (m_Membership.ContainsKey(dkey))
  130. return m_Membership.Remove(dkey);
  131. return false;
  132. }
  133. public int MemberCount(UUID groupID)
  134. {
  135. return m_Membership.Count;
  136. }
  137. #endregion
  138. #region roles table
  139. public bool StoreRole(RoleData data)
  140. {
  141. CompositeKey dkey = new CompositeKey(data.GroupID, data.RoleID.ToString());
  142. m_Roles[dkey] = data;
  143. return true;
  144. }
  145. public RoleData RetrieveRole(UUID groupID, UUID roleID)
  146. {
  147. CompositeKey dkey = new CompositeKey(groupID, roleID.ToString());
  148. if (m_Roles.ContainsKey(dkey))
  149. return m_Roles[dkey];
  150. return null;
  151. }
  152. public RoleData[] RetrieveRoles(UUID groupID)
  153. {
  154. IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString()));
  155. return keys.Where(m_Roles.ContainsKey).Select(x => m_Roles[x]).ToArray();
  156. }
  157. public bool DeleteRole(UUID groupID, UUID roleID)
  158. {
  159. CompositeKey dkey = new CompositeKey(groupID, roleID.ToString());
  160. if (m_Roles.ContainsKey(dkey))
  161. return m_Roles.Remove(dkey);
  162. return false;
  163. }
  164. public int RoleCount(UUID groupID)
  165. {
  166. return m_Roles.Count;
  167. }
  168. #endregion
  169. #region rolememberhip table
  170. public RoleMembershipData[] RetrieveRolesMembers(UUID groupID)
  171. {
  172. IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString()));
  173. return keys.Where(m_RoleMembership.ContainsKey).Select(x => m_RoleMembership[x]).ToArray();
  174. }
  175. public RoleMembershipData[] RetrieveRoleMembers(UUID groupID, UUID roleID)
  176. {
  177. IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString() + roleID.ToString()));
  178. return keys.Where(m_RoleMembership.ContainsKey).Select(x => m_RoleMembership[x]).ToArray();
  179. }
  180. public RoleMembershipData[] RetrieveMemberRoles(UUID groupID, string principalID)
  181. {
  182. IEnumerable<CompositeKey> keys = m_Roles.Keys.Where(k => k.Key.StartsWith(groupID.ToString()) && k.Key.EndsWith(principalID));
  183. return keys.Where(m_RoleMembership.ContainsKey).Select(x => m_RoleMembership[x]).ToArray();
  184. }
  185. public RoleMembershipData RetrieveRoleMember(UUID groupID, UUID roleID, string principalID)
  186. {
  187. CompositeKey dkey = new CompositeKey(groupID, roleID.ToString(), principalID);
  188. if (m_RoleMembership.ContainsKey(dkey))
  189. return m_RoleMembership[dkey];
  190. return null;
  191. }
  192. public int RoleMemberCount(UUID groupID, UUID roleID)
  193. {
  194. return m_RoleMembership.Count;
  195. }
  196. public bool StoreRoleMember(RoleMembershipData data)
  197. {
  198. CompositeKey dkey = new CompositeKey(data.GroupID, data.RoleID.ToString(), data.PrincipalID);
  199. m_RoleMembership[dkey] = data;
  200. return true;
  201. }
  202. public bool DeleteRoleMember(RoleMembershipData data)
  203. {
  204. CompositeKey dkey = new CompositeKey(data.GroupID, data.RoleID.ToString(), data.PrincipalID);
  205. if (m_RoleMembership.ContainsKey(dkey))
  206. return m_RoleMembership.Remove(dkey);
  207. return false;
  208. }
  209. public bool DeleteMemberAllRoles(UUID groupID, string principalID)
  210. {
  211. List<CompositeKey> keys = m_RoleMembership.Keys.Where(k => k.Key.StartsWith(groupID.ToString()) && k.Key.EndsWith(principalID)).ToList();
  212. foreach (CompositeKey k in keys)
  213. m_RoleMembership.Remove(k);
  214. return true;
  215. }
  216. #endregion
  217. #region principals table
  218. public bool StorePrincipal(PrincipalData data)
  219. {
  220. m_Principals[data.PrincipalID] = data;
  221. return true;
  222. }
  223. public PrincipalData RetrievePrincipal(string principalID)
  224. {
  225. if (m_Principals.ContainsKey(principalID))
  226. return m_Principals[principalID];
  227. return null;
  228. }
  229. public bool DeletePrincipal(string principalID)
  230. {
  231. if (m_Principals.ContainsKey(principalID))
  232. return m_Principals.Remove(principalID);
  233. return false;
  234. }
  235. #endregion
  236. #region invites table
  237. public bool StoreInvitation(InvitationData data)
  238. {
  239. return false;
  240. }
  241. public InvitationData RetrieveInvitation(UUID inviteID)
  242. {
  243. return null;
  244. }
  245. public InvitationData RetrieveInvitation(UUID groupID, string principalID)
  246. {
  247. return null;
  248. }
  249. public bool DeleteInvite(UUID inviteID)
  250. {
  251. return false;
  252. }
  253. public void DeleteOldInvites()
  254. {
  255. }
  256. #endregion
  257. #region notices table
  258. public bool StoreNotice(NoticeData data)
  259. {
  260. return false;
  261. }
  262. public NoticeData RetrieveNotice(UUID noticeID)
  263. {
  264. return null;
  265. }
  266. public NoticeData[] RetrieveNotices(UUID groupID)
  267. {
  268. return new NoticeData[0];
  269. }
  270. public bool DeleteNotice(UUID noticeID)
  271. {
  272. return false;
  273. }
  274. public void DeleteOldNotices()
  275. {
  276. }
  277. #endregion
  278. }
  279. }