UserAccountServerPostHandler.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 OpenSim.Framework.ServiceAuth;
  44. using OpenMetaverse;
  45. namespace OpenSim.Server.Handlers.UserAccounts
  46. {
  47. public class UserAccountServerPostHandler : BaseStreamHandler
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IUserAccountService m_UserAccountService;
  51. private bool m_AllowCreateUser = false;
  52. private bool m_AllowSetAccount = false;
  53. public UserAccountServerPostHandler(IUserAccountService service)
  54. : this(service, null, null) {}
  55. public UserAccountServerPostHandler(IUserAccountService service, IConfig config, IServiceAuth auth) :
  56. base("POST", "/accounts", auth)
  57. {
  58. m_UserAccountService = service;
  59. if (config != null)
  60. {
  61. m_AllowCreateUser = config.GetBoolean("AllowCreateUser", m_AllowCreateUser);
  62. m_AllowSetAccount = config.GetBoolean("AllowSetAccount", m_AllowSetAccount);
  63. }
  64. }
  65. protected override byte[] ProcessRequest(string path, Stream requestData,
  66. IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
  67. {
  68. StreamReader sr = new StreamReader(requestData);
  69. string body = sr.ReadToEnd();
  70. sr.Close();
  71. body = body.Trim();
  72. // We need to check the authorization header
  73. //httpRequest.Headers["authorization"] ...
  74. //m_log.DebugFormat("[XXX]: query String: {0}", body);
  75. string method = string.Empty;
  76. try
  77. {
  78. Dictionary<string, object> request =
  79. ServerUtils.ParseQueryString(body);
  80. if (!request.ContainsKey("METHOD"))
  81. return FailureResult();
  82. method = request["METHOD"].ToString();
  83. switch (method)
  84. {
  85. case "createuser":
  86. if (m_AllowCreateUser)
  87. return CreateUser(request);
  88. else
  89. break;
  90. case "getaccount":
  91. return GetAccount(request);
  92. case "getaccounts":
  93. return GetAccounts(request);
  94. case "setaccount":
  95. if (m_AllowSetAccount)
  96. return StoreAccount(request);
  97. else
  98. break;
  99. }
  100. m_log.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method);
  101. }
  102. catch (Exception e)
  103. {
  104. m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e);
  105. }
  106. return FailureResult();
  107. }
  108. byte[] GetAccount(Dictionary<string, object> request)
  109. {
  110. UserAccount account = null;
  111. UUID scopeID = UUID.Zero;
  112. Dictionary<string, object> result = new Dictionary<string, object>();
  113. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  114. {
  115. result["result"] = "null";
  116. return ResultToBytes(result);
  117. }
  118. if (request.ContainsKey("UserID") && request["UserID"] != null)
  119. {
  120. UUID userID;
  121. if (UUID.TryParse(request["UserID"].ToString(), out userID))
  122. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  123. }
  124. else if (request.ContainsKey("PrincipalID") && request["PrincipalID"] != null)
  125. {
  126. UUID userID;
  127. if (UUID.TryParse(request["PrincipalID"].ToString(), out userID))
  128. account = m_UserAccountService.GetUserAccount(scopeID, userID);
  129. }
  130. else if (request.ContainsKey("Email") && request["Email"] != null)
  131. {
  132. account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString());
  133. }
  134. else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") &&
  135. request["FirstName"] != null && request["LastName"] != null)
  136. {
  137. account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString());
  138. }
  139. if (account == null)
  140. {
  141. result["result"] = "null";
  142. }
  143. else
  144. {
  145. result["result"] = account.ToKeyValuePairs();
  146. }
  147. return ResultToBytes(result);
  148. }
  149. byte[] GetAccounts(Dictionary<string, object> request)
  150. {
  151. if (!request.ContainsKey("query"))
  152. return FailureResult();
  153. UUID scopeID = UUID.Zero;
  154. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  155. return FailureResult();
  156. string query = request["query"].ToString();
  157. List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query);
  158. Dictionary<string, object> result = new Dictionary<string, object>();
  159. if ((accounts == null) || ((accounts != null) && (accounts.Count == 0)))
  160. {
  161. result["result"] = "null";
  162. }
  163. else
  164. {
  165. int i = 0;
  166. foreach (UserAccount acc in accounts)
  167. {
  168. Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs();
  169. result["account" + i] = rinfoDict;
  170. i++;
  171. }
  172. }
  173. string xmlString = ServerUtils.BuildXmlResponse(result);
  174. //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString);
  175. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  176. }
  177. byte[] StoreAccount(Dictionary<string, object> request)
  178. {
  179. UUID principalID = UUID.Zero;
  180. if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID))
  181. return FailureResult();
  182. UUID scopeID = UUID.Zero;
  183. if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID))
  184. return FailureResult();
  185. UserAccount existingAccount = m_UserAccountService.GetUserAccount(scopeID, principalID);
  186. if (existingAccount == null)
  187. return FailureResult();
  188. Dictionary<string, object> result = new Dictionary<string, object>();
  189. if (request.ContainsKey("FirstName"))
  190. existingAccount.FirstName = request["FirstName"].ToString();
  191. if (request.ContainsKey("LastName"))
  192. existingAccount.LastName = request["LastName"].ToString();
  193. if (request.ContainsKey("Email"))
  194. existingAccount.Email = request["Email"].ToString();
  195. int created = 0;
  196. if (request.ContainsKey("Created") && int.TryParse(request["Created"].ToString(), out created))
  197. existingAccount.Created = created;
  198. int userLevel = 0;
  199. if (request.ContainsKey("UserLevel") && int.TryParse(request["UserLevel"].ToString(), out userLevel))
  200. existingAccount.UserLevel = userLevel;
  201. int userFlags = 0;
  202. if (request.ContainsKey("UserFlags") && int.TryParse(request["UserFlags"].ToString(), out userFlags))
  203. existingAccount.UserFlags = userFlags;
  204. if (request.ContainsKey("UserTitle"))
  205. existingAccount.UserTitle = request["UserTitle"].ToString();
  206. if (!m_UserAccountService.StoreUserAccount(existingAccount))
  207. {
  208. m_log.ErrorFormat(
  209. "[USER ACCOUNT SERVER POST HANDLER]: Account store failed for account {0} {1} {2}",
  210. existingAccount.FirstName, existingAccount.LastName, existingAccount.PrincipalID);
  211. return FailureResult();
  212. }
  213. result["result"] = existingAccount.ToKeyValuePairs();
  214. return ResultToBytes(result);
  215. }
  216. byte[] CreateUser(Dictionary<string, object> request)
  217. {
  218. if (! 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 Util.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 Util.DocToBytes(doc);
  272. }
  273. private byte[] ResultToBytes(Dictionary<string, object> result)
  274. {
  275. string xmlString = ServerUtils.BuildXmlResponse(result);
  276. return Util.UTF8NoBomEncoding.GetBytes(xmlString);
  277. }
  278. }
  279. }