GroupsService.cs 43 KB

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