GroupsService.cs 43 KB

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