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