clockwerk-opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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 | { |
||
49 | public class UserAccountServerPostHandler : BaseStreamHandler |
||
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 | |||
57 | public UserAccountServerPostHandler(IUserAccountService service) |
||
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 (! |
||
261 | request.ContainsKey("FirstName") |
||
262 | && request.ContainsKey("LastName") |
||
263 | && request.ContainsKey("Password")) |
||
264 | return FailureResult(); |
||
265 | |||
266 | Dictionary<string, object> result = new Dictionary<string, object>(); |
||
267 | |||
268 | UUID scopeID = UUID.Zero; |
||
269 | if (request.ContainsKey("ScopeID") && !UUID.TryParse(request["ScopeID"].ToString(), out scopeID)) |
||
270 | return FailureResult(); |
||
271 | |||
272 | UUID principalID = UUID.Random(); |
||
273 | if (request.ContainsKey("PrincipalID") && !UUID.TryParse(request["PrincipalID"].ToString(), out principalID)) |
||
274 | return FailureResult(); |
||
275 | |||
276 | string firstName = request["FirstName"].ToString(); |
||
277 | string lastName = request["LastName"].ToString(); |
||
278 | string password = request["Password"].ToString(); |
||
279 | |||
280 | string email = ""; |
||
281 | if (request.ContainsKey("Email")) |
||
282 | email = request["Email"].ToString(); |
||
283 | |||
284 | UserAccount createdUserAccount = null; |
||
285 | |||
286 | if (m_UserAccountService is UserAccountService) |
||
287 | createdUserAccount |
||
288 | = ((UserAccountService)m_UserAccountService).CreateUser( |
||
289 | scopeID, principalID, firstName, lastName, password, email); |
||
290 | |||
291 | if (createdUserAccount == null) |
||
292 | return FailureResult(); |
||
293 | |||
294 | result["result"] = createdUserAccount.ToKeyValuePairs(); |
||
295 | |||
296 | return ResultToBytes(result); |
||
297 | } |
||
298 | |||
299 | private byte[] SuccessResult() |
||
300 | { |
||
301 | XmlDocument doc = new XmlDocument(); |
||
302 | |||
303 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, |
||
304 | "", ""); |
||
305 | |||
306 | doc.AppendChild(xmlnode); |
||
307 | |||
308 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", |
||
309 | ""); |
||
310 | |||
311 | doc.AppendChild(rootElement); |
||
312 | |||
313 | XmlElement result = doc.CreateElement("", "result", ""); |
||
314 | result.AppendChild(doc.CreateTextNode("Success")); |
||
315 | |||
316 | rootElement.AppendChild(result); |
||
317 | |||
318 | return Util.DocToBytes(doc); |
||
319 | } |
||
320 | |||
321 | private byte[] FailureResult() |
||
322 | { |
||
323 | XmlDocument doc = new XmlDocument(); |
||
324 | |||
325 | XmlNode xmlnode = doc.CreateNode(XmlNodeType.XmlDeclaration, |
||
326 | "", ""); |
||
327 | |||
328 | doc.AppendChild(xmlnode); |
||
329 | |||
330 | XmlElement rootElement = doc.CreateElement("", "ServerResponse", |
||
331 | ""); |
||
332 | |||
333 | doc.AppendChild(rootElement); |
||
334 | |||
335 | XmlElement result = doc.CreateElement("", "result", ""); |
||
336 | result.AppendChild(doc.CreateTextNode("Failure")); |
||
337 | |||
338 | rootElement.AppendChild(result); |
||
339 | |||
340 | return Util.DocToBytes(doc); |
||
341 | } |
||
342 | |||
343 | private byte[] ResultToBytes(Dictionary<string, object> result) |
||
344 | { |
||
345 | string xmlString = ServerUtils.BuildXmlResponse(result); |
||
346 | return Util.UTF8NoBomEncoding.GetBytes(xmlString); |
||
347 | } |
||
348 | } |
||
349 | } |