UserAccountServicesConnector.cs 13 KB

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