opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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 OpenMetaverse; |
||
45 | |||
46 | namespace OpenSim.Server.Handlers.UserAccounts |
||
47 | { |
||
48 | public class UserAccountServerPostHandler : BaseStreamHandler |
||
49 | { |
||
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
51 | |||
52 | private IUserAccountService m_UserAccountService; |
||
53 | private bool m_AllowCreateUser = false; |
||
54 | private bool m_AllowSetAccount = false; |
||
55 | |||
56 | public UserAccountServerPostHandler(IUserAccountService service) |
||
57 | : this(service, null) {} |
||
58 | |||
59 | public UserAccountServerPostHandler(IUserAccountService service, IConfig config) : |
||
60 | base("POST", "/accounts") |
||
61 | { |
||
62 | m_UserAccountService = service; |
||
63 | |||
64 | if (config != null) |
||
65 | { |
||
66 | m_AllowCreateUser = config.GetBoolean("AllowCreateUser", m_AllowCreateUser); |
||
67 | m_AllowSetAccount = config.GetBoolean("AllowSetAccount", m_AllowSetAccount); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | protected override byte[] ProcessRequest(string path, Stream requestData, |
||
72 | IOSHttpRequest httpRequest, IOSHttpResponse httpResponse) |
||
73 | { |
||
74 | StreamReader sr = new StreamReader(requestData); |
||
75 | string body = sr.ReadToEnd(); |
||
76 | sr.Close(); |
||
77 | body = body.Trim(); |
||
78 | |||
79 | // We need to check the authorization header |
||
80 | //httpRequest.Headers["authorization"] ... |
||
81 | |||
82 | //m_log.DebugFormat("[XXX]: query String: {0}", body); |
||
83 | string method = string.Empty; |
||
84 | try |
||
85 | { |
||
86 | Dictionary<string, object> request = |
||
87 | ServerUtils.ParseQueryString(body); |
||
88 | |||
89 | if (!request.ContainsKey("METHOD")) |
||
90 | return FailureResult(); |
||
91 | |||
92 | method = request["METHOD"].ToString(); |
||
93 | |||
94 | switch (method) |
||
95 | { |
||
96 | case "createuser": |
||
97 | if (m_AllowCreateUser) |
||
98 | return CreateUser(request); |
||
99 | else |
||
100 | break; |
||
101 | case "getaccount": |
||
102 | return GetAccount(request); |
||
103 | case "getaccounts": |
||
104 | return GetAccounts(request); |
||
105 | case "setaccount": |
||
106 | if (m_AllowSetAccount) |
||
107 | return StoreAccount(request); |
||
108 | else |
||
109 | break; |
||
110 | } |
||
111 | |||
112 | m_log.DebugFormat("[USER SERVICE HANDLER]: unknown method request: {0}", method); |
||
113 | } |
||
114 | catch (Exception e) |
||
115 | { |
||
116 | m_log.DebugFormat("[USER SERVICE HANDLER]: Exception in method {0}: {1}", method, e); |
||
117 | } |
||
118 | |||
119 | return FailureResult(); |
||
120 | } |
||
121 | |||
122 | byte[] GetAccount(Dictionary<string, object> request) |
||
123 | { |
||
124 | UserAccount account = null; |
||
125 | UUID scopeID = UUID.Zero; |
||
126 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
127 | |||
128 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) |
||
129 | { |
||
130 | result["result"] = "null"; |
||
131 | return ResultToBytes(result); |
||
132 | } |
||
133 | |||
134 | if (request.ContainsKey("UserID") && request["UserID"] != null) |
||
135 | { |
||
136 | UUID userID; |
||
137 | if (UUID.TryParse(request["UserID"].ToString(), out userID)) |
||
138 | account = m_UserAccountService.GetUserAccount(scopeID, userID); |
||
139 | } |
||
140 | else if (request.ContainsKey("PrincipalID") && request["PrincipalID"] != null) |
||
141 | { |
||
142 | UUID userID; |
||
143 | if (UUID.TryParse(request["PrincipalID"].ToString(), out userID)) |
||
144 | account = m_UserAccountService.GetUserAccount(scopeID, userID); |
||
145 | } |
||
146 | else if (request.ContainsKey("Email") && request["Email"] != null) |
||
147 | { |
||
148 | account = m_UserAccountService.GetUserAccount(scopeID, request["Email"].ToString()); |
||
149 | } |
||
150 | else if (request.ContainsKey("FirstName") && request.ContainsKey("LastName") && |
||
151 | request["FirstName"] != null && request["LastName"] != null) |
||
152 | { |
||
153 | account = m_UserAccountService.GetUserAccount(scopeID, request["FirstName"].ToString(), request["LastName"].ToString()); |
||
154 | } |
||
155 | |||
156 | if (account == null) |
||
157 | { |
||
158 | result["result"] = "null"; |
||
159 | } |
||
160 | else |
||
161 | { |
||
162 | result["result"] = account.ToKeyValuePairs(); |
||
163 | } |
||
164 | |||
165 | return ResultToBytes(result); |
||
166 | } |
||
167 | |||
168 | byte[] GetAccounts(Dictionary<string, object> request) |
||
169 | { |
||
170 | if (!request.ContainsKey("query")) |
||
171 | return FailureResult(); |
||
172 | |||
173 | UUID scopeID = UUID.Zero; |
||
174 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) |
||
175 | return FailureResult(); |
||
176 | |||
177 | string query = request["query"].ToString(); |
||
178 | |||
179 | List<UserAccount> accounts = m_UserAccountService.GetUserAccounts(scopeID, query); |
||
180 | |||
181 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
182 | if ((accounts == null) || ((accounts != null) && (accounts.Count == 0))) |
||
183 | { |
||
184 | result["result"] = "null"; |
||
185 | } |
||
186 | else |
||
187 | { |
||
188 | int i = 0; |
||
189 | foreach (UserAccount acc in accounts) |
||
190 | { |
||
191 | Dictionary<string, object> rinfoDict = acc.ToKeyValuePairs(); |
||
192 | result["account" + i] = rinfoDict; |
||
193 | i++; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
198 | |||
199 | //m_log.DebugFormat("[GRID HANDLER]: resp string: {0}", xmlString); |
||
200 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
201 | } |
||
202 | |||
203 | byte[] StoreAccount(Dictionary<string, object> request) |
||
204 | { |
||
205 | UUID principalID = UUID.Zero; |
||
206 | if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID)) |
||
207 | return FailureResult(); |
||
208 | |||
209 | UUID scopeID = UUID.Zero; |
||
210 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) |
||
211 | return FailureResult(); |
||
212 | |||
213 | UserAccount existingAccount = m_UserAccountService.GetUserAccount(scopeID, principalID); |
||
214 | if (existingAccount == null) |
||
215 | return FailureResult(); |
||
216 | |||
217 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
218 | |||
219 | if (request.ContainsKey("FirstName")) |
||
220 | existingAccount.FirstName = request["FirstName"].ToString(); |
||
221 | |||
222 | if (request.ContainsKey("LastName")) |
||
223 | existingAccount.LastName = request["LastName"].ToString(); |
||
224 | |||
225 | if (request.ContainsKey("Email")) |
||
226 | existingAccount.Email = request["Email"].ToString(); |
||
227 | |||
228 | int created = 0; |
||
229 | if (request.ContainsKey("Created") && int.TryParse(request["Created"].ToString(), out created)) |
||
230 | existingAccount.Created = created; |
||
231 | |||
232 | int userLevel = 0; |
||
233 | if (request.ContainsKey("UserLevel") && int.TryParse(request["UserLevel"].ToString(), out userLevel)) |
||
234 | existingAccount.UserLevel = userLevel; |
||
235 | |||
236 | int userFlags = 0; |
||
237 | if (request.ContainsKey("UserFlags") && int.TryParse(request["UserFlags"].ToString(), out userFlags)) |
||
238 | existingAccount.UserFlags = userFlags; |
||
239 | |||
240 | if (request.ContainsKey("UserTitle")) |
||
241 | existingAccount.UserTitle = request["UserTitle"].ToString(); |
||
242 | |||
243 | if (!m_UserAccountService.StoreUserAccount(existingAccount)) |
||
244 | { |
||
245 | m_log.ErrorFormat( |
||
246 | "[USER ACCOUNT SERVER POST HANDLER]: Account store failed for account {0} {1} {2}", |
||
247 | existingAccount.FirstName, existingAccount.LastName, existingAccount.PrincipalID); |
||
248 | |||
249 | return FailureResult(); |
||
250 | } |
||
251 | |||
252 | result["result"] = existingAccount.ToKeyValuePairs(); |
||
253 | |||
254 | return ResultToBytes(result); |
||
255 | } |
||
256 | |||
257 | byte[] CreateUser(Dictionary<string, object> request) |
||
258 | { |
||
259 | if (! |
||
260 | 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 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 DocToBytes(doc); |
||
340 | } |
||
341 | |||
342 | private byte[] DocToBytes(XmlDocument doc) |
||
343 | { |
||
344 | MemoryStream ms = new MemoryStream(); |
||
345 | XmlTextWriter xw = new XmlTextWriter(ms, null); |
||
346 | xw.Formatting = Formatting.Indented; |
||
347 | doc.WriteTo(xw); |
||
348 | xw.Flush(); |
||
349 | |||
350 | return ms.ToArray(); |
||
351 | } |
||
352 | |||
353 | private byte[] ResultToBytes(Dictionary<string, object> result) |
||
354 | { |
||
355 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
356 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
357 | } |
||
358 | } |
||
359 | } |