UserAccountServiceConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Server.Base;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class UserAccountServicesConnector : IUserAccountService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. private string m_ServerURI = String.Empty;
  46. public UserAccountServicesConnector()
  47. {
  48. }
  49. public UserAccountServicesConnector(string serverURI)
  50. {
  51. m_ServerURI = serverURI.TrimEnd('/');
  52. }
  53. public UserAccountServicesConnector(IConfigSource source)
  54. {
  55. Initialise(source);
  56. }
  57. public virtual void Initialise(IConfigSource source)
  58. {
  59. IConfig assetConfig = source.Configs["UserAccountService"];
  60. if (assetConfig == null)
  61. {
  62. m_log.Error("[ACCOUNT CONNECTOR]: UserAccountService missing from OpenSim.ini");
  63. throw new Exception("User account connector init error");
  64. }
  65. string serviceURI = assetConfig.GetString("UserAccountServerURI",
  66. String.Empty);
  67. if (serviceURI == String.Empty)
  68. {
  69. m_log.Error("[ACCOUNT CONNECTOR]: No Server URI named in section UserAccountService");
  70. throw new Exception("User account connector init error");
  71. }
  72. m_ServerURI = serviceURI;
  73. }
  74. public virtual UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)
  75. {
  76. Dictionary<string, object> sendData = new Dictionary<string, object>();
  77. //sendData["SCOPEID"] = scopeID.ToString();
  78. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  79. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  80. sendData["METHOD"] = "getaccount";
  81. sendData["ScopeID"] = scopeID;
  82. sendData["FirstName"] = firstName.ToString();
  83. sendData["LastName"] = lastName.ToString();
  84. return SendAndGetReply(sendData);
  85. }
  86. public virtual UserAccount GetUserAccount(UUID scopeID, string email)
  87. {
  88. Dictionary<string, object> sendData = new Dictionary<string, object>();
  89. //sendData["SCOPEID"] = scopeID.ToString();
  90. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  91. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  92. sendData["METHOD"] = "getaccount";
  93. sendData["ScopeID"] = scopeID;
  94. sendData["Email"] = email;
  95. return SendAndGetReply(sendData);
  96. }
  97. public virtual UserAccount GetUserAccount(UUID scopeID, UUID userID)
  98. {
  99. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccount {0}", userID);
  100. Dictionary<string, object> sendData = new Dictionary<string, object>();
  101. //sendData["SCOPEID"] = scopeID.ToString();
  102. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  103. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  104. sendData["METHOD"] = "getaccount";
  105. sendData["ScopeID"] = scopeID;
  106. sendData["UserID"] = userID.ToString();
  107. return SendAndGetReply(sendData);
  108. }
  109. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  110. {
  111. Dictionary<string, object> sendData = new Dictionary<string, object>();
  112. //sendData["SCOPEID"] = scopeID.ToString();
  113. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  114. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  115. sendData["METHOD"] = "getaccounts";
  116. sendData["ScopeID"] = scopeID.ToString();
  117. sendData["query"] = query;
  118. string reply = string.Empty;
  119. string reqString = ServerUtils.BuildQueryString(sendData);
  120. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  121. try
  122. {
  123. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  124. m_ServerURI + "/accounts",
  125. reqString);
  126. if (reply == null || (reply != null && reply == string.Empty))
  127. {
  128. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received null or empty reply");
  129. return null;
  130. }
  131. }
  132. catch (Exception e)
  133. {
  134. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting accounts server: {0}", e.Message);
  135. }
  136. List<UserAccount> accounts = new List<UserAccount>();
  137. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  138. if (replyData != null)
  139. {
  140. if (replyData.ContainsKey("result") && replyData.ContainsKey("result").ToString() == "null")
  141. {
  142. return accounts;
  143. }
  144. Dictionary<string, object>.ValueCollection accountList = replyData.Values;
  145. //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  146. foreach (object acc in accountList)
  147. {
  148. if (acc is Dictionary<string, object>)
  149. {
  150. UserAccount pinfo = new UserAccount((Dictionary<string, object>)acc);
  151. accounts.Add(pinfo);
  152. }
  153. else
  154. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received invalid response type {0}",
  155. acc.GetType());
  156. }
  157. }
  158. else
  159. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccounts received null response");
  160. return accounts;
  161. }
  162. public virtual bool StoreUserAccount(UserAccount data)
  163. {
  164. Dictionary<string, object> sendData = new Dictionary<string, object>();
  165. //sendData["SCOPEID"] = scopeID.ToString();
  166. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  167. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  168. sendData["METHOD"] = "setaccount";
  169. Dictionary<string, object> structData = data.ToKeyValuePairs();
  170. foreach (KeyValuePair<string,object> kvp in structData)
  171. sendData[kvp.Key] = kvp.Value.ToString();
  172. return SendAndGetBoolReply(sendData);
  173. }
  174. private UserAccount SendAndGetReply(Dictionary<string, object> sendData)
  175. {
  176. string reply = string.Empty;
  177. string reqString = ServerUtils.BuildQueryString(sendData);
  178. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  179. try
  180. {
  181. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  182. m_ServerURI + "/accounts",
  183. reqString);
  184. if (reply == null || (reply != null && reply == string.Empty))
  185. {
  186. m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccount received null or empty reply");
  187. return null;
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user account server: {0}", e.Message);
  193. }
  194. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  195. UserAccount account = null;
  196. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  197. {
  198. if (replyData["result"] is Dictionary<string, object>)
  199. {
  200. account = new UserAccount((Dictionary<string, object>)replyData["result"]);
  201. }
  202. }
  203. return account;
  204. }
  205. private bool SendAndGetBoolReply(Dictionary<string, object> sendData)
  206. {
  207. string reqString = ServerUtils.BuildQueryString(sendData);
  208. // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
  209. try
  210. {
  211. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  212. m_ServerURI + "/accounts",
  213. reqString);
  214. if (reply != string.Empty)
  215. {
  216. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  217. if (replyData.ContainsKey("result"))
  218. {
  219. if (replyData["result"].ToString().ToLower() == "success")
  220. return true;
  221. else
  222. return false;
  223. }
  224. else
  225. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount reply data does not contain result field");
  226. }
  227. else
  228. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount received empty reply");
  229. }
  230. catch (Exception e)
  231. {
  232. m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Exception when contacting user account server: {0}", e.Message);
  233. }
  234. return false;
  235. }
  236. }
  237. }