1
0

GroupsService.cs 44 KB

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