1
0

GroupsService.cs 42 KB

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