UserAccountServerPostHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 Nini.Config;
  28. using log4net;
  29. using System;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Net;
  33. using System.Text;
  34. using System.Text.RegularExpressions;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using System.Collections.Generic;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using OpenSim.Services.UserAccountService;
  41. using OpenSim.Framework;
  42. using OpenSim.Framework.Servers.HttpServer;
  43. using OpenMetaverse;
  44. namespace OpenSim.Server.Handlers.UserAccounts
  45. {
  46. public class UserAccountServerPostHandler : BaseStreamHandler
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private IUserAccountService m_UserAccountService;
  50. private bool m_AllowCreateUser = false;
  51. private bool m_AllowSetAccount = false;
  52. public UserAccountServerPostHandler(IUserAccountService service)
  53. : this(service, null) {}
  54. public UserAccountServerPostHandler(IUserAccountService service, IConfig config) :
  55. base("POST", "/accounts")
  56. {
  57. m_UserAccountService = service;
  58. if (config != null)
  59. {
  60. m_AllowCreateUser = config.GetBoolean("AllowCreateUser", m_AllowCreateUser);
  61. m_AllowSetAccount = config.GetBoolean("AllowSetAccount", m_AllowSetAccount);
  62. }
  63. }
  64. protected override byte[] ProcessRequest(string path, Stream requestData,
  65. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  66. {
  67. StreamReader sr = new StreamReader(requestData);
  68. string body = sr.ReadToEnd();
  69. sr.Close();
  70. body = body.Trim();
  71. // We need to check the authorization header
  72. //httpRequest.Headers["authorization"] ...
  73. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  74. string method = string.Empty;
  75. try
  76. {
  77. Dictionary<string, object> request =
  78. ServerUtils.ParseQueryString(body);
  79. if (!request.ContainsKey("METHOD"))
  80. return FailureResult();
  81. method = request["METHOD"].ToString();
  82. switch (method)
  83. {
  84. case "createuser":
  85. if (m_AllowCreateUser)
  86. return CreateUser(request);
  87. else
  88. break;
  89. case "getaccount":
  90. return GetAccount(request);
  91. case "getaccounts":
  92. return GetAccounts(request);
  93. case "setaccount":
  94. if (m_AllowSetAccount)
  95. return StoreAccount(request);
  96. else
  97. break;
  98. }
  99. m_log.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method);
  100. }
  101. catch (Exception e)
  102. {
  103. m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e);
  104. }
  105. return FailureResult();
  106. }
  107. byte[] GetAccount(Dictionary<string, object> request)
  108. {
  109. UserAccount account = null;
  110. UUID scopeID = UUID.Zero;
  111. Dictionary<string, object> result = new Dictionary<string, object>();
  112. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  113. {
  114. result["result"] = "null";
  115. return ResultToBytes(result);
  116. }
  117. if (request.ContainsKey("UserID") && request["UserID"] != null)
  118. {
  119. UUID userID;
  120. if (UUID.TryParse(request["UserID"].ToString(), out userID))
  121. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  122. }
  123. else if (request.ContainsKey("PrincipalID") && request["PrincipalID"] != null)
  124. {
  125. UUID userID;
  126. if (UUID.TryParse(request["PrincipalID"].ToString(), out userID))
  127. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  128. }
  129. else if (request.ContainsKey("Email") && request["Email"] != null)
  130. {
  131. account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString());
  132. }
  133. else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") &&
  134. request["FirstName"] != null && request["LastName"] != null)
  135. {
  136. account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString());
  137. }
  138. if (account == null)
  139. {
  140. result["result"] = "null";
  141. }
  142. else
  143. {
  144. result["result"] = account.ToKeyValuePairs();
  145. }
  146. return ResultToBytes(result);
  147. }
  148. byte[] GetAccounts(Dictionary<string, object> request)
  149. {
  150. if (!request.ContainsKey("query"))
  151. return FailureResult();
  152. UUID scopeID = UUID.Zero;
  153. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  154. return FailureResult();
  155. string query = request["query"].ToString();
  156. List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query);
  157. Dictionary<string, object> result = new Dictionary<string, object>();
  158. if ((accounts == null) || ((accounts != null) && (accounts.Count == 0)))
  159. {
  160. result["result"] = "null";
  161. }
  162. else
  163. {
  164. int i = 0;
  165. foreach (UserAccount acc in accounts)
  166. {
  167. Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs();
  168. result["account" + i] = rinfoDict;
  169. i++;
  170. }
  171. }
  172. string xmlString = ServerUtils.BuildXmlResponse(result);
  173. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  174. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  175. }
  176. byte[] StoreAccount(Dictionary<string, object> request)
  177. {
  178. UUID principalID = UUID.Zero;
  179. if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID))
  180. return FailureResult();
  181. UUID scopeID = UUID.Zero;
  182. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  183. return FailureResult();
  184. UserAccount existingAccount = m_UserAccountService.GetUserAccount(scopeID, principalID);
  185. if (existingAccount == null)
  186. return FailureResult();
  187. Dictionary<string, object> result = new Dictionary<string, object>();
  188. if (request.ContainsKey("FirstName"))
  189. existingAccount.FirstName = request["FirstName"].ToString();
  190. if (request.ContainsKey("LastName"))
  191. existingAccount.LastName = request["LastName"].ToString();
  192. if (request.ContainsKey("Email"))
  193. existingAccount.Email = request["Email"].ToString();
  194. int created = 0;
  195. if (request.ContainsKey("Created") && int.TryParse(request["Created"].ToString(), out created))
  196. existingAccount.Created = created;
  197. int userLevel = 0;
  198. if (request.ContainsKey("UserLevel") && int.TryParse(request["UserLevel"].ToString(), out userLevel))
  199. existingAccount.UserLevel = userLevel;
  200. int userFlags = 0;
  201. if (request.ContainsKey("UserFlags") && int.TryParse(request["UserFlags"].ToString(), out userFlags))
  202. existingAccount.UserFlags = userFlags;
  203. if (request.ContainsKey("UserTitle"))
  204. existingAccount.UserTitle = request["UserTitle"].ToString();
  205. if (!m_UserAccountService.StoreUserAccount(existingAccount))
  206. {
  207. m_log.ErrorFormat(
  208. "[USER ACCOUNT SERVER POST HANDLER]: Account store failed for account {0} {1} {2}",
  209. existingAccount.FirstName, existingAccount.LastName, existingAccount.PrincipalID);
  210. return FailureResult();
  211. }
  212. result["result"] = existingAccount.ToKeyValuePairs();
  213. return ResultToBytes(result);
  214. }
  215. byte[] CreateUser(Dictionary<string, object> request)
  216. {
  217. if (!
  218. request.ContainsKey("FirstName")
  219. && request.ContainsKey("LastName")
  220. && request.ContainsKey("Password"))
  221. return FailureResult();
  222. Dictionary<string, object> result = new Dictionary<string, object>();
  223. UUID scopeID = UUID.Zero;
  224. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  225. return FailureResult();
  226. UUID principalID = UUID.Random();
  227. if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID))
  228. return FailureResult();
  229. string firstName = request["FirstName"].ToString();
  230. string lastName = request["LastName"].ToString();
  231. string password = request["Password"].ToString();
  232. string email = "";
  233. if (request.ContainsKey("Email"))
  234. email = request["Email"].ToString();
  235. UserAccount createdUserAccount = null;
  236. if (m_UserAccountService is UserAccountService)
  237. createdUserAccount
  238. = ((UserAccountService)m_UserAccountService).CreateUser(
  239. scopeID, principalID, firstName, lastName, password, email);
  240. if (createdUserAccount == null)
  241. return FailureResult();
  242. result["result"] = createdUserAccount.ToKeyValuePairs();
  243. return ResultToBytes(result);
  244. }
  245. private byte[] SuccessResult()
  246. {
  247. XmlDocument doc = new XmlDocument();
  248. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  249. "", "");
  250. doc.AppendChild(xmlnode);
  251. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  252. "");
  253. doc.AppendChild(rootElement);
  254. XmlElement result = doc.CreateElement("", "result", "");
  255. result.AppendChild(doc.CreateTextNode("Success"));
  256. rootElement.AppendChild(result);
  257. return DocToBytes(doc);
  258. }
  259. private byte[] FailureResult()
  260. {
  261. XmlDocument doc = new XmlDocument();
  262. XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
  263. "", "");
  264. doc.AppendChild(xmlnode);
  265. XmlElement rootElement = doc.CreateElement("", "ServerResponse",
  266. "");
  267. doc.AppendChild(rootElement);
  268. XmlElement result = doc.CreateElement("", "result", "");
  269. result.AppendChild(doc.CreateTextNode("Failure"));
  270. rootElement.AppendChild(result);
  271. return DocToBytes(doc);
  272. }
  273. private byte[] DocToBytes(XmlDocument doc)
  274. {
  275. MemoryStream ms = new MemoryStream();
  276. XmlTextWriter xw = new XmlTextWriter(ms, null);
  277. xw.Formatting = Formatting.Indented;
  278. doc.WriteTo(xw);
  279. xw.Flush();
  280. return ms.ToArray();
  281. }
  282. private byte[] ResultToBytes(Dictionary<string, object> result)
  283. {
  284. string xmlString = ServerUtils.BuildXmlResponse(result);
  285. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  286. }
  287. }
  288. }