UserAccountServicesConnector.cs 13 KB

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