GroupsService.cs 43 KB

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