PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Aurora/Services/DataService/Connectors/Local/LocalProfileConnector.cs

https://bitbucket.org/VirtualReality/software-testing
C# | 339 lines | 230 code | 54 blank | 55 comment | 58 complexity | 9d15a8c5022198eb6a7c32e14b84951b MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.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 Aurora-Sim 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.Collections.Generic;
  28. using Aurora.Framework;
  29. using Aurora.Framework.DatabaseInterfaces;
  30. using Aurora.Framework.Modules;
  31. using Aurora.Framework.Services;
  32. using Aurora.Framework.Services.ClassHelpers.Profile;
  33. using Aurora.Framework.Utilities;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. namespace Aurora.Services.DataService
  38. {
  39. public class LocalProfileConnector : ConnectorBase, IProfileConnector
  40. {
  41. //We can use a cache because we are the only place that profiles will be served from
  42. private readonly Dictionary<UUID, IUserProfileInfo> UserProfilesCache = new Dictionary<UUID, IUserProfileInfo>();
  43. private IGenericData GD;
  44. #region IProfileConnector Members
  45. public void Initialize(IGenericData GenericData, IConfigSource source, IRegistryCore simBase,
  46. string defaultConnectionString)
  47. {
  48. GD = GenericData;
  49. if (source.Configs[Name] != null)
  50. defaultConnectionString = source.Configs[Name].GetString("ConnectionString", defaultConnectionString);
  51. if (GD != null)
  52. GD.ConnectToDatabase(defaultConnectionString, "Agent",
  53. source.Configs["AuroraConnectors"].GetBoolean("ValidateTables", true));
  54. Framework.Utilities.DataManager.RegisterPlugin(Name + "Local", this);
  55. if (source.Configs["AuroraConnectors"].GetString("ProfileConnector", "LocalConnector") == "LocalConnector")
  56. {
  57. Framework.Utilities.DataManager.RegisterPlugin(this);
  58. }
  59. Init(simBase, Name);
  60. }
  61. public string Name
  62. {
  63. get { return "IProfileConnector"; }
  64. }
  65. /// <summary>
  66. /// Get a user's profile
  67. /// </summary>
  68. /// <param name="agentID"></param>
  69. /// <returns></returns>
  70. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  71. public IUserProfileInfo GetUserProfile(UUID agentID)
  72. {
  73. object remoteValue = DoRemote(agentID);
  74. if (remoteValue != null || m_doRemoteOnly)
  75. return (IUserProfileInfo) remoteValue;
  76. IUserProfileInfo UserProfile = new IUserProfileInfo();
  77. //Try from the user profile first before getting from the DB
  78. if (UserProfilesCache.TryGetValue(agentID, out UserProfile))
  79. return UserProfile;
  80. QueryFilter filter = new QueryFilter();
  81. filter.andFilters["ID"] = agentID;
  82. filter.andFilters["`Key`"] = "LLProfile";
  83. List<string> query = null;
  84. //Grab it from the almost generic interface
  85. query = GD.Query(new[] {"Value"}, "userdata", filter, null, null, null);
  86. if (query == null || query.Count == 0)
  87. return null;
  88. //Pull out the OSDmap
  89. OSDMap profile = (OSDMap) OSDParser.DeserializeLLSDXml(query[0]);
  90. UserProfile = new IUserProfileInfo();
  91. UserProfile.FromOSD(profile);
  92. //Add to the cache
  93. UserProfilesCache[agentID] = UserProfile;
  94. return UserProfile;
  95. }
  96. /// <summary>
  97. /// Update a user's profile (Note: this does not work if the user does not have a profile)
  98. /// </summary>
  99. /// <param name="Profile"></param>
  100. /// <returns></returns>
  101. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  102. public bool UpdateUserProfile(IUserProfileInfo Profile)
  103. {
  104. object remoteValue = DoRemote(Profile);
  105. if (remoteValue != null || m_doRemoteOnly)
  106. return remoteValue != null && (bool) remoteValue;
  107. IUserProfileInfo previousProfile = GetUserProfile(Profile.PrincipalID);
  108. //Make sure the previous one exists
  109. if (previousProfile == null)
  110. return false;
  111. //Now fix values that the sim cannot change
  112. Profile.Partner = previousProfile.Partner;
  113. Profile.CustomType = previousProfile.CustomType;
  114. Profile.MembershipGroup = previousProfile.MembershipGroup;
  115. Profile.Created = previousProfile.Created;
  116. Dictionary<string, object> values = new Dictionary<string, object>(1);
  117. values["Value"] = OSDParser.SerializeLLSDXmlString(Profile.ToOSD());
  118. QueryFilter filter = new QueryFilter();
  119. filter.andFilters["ID"] = Profile.PrincipalID.ToString();
  120. filter.andFilters["`Key`"] = "LLProfile";
  121. //Update cache
  122. UserProfilesCache[Profile.PrincipalID] = Profile;
  123. return GD.Update("userdata", values, null, filter, null, null);
  124. }
  125. /// <summary>
  126. /// Create a new profile for a user
  127. /// </summary>
  128. /// <param name="AgentID"></param>
  129. //[CanBeReflected(ThreatLevel = ThreatLevel.Full)]
  130. public void CreateNewProfile(UUID AgentID)
  131. {
  132. /*object remoteValue = DoRemote(AgentID);
  133. if (remoteValue != null || m_doRemoteOnly)
  134. return;*/
  135. List<object> values = new List<object> {AgentID.ToString(), "LLProfile"};
  136. //Create a new basic profile for them
  137. IUserProfileInfo profile = new IUserProfileInfo {PrincipalID = AgentID};
  138. values.Add(OSDParser.SerializeLLSDXmlString(profile.ToOSD())); //Value which is a default Profile
  139. GD.Insert("userdata", values.ToArray());
  140. }
  141. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  142. public bool AddClassified(Classified classified)
  143. {
  144. object remoteValue = DoRemote(classified);
  145. if (remoteValue != null || m_doRemoteOnly)
  146. return remoteValue != null && (bool) remoteValue;
  147. if (GetUserProfile(classified.CreatorUUID) == null)
  148. return false;
  149. string keywords = classified.Description;
  150. if (keywords.Length > 512)
  151. keywords = keywords.Substring(keywords.Length - 512, 512);
  152. //It might be updating, delete the old
  153. QueryFilter filter = new QueryFilter();
  154. filter.andFilters["ClassifiedUUID"] = classified.ClassifiedUUID;
  155. GD.Delete("userclassifieds", filter);
  156. List<object> values = new List<object>
  157. {
  158. classified.Name,
  159. classified.Category,
  160. classified.SimName,
  161. classified.CreatorUUID,
  162. classified.ScopeID,
  163. classified.ClassifiedUUID,
  164. OSDParser.SerializeJsonString(classified.ToOSD()),
  165. classified.PriceForListing,
  166. keywords
  167. };
  168. return GD.Insert("userclassifieds", values.ToArray());
  169. }
  170. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  171. public List<Classified> GetClassifieds(UUID ownerID)
  172. {
  173. object remoteValue = DoRemote(ownerID);
  174. if (remoteValue != null || m_doRemoteOnly)
  175. return (List<Classified>) remoteValue;
  176. QueryFilter filter = new QueryFilter();
  177. filter.andFilters["OwnerUUID"] = ownerID;
  178. List<string> query = GD.Query(new[] {"*"}, "userclassifieds", filter, null, null, null);
  179. List<Classified> classifieds = new List<Classified>();
  180. for (int i = 0; i < query.Count; i += 9)
  181. {
  182. Classified classified = new Classified();
  183. classified.FromOSD((OSDMap) OSDParser.DeserializeJson(query[i + 6]));
  184. classifieds.Add(classified);
  185. }
  186. return classifieds;
  187. }
  188. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  189. public Classified GetClassified(UUID queryClassifiedID)
  190. {
  191. object remoteValue = DoRemote(queryClassifiedID);
  192. if (remoteValue != null || m_doRemoteOnly)
  193. return (Classified) remoteValue;
  194. QueryFilter filter = new QueryFilter();
  195. filter.andFilters["ClassifiedUUID"] = queryClassifiedID;
  196. List<string> query = GD.Query(new[] {"*"}, "userclassifieds", filter, null, null, null);
  197. if (query.Count < 9)
  198. {
  199. return null;
  200. }
  201. Classified classified = new Classified();
  202. classified.FromOSD((OSDMap) OSDParser.DeserializeJson(query[6]));
  203. return classified;
  204. }
  205. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  206. public void RemoveClassified(UUID queryClassifiedID)
  207. {
  208. object remoteValue = DoRemote(queryClassifiedID);
  209. if (remoteValue != null || m_doRemoteOnly)
  210. return;
  211. QueryFilter filter = new QueryFilter();
  212. filter.andFilters["ClassifiedUUID"] = queryClassifiedID;
  213. GD.Delete("userclassifieds", filter);
  214. }
  215. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  216. public bool AddPick(ProfilePickInfo pick)
  217. {
  218. object remoteValue = DoRemote(pick);
  219. if (remoteValue != null || m_doRemoteOnly)
  220. return remoteValue != null && (bool) remoteValue;
  221. if (GetUserProfile(pick.CreatorUUID) == null)
  222. return false;
  223. //It might be updating, delete the old
  224. QueryFilter filter = new QueryFilter();
  225. filter.andFilters["PickUUID"] = pick.PickUUID;
  226. GD.Delete("userpicks", filter);
  227. List<object> values = new List<object>
  228. {
  229. pick.Name,
  230. pick.SimName,
  231. pick.CreatorUUID,
  232. pick.PickUUID,
  233. OSDParser.SerializeJsonString(pick.ToOSD())
  234. };
  235. return GD.Insert("userpicks", values.ToArray());
  236. }
  237. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  238. public ProfilePickInfo GetPick(UUID queryPickID)
  239. {
  240. object remoteValue = DoRemote(queryPickID);
  241. if (remoteValue != null || m_doRemoteOnly)
  242. return (ProfilePickInfo) remoteValue;
  243. QueryFilter filter = new QueryFilter();
  244. filter.andFilters["PickUUID"] = queryPickID;
  245. List<string> query = GD.Query(new[] {"*"}, "userpicks", filter, null, null, null);
  246. if (query.Count < 5)
  247. return null;
  248. ProfilePickInfo pick = new ProfilePickInfo();
  249. pick.FromOSD((OSDMap) OSDParser.DeserializeJson(query[4]));
  250. return pick;
  251. }
  252. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  253. public List<ProfilePickInfo> GetPicks(UUID ownerID)
  254. {
  255. object remoteValue = DoRemote(ownerID);
  256. if (remoteValue != null || m_doRemoteOnly)
  257. return (List<ProfilePickInfo>) remoteValue;
  258. QueryFilter filter = new QueryFilter();
  259. filter.andFilters["OwnerUUID"] = ownerID;
  260. List<string> query = GD.Query(new[] {"*"}, "userpicks", filter, null, null, null);
  261. List<ProfilePickInfo> picks = new List<ProfilePickInfo>();
  262. for (int i = 0; i < query.Count; i += 5)
  263. {
  264. ProfilePickInfo pick = new ProfilePickInfo();
  265. pick.FromOSD((OSDMap) OSDParser.DeserializeJson(query[i + 4]));
  266. picks.Add(pick);
  267. }
  268. return picks;
  269. }
  270. [CanBeReflected(ThreatLevel = ThreatLevel.Low)]
  271. public void RemovePick(UUID queryPickID)
  272. {
  273. object remoteValue = DoRemote(queryPickID);
  274. if (remoteValue != null || m_doRemoteOnly)
  275. return;
  276. QueryFilter filter = new QueryFilter();
  277. filter.andFilters["PickUUID"] = queryPickID;
  278. GD.Delete("userpicks", filter);
  279. }
  280. #endregion
  281. public void Dispose()
  282. {
  283. }
  284. }
  285. }