GroupsService.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Timers;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Data;
  35. using OpenSim.Framework;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Groups
  38. {
  39. public class GroupsService : GroupsServiceBase
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. public const GroupPowers DefaultEveryonePowers = GroupPowers.AllowSetHome |
  43. GroupPowers.Accountable |
  44. GroupPowers.JoinChat |
  45. GroupPowers.AllowVoiceChat |
  46. GroupPowers.ReceiveNotices |
  47. GroupPowers.StartProposal |
  48. GroupPowers.VoteOnProposal;
  49. public const GroupPowers OwnerPowers = GroupPowers.Accountable |
  50. GroupPowers.AllowEditLand |
  51. GroupPowers.AllowFly |
  52. GroupPowers.AllowLandmark |
  53. GroupPowers.AllowRez |
  54. GroupPowers.AllowSetHome |
  55. GroupPowers.AllowVoiceChat |
  56. GroupPowers.AssignMember |
  57. GroupPowers.AssignMemberLimited |
  58. GroupPowers.ChangeActions |
  59. GroupPowers.ChangeIdentity |
  60. GroupPowers.ChangeMedia |
  61. GroupPowers.ChangeOptions |
  62. GroupPowers.CreateRole |
  63. GroupPowers.DeedObject |
  64. GroupPowers.DeleteRole |
  65. GroupPowers.Eject |
  66. GroupPowers.FindPlaces |
  67. GroupPowers.Invite |
  68. GroupPowers.JoinChat |
  69. GroupPowers.LandChangeIdentity |
  70. GroupPowers.LandDeed |
  71. GroupPowers.LandDivideJoin |
  72. GroupPowers.LandEdit |
  73. GroupPowers.LandEjectAndFreeze |
  74. GroupPowers.LandGardening |
  75. GroupPowers.LandManageAllowed |
  76. GroupPowers.LandManageBanned |
  77. GroupPowers.LandManagePasses |
  78. GroupPowers.LandOptions |
  79. GroupPowers.LandRelease |
  80. GroupPowers.LandSetSale |
  81. GroupPowers.ModerateChat |
  82. GroupPowers.ObjectManipulate |
  83. GroupPowers.ObjectSetForSale |
  84. GroupPowers.ReceiveNotices |
  85. GroupPowers.RemoveMember |
  86. GroupPowers.ReturnGroupOwned |
  87. GroupPowers.ReturnGroupSet |
  88. GroupPowers.ReturnNonGroup |
  89. GroupPowers.RoleProperties |
  90. GroupPowers.SendNotices |
  91. GroupPowers.SetLandingPoint |
  92. GroupPowers.StartProposal |
  93. GroupPowers.VoteOnProposal;
  94. #region Daily Cleanup
  95. private Timer m_CleanupTimer;
  96. public GroupsService(IConfigSource config, string configName)
  97. : base(config, configName)
  98. {
  99. }
  100. public GroupsService(IConfigSource config)
  101. : this(config, string.Empty)
  102. {
  103. // Once a day
  104. m_CleanupTimer = new Timer(24 * 60 * 60 * 1000);
  105. m_CleanupTimer.AutoReset = true;
  106. m_CleanupTimer.Elapsed += new ElapsedEventHandler(m_CleanupTimer_Elapsed);
  107. m_CleanupTimer.Enabled = true;
  108. m_CleanupTimer.Start();
  109. }
  110. private void m_CleanupTimer_Elapsed(object sender, ElapsedEventArgs e)
  111. {
  112. m_Database.DeleteOldNotices();
  113. m_Database.DeleteOldInvites();
  114. }
  115. #endregion
  116. public UUID CreateGroup(string RequestingAgentID, string name, string charter, bool showInList, UUID insigniaID, int membershipFee, bool openEnrollment,
  117. bool allowPublish, bool maturePublish, UUID founderID, out string reason)
  118. {
  119. reason = string.Empty;
  120. // Check if the group already exists
  121. if (m_Database.RetrieveGroup(name) != null)
  122. {
  123. reason = "A group with that name already exists";
  124. return UUID.Zero;
  125. }
  126. // Create the group
  127. GroupData data = new GroupData();
  128. data.GroupID = UUID.Random();
  129. data.Data = new Dictionary<string, string>();
  130. data.Data["Name"] = name;
  131. data.Data["Charter"] = charter;
  132. data.Data["InsigniaID"] = insigniaID.ToString();
  133. data.Data["FounderID"] = founderID.ToString();
  134. data.Data["MembershipFee"] = membershipFee.ToString();
  135. data.Data["OpenEnrollment"] = openEnrollment ? "1" : "0";
  136. data.Data["ShowInList"] = showInList ? "1" : "0";
  137. data.Data["AllowPublish"] = allowPublish ? "1" : "0";
  138. data.Data["MaturePublish"] = maturePublish ? "1" : "0";
  139. UUID roleID = UUID.Random();
  140. data.Data["OwnerRoleID"] = roleID.ToString();
  141. if (!m_Database.StoreGroup(data))
  142. return UUID.Zero;
  143. // Create Everyone role
  144. _AddOrUpdateGroupRole(RequestingAgentID, data.GroupID, UUID.Zero, "Everyone", "Everyone in the group", "Member of " + name, (ulong)DefaultEveryonePowers, true);
  145. // Create Owner role
  146. _AddOrUpdateGroupRole(RequestingAgentID, data.GroupID, roleID, "Owners", "Owners of the group", "Owner of " + name, (ulong)OwnerPowers, true);
  147. // Add founder to group
  148. _AddAgentToGroup(RequestingAgentID, founderID.ToString(), data.GroupID, roleID);
  149. return data.GroupID;
  150. }
  151. public void UpdateGroup(string RequestingAgentID, UUID groupID, string charter, bool showInList, UUID insigniaID, int membershipFee, bool openEnrollment, bool allowPublish, bool maturePublish)
  152. {
  153. GroupData data = m_Database.RetrieveGroup(groupID);
  154. if (data == null)
  155. return;
  156. // Check perms
  157. if (!HasPower(RequestingAgentID, groupID, GroupPowers.ChangeActions))
  158. {
  159. m_log.DebugFormat("[Groups]: ({0}) Attempt at updating group {1} denied because of lack of permission", RequestingAgentID, groupID);
  160. return;
  161. }
  162. data.GroupID = groupID;
  163. data.Data["Charter"] = charter;
  164. data.Data["ShowInList"] = showInList ? "1" : "0";
  165. data.Data["InsigniaID"] = insigniaID.ToString();
  166. data.Data["MembershipFee"] = membershipFee.ToString();
  167. data.Data["OpenEnrollment"] = openEnrollment ? "1" : "0";
  168. data.Data["AllowPublish"] = allowPublish ? "1" : "0";
  169. data.Data["MaturePublish"] = maturePublish ? "1" : "0";
  170. m_Database.StoreGroup(data);
  171. }
  172. public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, UUID GroupID)
  173. {
  174. GroupData data = m_Database.RetrieveGroup(GroupID);
  175. return _GroupDataToRecord(data);
  176. }
  177. public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, string GroupName)
  178. {
  179. GroupData data = m_Database.RetrieveGroup(GroupName);
  180. return _GroupDataToRecord(data);
  181. }
  182. public List<DirGroupsReplyData> FindGroups(string RequestingAgentID, string search)
  183. {
  184. List<DirGroupsReplyData> groups = new List<DirGroupsReplyData>();
  185. GroupData[] data = m_Database.RetrieveGroups(search);
  186. if (data != null && data.Length > 0)
  187. {
  188. foreach (GroupData d in data)
  189. {
  190. // Don't list group proxies
  191. if (d.Data.ContainsKey("Location") && d.Data["Location"] != string.Empty)
  192. continue;
  193. DirGroupsReplyData g = new DirGroupsReplyData();
  194. g.groupID = d.GroupID;
  195. if (d.Data.ContainsKey("Name"))
  196. g.groupName = d.Data["Name"];
  197. else
  198. m_log.DebugFormat("[Groups]: Key Name not found");
  199. g.members = m_Database.MemberCount(d.GroupID);
  200. groups.Add(g);
  201. }
  202. }
  203. return groups;
  204. }
  205. public List<ExtendedGroupMembersData> GetGroupMembers(string RequestingAgentID, UUID GroupID)
  206. {
  207. List<ExtendedGroupMembersData> members = new List<ExtendedGroupMembersData>();
  208. GroupData group = m_Database.RetrieveGroup(GroupID);
  209. if (group == null)
  210. return members;
  211. // Unfortunately this doesn't quite work on legacy group data because of a bug
  212. // that's also being fixed here on CreateGroup. The OwnerRoleID sent to the DB was wrong.
  213. // See how to find the ownerRoleID a few lines below.
  214. UUID ownerRoleID = new UUID(group.Data["OwnerRoleID"]);
  215. RoleData[] roles = m_Database.RetrieveRoles(GroupID);
  216. if (roles == null)
  217. // something wrong with this group
  218. return members;
  219. List<RoleData> rolesList = new List<RoleData>(roles);
  220. // Let's find the "real" ownerRoleID
  221. RoleData ownerRole = rolesList.Find(r => r.Data["Powers"] == ((long)OwnerPowers).ToString());
  222. if (ownerRole != null)
  223. ownerRoleID = ownerRole.RoleID;
  224. // Check visibility?
  225. // When we don't want to check visibility, we pass it "all" as the requestingAgentID
  226. bool checkVisibility = !RequestingAgentID.Equals(UUID.Zero.ToString());
  227. if (checkVisibility)
  228. {
  229. // Is the requester a member of the group?
  230. bool isInGroup = false;
  231. if (m_Database.RetrieveMember(GroupID, RequestingAgentID) != null)
  232. isInGroup = true;
  233. if (!isInGroup) // reduce the roles to the visible ones
  234. rolesList = rolesList.FindAll(r => (UInt64.Parse(r.Data["Powers"]) & (ulong)GroupPowers.MemberVisible) != 0);
  235. }
  236. MembershipData[] datas = m_Database.RetrieveMembers(GroupID);
  237. if (datas == null || (datas != null && datas.Length == 0))
  238. return members;
  239. // OK, we have everything we need
  240. foreach (MembershipData d in datas)
  241. {
  242. RoleMembershipData[] rolememberships = m_Database.RetrieveMemberRoles(GroupID, d.PrincipalID);
  243. List<RoleMembershipData> rolemembershipsList = new List<RoleMembershipData>(rolememberships);
  244. ExtendedGroupMembersData m = new ExtendedGroupMembersData();
  245. // What's this person's current role in the group?
  246. UUID selectedRole = new UUID(d.Data["SelectedRoleID"]);
  247. RoleData selected = rolesList.Find(r => r.RoleID == selectedRole);
  248. if (selected != null)
  249. {
  250. m.Title = selected.Data["Title"];
  251. m.AgentPowers = UInt64.Parse(selected.Data["Powers"]);
  252. }
  253. m.AgentID = d.PrincipalID;
  254. m.AcceptNotices = d.Data["AcceptNotices"] == "1" ? true : false;
  255. m.Contribution = Int32.Parse(d.Data["Contribution"]);
  256. m.ListInProfile = d.Data["ListInProfile"] == "1" ? true : false;
  257. // Is this person an owner of the group?
  258. m.IsOwner = (rolemembershipsList.Find(r => r.RoleID == ownerRoleID) != null) ? true : false;
  259. members.Add(m);
  260. }
  261. return members;
  262. }
  263. public bool AddGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers, out string reason)
  264. {
  265. reason = string.Empty;
  266. // check that the requesting agent has permissions to add role
  267. if (!HasPower(RequestingAgentID, groupID, GroupPowers.CreateRole))
  268. {
  269. m_log.DebugFormat("[Groups]: ({0}) Attempt at creating role in group {1} denied because of lack of permission", RequestingAgentID, groupID);
  270. reason = "Insufficient permission to create role";
  271. return false;
  272. }
  273. return _AddOrUpdateGroupRole(RequestingAgentID, groupID, roleID, name, description, title, powers, true);
  274. }
  275. public bool UpdateGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers)
  276. {
  277. // check perms
  278. if (!HasPower(RequestingAgentID, groupID, GroupPowers.ChangeActions))
  279. {
  280. m_log.DebugFormat("[Groups]: ({0}) Attempt at changing role in group {1} denied because of lack of permission", RequestingAgentID, groupID);
  281. return false;
  282. }
  283. return _AddOrUpdateGroupRole(RequestingAgentID, groupID, roleID, name, description, title, powers, false);
  284. }
  285. public void RemoveGroupRole(string RequestingAgentID, UUID groupID, UUID roleID)
  286. {
  287. // check perms
  288. if (!HasPower(RequestingAgentID, groupID, GroupPowers.DeleteRole))
  289. {
  290. m_log.DebugFormat("[Groups]: ({0}) Attempt at deleting role from group {1} denied because of lack of permission", RequestingAgentID, groupID);
  291. return;
  292. }
  293. // Can't delete Everyone and Owners roles
  294. if (roleID == UUID.Zero)
  295. {
  296. m_log.DebugFormat("[Groups]: Attempt at deleting Everyone role from group {0} denied", groupID);
  297. return;
  298. }
  299. GroupData group = m_Database.RetrieveGroup(groupID);
  300. if (group == null)
  301. {
  302. m_log.DebugFormat("[Groups]: Attempt at deleting role from non-existing group {0}", groupID);
  303. return;
  304. }
  305. if (roleID == new UUID(group.Data["OwnerRoleID"]))
  306. {
  307. m_log.DebugFormat("[Groups]: Attempt at deleting Owners role from group {0} denied", groupID);
  308. return;
  309. }
  310. _RemoveGroupRole(groupID, roleID);
  311. }
  312. public List<GroupRolesData> GetGroupRoles(string RequestingAgentID, UUID GroupID)
  313. {
  314. // TODO: check perms
  315. return _GetGroupRoles(GroupID);
  316. }
  317. public List<ExtendedGroupRoleMembersData> GetGroupRoleMembers(string RequestingAgentID, UUID GroupID)
  318. {
  319. // TODO: check perms
  320. // Is the requester a member of the group?
  321. bool isInGroup = false;
  322. if (m_Database.RetrieveMember(GroupID, RequestingAgentID) != null)
  323. isInGroup = true;
  324. return _GetGroupRoleMembers(GroupID, isInGroup);
  325. }
  326. public bool AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID, string token, out string reason)
  327. {
  328. reason = string.Empty;
  329. _AddAgentToGroup(RequestingAgentID, AgentID, GroupID, RoleID, token);
  330. return true;
  331. }
  332. public bool RemoveAgentFromGroup(string RequestingAgentID, string AgentID, UUID GroupID)
  333. {
  334. // check perms
  335. if (RequestingAgentID != AgentID && !HasPower(RequestingAgentID, GroupID, GroupPowers.Eject))
  336. return false;
  337. _RemoveAgentFromGroup(RequestingAgentID, AgentID, GroupID);
  338. return true;
  339. }
  340. public bool AddAgentToGroupInvite(string RequestingAgentID, UUID inviteID, UUID groupID, UUID roleID, string agentID)
  341. {
  342. // Check whether the invitee is already a member of the group
  343. MembershipData m = m_Database.RetrieveMember(groupID, agentID);
  344. if (m != null)
  345. return false;
  346. // Check permission to invite
  347. if (!HasPower(RequestingAgentID, groupID, GroupPowers.Invite))
  348. {
  349. m_log.DebugFormat("[Groups]: ({0}) Attempt at inviting to group {1} denied because of lack of permission", RequestingAgentID, groupID);
  350. return false;
  351. }
  352. // Check whether there are pending invitations and delete them
  353. InvitationData invite = m_Database.RetrieveInvitation(groupID, agentID);
  354. if (invite != null)
  355. m_Database.DeleteInvite(invite.InviteID);
  356. invite = new InvitationData();
  357. invite.InviteID = inviteID;
  358. invite.PrincipalID = agentID;
  359. invite.GroupID = groupID;
  360. invite.RoleID = roleID;
  361. invite.Data = new Dictionary<string, string>();
  362. return m_Database.StoreInvitation(invite);
  363. }
  364. public GroupInviteInfo GetAgentToGroupInvite(string RequestingAgentID, UUID inviteID)
  365. {
  366. InvitationData data = m_Database.RetrieveInvitation(inviteID);
  367. if (data == null)
  368. return null;
  369. GroupInviteInfo inviteInfo = new GroupInviteInfo();
  370. inviteInfo.AgentID = data.PrincipalID;
  371. inviteInfo.GroupID = data.GroupID;
  372. inviteInfo.InviteID = data.InviteID;
  373. inviteInfo.RoleID = data.RoleID;
  374. return inviteInfo;
  375. }
  376. public void RemoveAgentToGroupInvite(string RequestingAgentID, UUID inviteID)
  377. {
  378. m_Database.DeleteInvite(inviteID);
  379. }
  380. public bool AddAgentToGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  381. {
  382. //if (!m_Database.CheckOwnerRole(RequestingAgentID, GroupID, RoleID))
  383. // return;
  384. // check permissions
  385. bool limited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMemberLimited);
  386. bool unlimited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMember) | IsOwner(RequestingAgentID, GroupID);
  387. if (!limited || !unlimited)
  388. {
  389. m_log.DebugFormat("[Groups]: ({0}) Attempt at assigning {1} to role {2} denied because of lack of permission", RequestingAgentID, AgentID, RoleID);
  390. return false;
  391. }
  392. // AssignMemberLimited means that the person can assign another person to the same roles that she has in the group
  393. if (!unlimited && limited)
  394. {
  395. // check whether person's has this role
  396. RoleMembershipData rolemembership = m_Database.RetrieveRoleMember(GroupID, RoleID, AgentID);
  397. if (rolemembership == null)
  398. {
  399. m_log.DebugFormat("[Groups]: ({0}) Attempt at assigning {1} to role {2} denied because of limited permission", RequestingAgentID, AgentID, RoleID);
  400. return false;
  401. }
  402. }
  403. _AddAgentToGroupRole(RequestingAgentID, AgentID, GroupID, RoleID);
  404. return true;
  405. }
  406. public bool RemoveAgentFromGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  407. {
  408. // Don't remove from Everyone role!
  409. if (RoleID == UUID.Zero)
  410. return false;
  411. // check permissions
  412. bool unlimited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMember) || IsOwner(RequestingAgentID, GroupID);
  413. if (!unlimited)
  414. {
  415. m_log.DebugFormat("[Groups]: ({0}) Attempt at removing {1} from role {2} denied because of lack of permission", RequestingAgentID, AgentID, RoleID);
  416. return false;
  417. }
  418. RoleMembershipData rolemember = m_Database.RetrieveRoleMember(GroupID, RoleID, AgentID);
  419. if (rolemember == null)
  420. return false;
  421. m_Database.DeleteRoleMember(rolemember);
  422. // Find another role for this person
  423. UUID newRoleID = UUID.Zero; // Everyone
  424. RoleMembershipData[] rdata = m_Database.RetrieveMemberRoles(GroupID, AgentID);
  425. if (rdata != null)
  426. foreach (RoleMembershipData r in rdata)
  427. {
  428. if (r.RoleID != UUID.Zero)
  429. {
  430. newRoleID = r.RoleID;
  431. break;
  432. }
  433. }
  434. MembershipData member = m_Database.RetrieveMember(GroupID, AgentID);
  435. if (member != null)
  436. {
  437. member.Data["SelectedRoleID"] = newRoleID.ToString();
  438. m_Database.StoreMember(member);
  439. }
  440. return true;
  441. }
  442. public List<GroupRolesData> GetAgentGroupRoles(string RequestingAgentID, string AgentID, UUID GroupID)
  443. {
  444. List<GroupRolesData> roles = new List<GroupRolesData>();
  445. // TODO: check permissions
  446. RoleMembershipData[] data = m_Database.RetrieveMemberRoles(GroupID, AgentID);
  447. if (data == null || (data != null && data.Length ==0))
  448. return roles;
  449. foreach (RoleMembershipData d in data)
  450. {
  451. RoleData rdata = m_Database.RetrieveRole(GroupID, d.RoleID);
  452. if (rdata == null) // hippos
  453. continue;
  454. GroupRolesData r = new GroupRolesData();
  455. r.Name = rdata.Data["Name"];
  456. r.Powers = UInt64.Parse(rdata.Data["Powers"]);
  457. r.RoleID = rdata.RoleID;
  458. r.Title = rdata.Data["Title"];
  459. roles.Add(r);
  460. }
  461. return roles;
  462. }
  463. public ExtendedGroupMembershipData SetAgentActiveGroup(string RequestingAgentID, string AgentID, UUID GroupID)
  464. {
  465. // TODO: check perms
  466. PrincipalData principal = new PrincipalData();
  467. principal.PrincipalID = AgentID;
  468. principal.ActiveGroupID = GroupID;
  469. m_Database.StorePrincipal(principal);
  470. return GetAgentGroupMembership(RequestingAgentID, AgentID, GroupID);
  471. }
  472. public ExtendedGroupMembershipData GetAgentActiveMembership(string RequestingAgentID, string AgentID)
  473. {
  474. // 1. get the principal data for the active group
  475. PrincipalData principal = m_Database.RetrievePrincipal(AgentID);
  476. if (principal == null)
  477. return null;
  478. return GetAgentGroupMembership(RequestingAgentID, AgentID, principal.ActiveGroupID);
  479. }
  480. public ExtendedGroupMembershipData GetAgentGroupMembership(string RequestingAgentID, string AgentID, UUID GroupID)
  481. {
  482. return GetAgentGroupMembership(RequestingAgentID, AgentID, GroupID, null);
  483. }
  484. private ExtendedGroupMembershipData GetAgentGroupMembership(string RequestingAgentID, string AgentID, UUID GroupID, MembershipData membership)
  485. {
  486. // 2. get the active group
  487. GroupData group = m_Database.RetrieveGroup(GroupID);
  488. if (group == null)
  489. return null;
  490. // 3. get the membership info if we don't have it already
  491. if (membership == null)
  492. {
  493. membership = m_Database.RetrieveMember(group.GroupID, AgentID);
  494. if (membership == null)
  495. return null;
  496. }
  497. // 4. get the active role
  498. UUID activeRoleID = new UUID(membership.Data["SelectedRoleID"]);
  499. RoleData role = m_Database.RetrieveRole(group.GroupID, activeRoleID);
  500. ExtendedGroupMembershipData data = new ExtendedGroupMembershipData();
  501. data.AcceptNotices = membership.Data["AcceptNotices"] == "1" ? true : false;
  502. data.AccessToken = membership.Data["AccessToken"];
  503. data.Active = true;
  504. data.ActiveRole = activeRoleID;
  505. data.AllowPublish = group.Data["AllowPublish"] == "1" ? true : false;
  506. data.Charter = group.Data["Charter"];
  507. data.Contribution = Int32.Parse(membership.Data["Contribution"]);
  508. data.FounderID = new UUID(group.Data["FounderID"]);
  509. data.GroupID = new UUID(group.GroupID);
  510. data.GroupName = group.Data["Name"];
  511. data.GroupPicture = new UUID(group.Data["InsigniaID"]);
  512. if (role != null)
  513. {
  514. data.GroupPowers = UInt64.Parse(role.Data["Powers"]);
  515. data.GroupTitle = role.Data["Title"];
  516. }
  517. data.ListInProfile = membership.Data["ListInProfile"] == "1" ? true : false;
  518. data.MaturePublish = group.Data["MaturePublish"] == "1" ? true : false;
  519. data.MembershipFee = Int32.Parse(group.Data["MembershipFee"]);
  520. data.OpenEnrollment = group.Data["OpenEnrollment"] == "1" ? true : false;
  521. data.ShowInList = group.Data["ShowInList"] == "1" ? true : false;
  522. return data;
  523. }
  524. public List<GroupMembershipData> GetAgentGroupMemberships(string RequestingAgentID, string AgentID)
  525. {
  526. List<GroupMembershipData> memberships = new List<GroupMembershipData>();
  527. // 1. Get all the groups that this person is a member of
  528. MembershipData[] mdata = m_Database.RetrieveMemberships(AgentID);
  529. if (mdata == null || (mdata != null && mdata.Length == 0))
  530. return memberships;
  531. foreach (MembershipData d in mdata)
  532. {
  533. GroupMembershipData gmember = GetAgentGroupMembership(RequestingAgentID, AgentID, d.GroupID, d);
  534. if (gmember != null)
  535. {
  536. memberships.Add(gmember);
  537. //m_log.DebugFormat("[XXX]: Member of {0} as {1}", gmember.GroupName, gmember.GroupTitle);
  538. //Util.PrintCallStack();
  539. }
  540. }
  541. return memberships;
  542. }
  543. public void SetAgentActiveGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  544. {
  545. MembershipData data = m_Database.RetrieveMember(GroupID, AgentID);
  546. if (data == null)
  547. return;
  548. data.Data["SelectedRoleID"] = RoleID.ToString();
  549. m_Database.StoreMember(data);
  550. }
  551. public void UpdateMembership(string RequestingAgentID, string AgentID, UUID GroupID, bool AcceptNotices, bool ListInProfile)
  552. {
  553. // TODO: check perms
  554. MembershipData membership = m_Database.RetrieveMember(GroupID, AgentID);
  555. if (membership == null)
  556. return;
  557. membership.Data["AcceptNotices"] = AcceptNotices ? "1" : "0";
  558. membership.Data["ListInProfile"] = ListInProfile ? "1" : "0";
  559. m_Database.StoreMember(membership);
  560. }
  561. public bool AddGroupNotice(string RequestingAgentID, UUID groupID, UUID noticeID, string fromName, string subject, string message,
  562. bool hasAttachment, byte attType, string attName, UUID attItemID, string attOwnerID)
  563. {
  564. // Check perms
  565. if (!HasPower(RequestingAgentID, groupID, GroupPowers.SendNotices))
  566. {
  567. m_log.DebugFormat("[Groups]: ({0}) Attempt at sending notice to group {1} denied because of lack of permission", RequestingAgentID, groupID);
  568. return false;
  569. }
  570. return _AddNotice(groupID, noticeID, fromName, subject, message, hasAttachment, attType, attName, attItemID, attOwnerID);
  571. }
  572. public GroupNoticeInfo GetGroupNotice(string RequestingAgentID, UUID noticeID)
  573. {
  574. NoticeData data = m_Database.RetrieveNotice(noticeID);
  575. if (data == null)
  576. return null;
  577. return _NoticeDataToInfo(data);
  578. }
  579. public List<ExtendedGroupNoticeData> GetGroupNotices(string RequestingAgentID, UUID groupID)
  580. {
  581. NoticeData[] data = m_Database.RetrieveNotices(groupID);
  582. List<ExtendedGroupNoticeData> infos = new List<ExtendedGroupNoticeData>();
  583. if (data == null || (data != null && data.Length == 0))
  584. return infos;
  585. foreach (NoticeData d in data)
  586. {
  587. ExtendedGroupNoticeData info = _NoticeDataToData(d);
  588. infos.Add(info);
  589. }
  590. return infos;
  591. }
  592. public void ResetAgentGroupChatSessions(string agentID)
  593. {
  594. }
  595. public bool hasAgentBeenInvitedToGroupChatSession(string agentID, UUID groupID)
  596. {
  597. return false;
  598. }
  599. public bool hasAgentDroppedGroupChatSession(string agentID, UUID groupID)
  600. {
  601. return false;
  602. }
  603. public void AgentDroppedFromGroupChatSession(string agentID, UUID groupID)
  604. {
  605. }
  606. public void AgentInvitedToGroupChatSession(string agentID, UUID groupID)
  607. {
  608. }
  609. #region Actions without permission checks
  610. protected void _AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  611. {
  612. _AddAgentToGroup(RequestingAgentID, AgentID, GroupID, RoleID, string.Empty);
  613. }
  614. protected void _RemoveAgentFromGroup(string RequestingAgentID, string AgentID, UUID GroupID)
  615. {
  616. // 1. Delete membership
  617. m_Database.DeleteMember(GroupID, AgentID);
  618. // 2. Remove from rolememberships
  619. m_Database.DeleteMemberAllRoles(GroupID, AgentID);
  620. // 3. if it was active group, inactivate it
  621. PrincipalData principal = m_Database.RetrievePrincipal(AgentID);
  622. if (principal != null && principal.ActiveGroupID == GroupID)
  623. {
  624. principal.ActiveGroupID = UUID.Zero;
  625. m_Database.StorePrincipal(principal);
  626. }
  627. }
  628. protected void _AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID, string accessToken)
  629. {
  630. // Check if it's already there
  631. MembershipData data = m_Database.RetrieveMember(GroupID, AgentID);
  632. if (data != null)
  633. return;
  634. // Add the membership
  635. data = new MembershipData();
  636. data.PrincipalID = AgentID;
  637. data.GroupID = GroupID;
  638. data.Data = new Dictionary<string, string>();
  639. data.Data["SelectedRoleID"] = RoleID.ToString();
  640. data.Data["Contribution"] = "0";
  641. data.Data["ListInProfile"] = "1";
  642. data.Data["AcceptNotices"] = "1";
  643. data.Data["AccessToken"] = accessToken;
  644. m_Database.StoreMember(data);
  645. // Add principal to everyone role
  646. _AddAgentToGroupRole(RequestingAgentID, AgentID, GroupID, UUID.Zero);
  647. // Add principal to role, if different from everyone role
  648. if (RoleID != UUID.Zero)
  649. _AddAgentToGroupRole(RequestingAgentID, AgentID, GroupID, RoleID);
  650. // Make thit this active group
  651. PrincipalData pdata = new PrincipalData();
  652. pdata.PrincipalID = AgentID;
  653. pdata.ActiveGroupID = GroupID;
  654. m_Database.StorePrincipal(pdata);
  655. }
  656. protected bool _AddOrUpdateGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers, bool add)
  657. {
  658. RoleData data = m_Database.RetrieveRole(groupID, roleID);
  659. if (add && data != null) // it already exists, can't create
  660. {
  661. m_log.DebugFormat("[Groups]: Group {0} already exists. Can't create it again", groupID);
  662. return false;
  663. }
  664. if (!add && data == null) // it deosn't exist, can't update
  665. {
  666. m_log.DebugFormat("[Groups]: Group {0} doesn't exist. Can't update it", groupID);
  667. return false;
  668. }
  669. if (add)
  670. data = new RoleData();
  671. data.GroupID = groupID;
  672. data.RoleID = roleID;
  673. data.Data = new Dictionary<string, string>();
  674. data.Data["Name"] = name;
  675. data.Data["Description"] = description;
  676. data.Data["Title"] = title;
  677. data.Data["Powers"] = powers.ToString();
  678. return m_Database.StoreRole(data);
  679. }
  680. protected void _RemoveGroupRole(UUID groupID, UUID roleID)
  681. {
  682. m_Database.DeleteRole(groupID, roleID);
  683. }
  684. protected void _AddAgentToGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  685. {
  686. RoleMembershipData data = m_Database.RetrieveRoleMember(GroupID, RoleID, AgentID);
  687. if (data != null)
  688. return;
  689. data = new RoleMembershipData();
  690. data.GroupID = GroupID;
  691. data.PrincipalID = AgentID;
  692. data.RoleID = RoleID;
  693. m_Database.StoreRoleMember(data);
  694. // Make it the SelectedRoleID
  695. MembershipData membership = m_Database.RetrieveMember(GroupID, AgentID);
  696. if (membership == null)
  697. {
  698. m_log.DebugFormat("[Groups]: ({0}) No such member {0} in group {1}", AgentID, GroupID);
  699. return;
  700. }
  701. membership.Data["SelectedRoleID"] = RoleID.ToString();
  702. m_Database.StoreMember(membership);
  703. }
  704. protected List<GroupRolesData> _GetGroupRoles(UUID groupID)
  705. {
  706. List<GroupRolesData> roles = new List<GroupRolesData>();
  707. RoleData[] data = m_Database.RetrieveRoles(groupID);
  708. if (data == null || (data != null && data.Length == 0))
  709. return roles;
  710. foreach (RoleData d in data)
  711. {
  712. GroupRolesData r = new GroupRolesData();
  713. r.Description = d.Data["Description"];
  714. r.Members = m_Database.RoleMemberCount(groupID, d.RoleID);
  715. r.Name = d.Data["Name"];
  716. r.Powers = UInt64.Parse(d.Data["Powers"]);
  717. r.RoleID = d.RoleID;
  718. r.Title = d.Data["Title"];
  719. roles.Add(r);
  720. }
  721. return roles;
  722. }
  723. protected List<ExtendedGroupRoleMembersData> _GetGroupRoleMembers(UUID GroupID, bool isInGroup)
  724. {
  725. List<ExtendedGroupRoleMembersData> rmembers = new List<ExtendedGroupRoleMembersData>();
  726. RoleData[] rdata = new RoleData[0];
  727. if (!isInGroup)
  728. {
  729. rdata = m_Database.RetrieveRoles(GroupID);
  730. if (rdata == null || (rdata != null && rdata.Length == 0))
  731. return rmembers;
  732. }
  733. List<RoleData> rlist = new List<RoleData>(rdata);
  734. if (!isInGroup)
  735. rlist = rlist.FindAll(r => (UInt64.Parse(r.Data["Powers"]) & (ulong)GroupPowers.MemberVisible) != 0);
  736. RoleMembershipData[] data = m_Database.RetrieveRolesMembers(GroupID);
  737. if (data == null || (data != null && data.Length == 0))
  738. return rmembers;
  739. foreach (RoleMembershipData d in data)
  740. {
  741. if (!isInGroup)
  742. {
  743. RoleData rd = rlist.Find(_r => _r.RoleID == d.RoleID); // visible role
  744. if (rd == null)
  745. continue;
  746. }
  747. ExtendedGroupRoleMembersData r = new ExtendedGroupRoleMembersData();
  748. r.MemberID = d.PrincipalID;
  749. r.RoleID = d.RoleID;
  750. rmembers.Add(r);
  751. }
  752. return rmembers;
  753. }
  754. protected bool _AddNotice(UUID groupID, UUID noticeID, string fromName, string subject, string message,
  755. bool hasAttachment, byte attType, string attName, UUID attItemID, string attOwnerID)
  756. {
  757. NoticeData data = new NoticeData();
  758. data.GroupID = groupID;
  759. data.NoticeID = noticeID;
  760. data.Data = new Dictionary<string, string>();
  761. data.Data["FromName"] = fromName;
  762. data.Data["Subject"] = subject;
  763. data.Data["Message"] = message;
  764. data.Data["HasAttachment"] = hasAttachment ? "1" : "0";
  765. if (hasAttachment)
  766. {
  767. data.Data["AttachmentType"] = attType.ToString();
  768. data.Data["AttachmentName"] = attName;
  769. data.Data["AttachmentItemID"] = attItemID.ToString();
  770. data.Data["AttachmentOwnerID"] = attOwnerID;
  771. }
  772. data.Data["TMStamp"] = ((uint)Util.UnixTimeSinceEpoch()).ToString();
  773. return m_Database.StoreNotice(data);
  774. }
  775. #endregion
  776. #region structure translations
  777. ExtendedGroupRecord _GroupDataToRecord(GroupData data)
  778. {
  779. if (data == null)
  780. return null;
  781. ExtendedGroupRecord rec = new ExtendedGroupRecord();
  782. rec.AllowPublish = data.Data["AllowPublish"] == "1" ? true : false;
  783. rec.Charter = data.Data["Charter"];
  784. rec.FounderID = new UUID(data.Data["FounderID"]);
  785. rec.GroupID = data.GroupID;
  786. rec.GroupName = data.Data["Name"];
  787. rec.GroupPicture = new UUID(data.Data["InsigniaID"]);
  788. rec.MaturePublish = data.Data["MaturePublish"] == "1" ? true : false;
  789. rec.MembershipFee = Int32.Parse(data.Data["MembershipFee"]);
  790. rec.OpenEnrollment = data.Data["OpenEnrollment"] == "1" ? true : false;
  791. rec.OwnerRoleID = new UUID(data.Data["OwnerRoleID"]);
  792. rec.ShowInList = data.Data["ShowInList"] == "1" ? true : false;
  793. rec.ServiceLocation = data.Data["Location"];
  794. rec.MemberCount = m_Database.MemberCount(data.GroupID);
  795. rec.RoleCount = m_Database.RoleCount(data.GroupID);
  796. return rec;
  797. }
  798. GroupNoticeInfo _NoticeDataToInfo(NoticeData data)
  799. {
  800. GroupNoticeInfo notice = new GroupNoticeInfo();
  801. notice.GroupID = data.GroupID;
  802. notice.Message = data.Data["Message"];
  803. notice.noticeData = _NoticeDataToData(data);
  804. return notice;
  805. }
  806. ExtendedGroupNoticeData _NoticeDataToData(NoticeData data)
  807. {
  808. ExtendedGroupNoticeData notice = new ExtendedGroupNoticeData();
  809. notice.FromName = data.Data["FromName"];
  810. notice.NoticeID = data.NoticeID;
  811. notice.Subject = data.Data["Subject"];
  812. notice.Timestamp = uint.Parse((string)data.Data["TMStamp"]);
  813. notice.HasAttachment = data.Data["HasAttachment"] == "1" ? true : false;
  814. if (notice.HasAttachment)
  815. {
  816. notice.AttachmentName = data.Data["AttachmentName"];
  817. notice.AttachmentItemID = new UUID(data.Data["AttachmentItemID"].ToString());
  818. notice.AttachmentType = byte.Parse(data.Data["AttachmentType"].ToString());
  819. notice.AttachmentOwnerID = data.Data["AttachmentOwnerID"].ToString();
  820. }
  821. return notice;
  822. }
  823. #endregion
  824. #region permissions
  825. private bool HasPower(string agentID, UUID groupID, GroupPowers power)
  826. {
  827. RoleMembershipData[] rmembership = m_Database.RetrieveMemberRoles(groupID, agentID);
  828. if (rmembership == null || (rmembership != null && rmembership.Length == 0))
  829. return false;
  830. foreach (RoleMembershipData rdata in rmembership)
  831. {
  832. RoleData role = m_Database.RetrieveRole(groupID, rdata.RoleID);
  833. if ( (UInt64.Parse(role.Data["Powers"]) & (ulong)power) != 0 )
  834. return true;
  835. }
  836. return false;
  837. }
  838. private bool IsOwner(string agentID, UUID groupID)
  839. {
  840. GroupData group = m_Database.RetrieveGroup(groupID);
  841. if (group == null)
  842. return false;
  843. RoleMembershipData rmembership = m_Database.RetrieveRoleMember(groupID, new UUID(group.Data["OwnerRoleID"]), agentID);
  844. if (rmembership == null)
  845. return false;
  846. return true;
  847. }
  848. #endregion
  849. }
  850. }