31 using System.Reflection;
35 using System.Text.RegularExpressions;
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;
47 namespace OpenSim.Server.Handlers.UserAccounts
51 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 private bool m_AllowCreateUser =
false;
55 private bool m_AllowSetAccount =
false;
58 : this(service, null, null) {}
61 base(
"POST",
"/accounts", auth)
63 m_UserAccountService = service;
67 m_AllowCreateUser = config.GetBoolean(
"AllowCreateUser", m_AllowCreateUser);
68 m_AllowSetAccount = config.GetBoolean(
"AllowSetAccount", m_AllowSetAccount);
75 StreamReader sr =
new StreamReader(requestData);
76 string body = sr.ReadToEnd();
84 string method = string.Empty;
87 Dictionary<string, object> request =
88 ServerUtils.ParseQueryString(body);
90 if (!request.ContainsKey(
"METHOD"))
91 return FailureResult();
93 method = request[
"METHOD"].ToString();
98 if (m_AllowCreateUser)
99 return CreateUser(request);
103 return GetAccount(request);
105 return GetAccounts(request);
107 if (m_AllowSetAccount)
108 return StoreAccount(request);
113 m_log.DebugFormat(
"[USER SERVICE HANDLER]: unknown method request: {0}", method);
117 m_log.DebugFormat(
"[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e);
120 return FailureResult();
123 byte[] GetAccount(Dictionary<string, object> request)
126 UUID scopeID = UUID.Zero;
127 Dictionary<string, object> result =
new Dictionary<string, object>();
129 if (request.ContainsKey(
"ScopeID") && !UUID.TryParse(request[
"ScopeID"].ToString(), out scopeID))
131 result[
"result"] =
"null";
132 return ResultToBytes(result);
135 if (request.ContainsKey(
"UserID") && request[
"UserID"] != null)
138 if (UUID.TryParse(request[
"UserID"].ToString(), out userID))
139 account = m_UserAccountService.GetUserAccount(scopeID, userID);
141 else if (request.ContainsKey(
"PrincipalID") && request[
"PrincipalID"] != null)
144 if (UUID.TryParse(request[
"PrincipalID"].ToString(), out userID))
145 account = m_UserAccountService.GetUserAccount(scopeID, userID);
147 else if (request.ContainsKey(
"Email") && request[
"Email"] != null)
149 account = m_UserAccountService.GetUserAccount(scopeID, request[
"Email"].ToString());
151 else if (request.ContainsKey(
"FirstName") && request.ContainsKey(
"LastName") &&
152 request[
"FirstName"] != null && request[
"LastName"] != null)
154 account = m_UserAccountService.GetUserAccount(scopeID, request[
"FirstName"].ToString(), request[
"LastName"].ToString());
159 result[
"result"] =
"null";
163 result[
"result"] = account.ToKeyValuePairs();
166 return ResultToBytes(result);
169 byte[] GetAccounts(Dictionary<string, object> request)
171 if (!request.ContainsKey(
"query"))
172 return FailureResult();
174 UUID scopeID = UUID.Zero;
175 if (request.ContainsKey(
"ScopeID") && !UUID.TryParse(request[
"ScopeID"].ToString(), out scopeID))
176 return FailureResult();
178 string query = request[
"query"].ToString();
180 List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query);
182 Dictionary<string, object> result =
new Dictionary<string, object>();
183 if ((accounts == null) || ((accounts != null) && (accounts.Count == 0)))
185 result[
"result"] =
"null";
192 Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs();
193 result[
"account" + i] = rinfoDict;
198 string xmlString = ServerUtils.BuildXmlResponse(result);
201 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
204 byte[] StoreAccount(Dictionary<string, object> request)
206 UUID principalID = UUID.Zero;
207 if (request.ContainsKey(
"PrincipalID") && !UUID.TryParse(request[
"PrincipalID"].ToString(), out principalID))
208 return FailureResult();
210 UUID scopeID = UUID.Zero;
211 if (request.ContainsKey(
"ScopeID") && !UUID.TryParse(request[
"ScopeID"].ToString(), out scopeID))
212 return FailureResult();
214 UserAccount existingAccount = m_UserAccountService.GetUserAccount(scopeID, principalID);
215 if (existingAccount == null)
216 return FailureResult();
218 Dictionary<string, object> result =
new Dictionary<string, object>();
220 if (request.ContainsKey(
"FirstName"))
221 existingAccount.
FirstName = request[
"FirstName"].ToString();
223 if (request.ContainsKey(
"LastName"))
224 existingAccount.
LastName = request[
"LastName"].ToString();
226 if (request.ContainsKey(
"Email"))
227 existingAccount.
Email = request[
"Email"].ToString();
230 if (request.ContainsKey(
"Created") && int.TryParse(request[
"Created"].ToString(), out created))
231 existingAccount.
Created = created;
234 if (request.ContainsKey(
"UserLevel") && int.TryParse(request[
"UserLevel"].ToString(), out userLevel))
238 if (request.ContainsKey(
"UserFlags") && int.TryParse(request[
"UserFlags"].ToString(), out userFlags))
241 if (request.ContainsKey(
"UserTitle"))
242 existingAccount.
UserTitle = request[
"UserTitle"].ToString();
244 if (!m_UserAccountService.StoreUserAccount(existingAccount))
247 "[USER ACCOUNT SERVER POST HANDLER]: Account store failed for account {0} {1} {2}",
248 existingAccount.FirstName, existingAccount.LastName, existingAccount.PrincipalID);
250 return FailureResult();
253 result[
"result"] = existingAccount.ToKeyValuePairs();
255 return ResultToBytes(result);
258 byte[] CreateUser(Dictionary<string, object> request)
260 if (! request.ContainsKey(
"FirstName")
261 && request.ContainsKey(
"LastName")
262 && request.ContainsKey(
"Password"))
263 return FailureResult();
265 Dictionary<string, object> result =
new Dictionary<string, object>();
267 UUID scopeID = UUID.Zero;
268 if (request.ContainsKey(
"ScopeID") && !UUID.TryParse(request[
"ScopeID"].ToString(), out scopeID))
269 return FailureResult();
271 UUID principalID = UUID.Random();
272 if (request.ContainsKey(
"PrincipalID") && !UUID.TryParse(request[
"PrincipalID"].ToString(), out principalID))
273 return FailureResult();
275 string firstName = request[
"FirstName"].ToString();
276 string lastName = request[
"LastName"].ToString();
277 string password = request[
"Password"].ToString();
280 if (request.ContainsKey(
"Email"))
281 email = request[
"Email"].ToString();
287 = ((UserAccountService)m_UserAccountService).CreateUser(
288 scopeID, principalID, firstName, lastName, password, email);
290 if (createdUserAccount == null)
291 return FailureResult();
293 result[
"result"] = createdUserAccount.ToKeyValuePairs();
295 return ResultToBytes(result);
298 private byte[] SuccessResult()
300 XmlDocument doc =
new XmlDocument();
302 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
305 doc.AppendChild(xmlnode);
307 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
310 doc.AppendChild(rootElement);
312 XmlElement result = doc.CreateElement(
"",
"result",
"");
313 result.AppendChild(doc.CreateTextNode(
"Success"));
315 rootElement.AppendChild(result);
317 return Util.DocToBytes(doc);
320 private byte[] FailureResult()
322 XmlDocument doc =
new XmlDocument();
324 XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration,
327 doc.AppendChild(xmlnode);
329 XmlElement rootElement = doc.CreateElement(
"",
"ServerResponse",
332 doc.AppendChild(rootElement);
334 XmlElement result = doc.CreateElement(
"",
"result",
"");
335 result.AppendChild(doc.CreateTextNode(
"Failure"));
337 rootElement.AppendChild(result);
339 return Util.DocToBytes(doc);
342 private byte[] ResultToBytes(Dictionary<string, object> result)
344 string xmlString = ServerUtils.BuildXmlResponse(result);
345 return Util.UTF8NoBomEncoding.GetBytes(xmlString);
Base streamed request handler.
override byte[] ProcessRequest(string path, Stream requestData, IOSHttpRequest httpRequest, IOSHttpResponse httpResponse)
UserAccountServerPostHandler(IUserAccountService service, IConfig config, IServiceAuth auth)
UserAccountServerPostHandler(IUserAccountService service)