diff options
Diffstat (limited to 'OpenSim/Services/UserAccountService/UserAccountService.cs')
-rw-r--r-- | OpenSim/Services/UserAccountService/UserAccountService.cs | 381 |
1 files changed, 381 insertions, 0 deletions
diff --git a/OpenSim/Services/UserAccountService/UserAccountService.cs b/OpenSim/Services/UserAccountService/UserAccountService.cs new file mode 100644 index 0000000..7b38aa6 --- /dev/null +++ b/OpenSim/Services/UserAccountService/UserAccountService.cs | |||
@@ -0,0 +1,381 @@ | |||
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 System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using Nini.Config; | ||
32 | using OpenSim.Data; | ||
33 | using OpenSim.Services.Interfaces; | ||
34 | using OpenSim.Framework.Console; | ||
35 | using GridRegion = OpenSim.Services.Interfaces.GridRegion; | ||
36 | |||
37 | using OpenMetaverse; | ||
38 | using log4net; | ||
39 | |||
40 | namespace OpenSim.Services.UserAccountService | ||
41 | { | ||
42 | public class UserAccountService : UserAccountServiceBase, IUserAccountService | ||
43 | { | ||
44 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
45 | private static UserAccountService m_RootInstance; | ||
46 | |||
47 | protected IGridService m_GridService; | ||
48 | protected IAuthenticationService m_AuthenticationService; | ||
49 | protected IPresenceService m_PresenceService; | ||
50 | protected IInventoryService m_InventoryService; | ||
51 | |||
52 | public UserAccountService(IConfigSource config) | ||
53 | : base(config) | ||
54 | { | ||
55 | IConfig userConfig = config.Configs["UserAccountService"]; | ||
56 | if (userConfig == null) | ||
57 | throw new Exception("No UserAccountService configuration"); | ||
58 | |||
59 | // In case there are several instances of this class in the same process, | ||
60 | // the console commands are only registered for the root instance | ||
61 | if (m_RootInstance == null) | ||
62 | { | ||
63 | m_RootInstance = this; | ||
64 | string gridServiceDll = userConfig.GetString("GridService", string.Empty); | ||
65 | if (gridServiceDll != string.Empty) | ||
66 | m_GridService = LoadPlugin<IGridService>(gridServiceDll, new Object[] { config }); | ||
67 | |||
68 | string authServiceDll = userConfig.GetString("AuthenticationService", string.Empty); | ||
69 | if (authServiceDll != string.Empty) | ||
70 | m_AuthenticationService = LoadPlugin<IAuthenticationService>(authServiceDll, new Object[] { config }); | ||
71 | |||
72 | string presenceServiceDll = userConfig.GetString("PresenceService", string.Empty); | ||
73 | if (presenceServiceDll != string.Empty) | ||
74 | m_PresenceService = LoadPlugin<IPresenceService>(presenceServiceDll, new Object[] { config }); | ||
75 | |||
76 | string invServiceDll = userConfig.GetString("InventoryService", string.Empty); | ||
77 | if (invServiceDll != string.Empty) | ||
78 | m_InventoryService = LoadPlugin<IInventoryService>(invServiceDll, new Object[] { config }); | ||
79 | |||
80 | if (MainConsole.Instance != null) | ||
81 | { | ||
82 | MainConsole.Instance.Commands.AddCommand("UserService", false, | ||
83 | "create user", | ||
84 | "create user [<first> [<last> [<pass> [<email>]]]]", | ||
85 | "Create a new user", HandleCreateUser); | ||
86 | MainConsole.Instance.Commands.AddCommand("UserService", false, "reset user password", | ||
87 | "reset user password [<first> [<last> [<password>]]]", | ||
88 | "Reset a user password", HandleResetUserPassword); | ||
89 | } | ||
90 | |||
91 | } | ||
92 | |||
93 | } | ||
94 | |||
95 | #region IUserAccountService | ||
96 | |||
97 | public UserAccount GetUserAccount(UUID scopeID, string firstName, | ||
98 | string lastName) | ||
99 | { | ||
100 | UserAccountData[] d; | ||
101 | |||
102 | if (scopeID != UUID.Zero) | ||
103 | { | ||
104 | d = m_Database.Get( | ||
105 | new string[] { "ScopeID", "FirstName", "LastName" }, | ||
106 | new string[] { scopeID.ToString(), firstName, lastName }); | ||
107 | } | ||
108 | else | ||
109 | { | ||
110 | d = m_Database.Get( | ||
111 | new string[] { "FirstName", "LastName" }, | ||
112 | new string[] { firstName, lastName }); | ||
113 | } | ||
114 | |||
115 | if (d.Length < 1) | ||
116 | return null; | ||
117 | |||
118 | return MakeUserAccount(d[0]); | ||
119 | } | ||
120 | |||
121 | private UserAccount MakeUserAccount(UserAccountData d) | ||
122 | { | ||
123 | UserAccount u = new UserAccount(); | ||
124 | u.FirstName = d.FirstName; | ||
125 | u.LastName = d.LastName; | ||
126 | u.PrincipalID = d.PrincipalID; | ||
127 | u.ScopeID = d.ScopeID; | ||
128 | if (d.Data.ContainsKey("Email") && d.Data["Email"] != null) | ||
129 | u.Email = d.Data["Email"].ToString(); | ||
130 | else | ||
131 | u.Email = string.Empty; | ||
132 | u.Created = Convert.ToInt32(d.Data["Created"].ToString()); | ||
133 | if (d.Data.ContainsKey("UserTitle") && d.Data["UserTitle"] != null) | ||
134 | u.UserTitle = d.Data["UserTitle"].ToString(); | ||
135 | else | ||
136 | u.UserTitle = string.Empty; | ||
137 | if (d.Data.ContainsKey("UserLevel") && d.Data["UserLevel"] != null) | ||
138 | Int32.TryParse(d.Data["UserLevel"], out u.UserLevel); | ||
139 | if (d.Data.ContainsKey("UserFlags") && d.Data["UserFlags"] != null) | ||
140 | Int32.TryParse(d.Data["UserFlags"], out u.UserFlags); | ||
141 | |||
142 | if (d.Data.ContainsKey("ServiceURLs") && d.Data["ServiceURLs"] != null) | ||
143 | { | ||
144 | string[] URLs = d.Data["ServiceURLs"].ToString().Split(new char[] { ' ' }); | ||
145 | u.ServiceURLs = new Dictionary<string, object>(); | ||
146 | |||
147 | foreach (string url in URLs) | ||
148 | { | ||
149 | string[] parts = url.Split(new char[] { '=' }); | ||
150 | |||
151 | if (parts.Length != 2) | ||
152 | continue; | ||
153 | |||
154 | string name = System.Web.HttpUtility.UrlDecode(parts[0]); | ||
155 | string val = System.Web.HttpUtility.UrlDecode(parts[1]); | ||
156 | |||
157 | u.ServiceURLs[name] = val; | ||
158 | } | ||
159 | } | ||
160 | else | ||
161 | u.ServiceURLs = new Dictionary<string, object>(); | ||
162 | |||
163 | return u; | ||
164 | } | ||
165 | |||
166 | public UserAccount GetUserAccount(UUID scopeID, string email) | ||
167 | { | ||
168 | UserAccountData[] d; | ||
169 | |||
170 | if (scopeID != UUID.Zero) | ||
171 | { | ||
172 | d = m_Database.Get( | ||
173 | new string[] { "ScopeID", "Email" }, | ||
174 | new string[] { scopeID.ToString(), email }); | ||
175 | } | ||
176 | else | ||
177 | { | ||
178 | d = m_Database.Get( | ||
179 | new string[] { "Email" }, | ||
180 | new string[] { email }); | ||
181 | } | ||
182 | |||
183 | if (d.Length < 1) | ||
184 | return null; | ||
185 | |||
186 | return MakeUserAccount(d[0]); | ||
187 | } | ||
188 | |||
189 | public UserAccount GetUserAccount(UUID scopeID, UUID principalID) | ||
190 | { | ||
191 | UserAccountData[] d; | ||
192 | |||
193 | if (scopeID != UUID.Zero) | ||
194 | { | ||
195 | d = m_Database.Get( | ||
196 | new string[] { "ScopeID", "PrincipalID" }, | ||
197 | new string[] { scopeID.ToString(), principalID.ToString() }); | ||
198 | } | ||
199 | else | ||
200 | { | ||
201 | d = m_Database.Get( | ||
202 | new string[] { "PrincipalID" }, | ||
203 | new string[] { principalID.ToString() }); | ||
204 | } | ||
205 | |||
206 | if (d.Length < 1) | ||
207 | { | ||
208 | return null; | ||
209 | } | ||
210 | |||
211 | return MakeUserAccount(d[0]); | ||
212 | } | ||
213 | |||
214 | public bool StoreUserAccount(UserAccount data) | ||
215 | { | ||
216 | UserAccountData d = new UserAccountData(); | ||
217 | |||
218 | d.FirstName = data.FirstName; | ||
219 | d.LastName = data.LastName; | ||
220 | d.PrincipalID = data.PrincipalID; | ||
221 | d.ScopeID = data.ScopeID; | ||
222 | d.Data = new Dictionary<string, string>(); | ||
223 | d.Data["Email"] = data.Email; | ||
224 | d.Data["Created"] = data.Created.ToString(); | ||
225 | d.Data["UserLevel"] = data.UserLevel.ToString(); | ||
226 | d.Data["UserFlags"] = data.UserFlags.ToString(); | ||
227 | if (data.UserTitle != null) | ||
228 | d.Data["UserTitle"] = data.UserTitle.ToString(); | ||
229 | |||
230 | List<string> parts = new List<string>(); | ||
231 | |||
232 | foreach (KeyValuePair<string, object> kvp in data.ServiceURLs) | ||
233 | { | ||
234 | string key = System.Web.HttpUtility.UrlEncode(kvp.Key); | ||
235 | string val = System.Web.HttpUtility.UrlEncode(kvp.Value.ToString()); | ||
236 | parts.Add(key + "=" + val); | ||
237 | } | ||
238 | |||
239 | d.Data["ServiceURLs"] = string.Join(" ", parts.ToArray()); | ||
240 | |||
241 | return m_Database.Store(d); | ||
242 | } | ||
243 | |||
244 | public List<UserAccount> GetUserAccounts(UUID scopeID, string query) | ||
245 | { | ||
246 | UserAccountData[] d = m_Database.GetUsers(scopeID, query); | ||
247 | |||
248 | if (d == null) | ||
249 | return new List<UserAccount>(); | ||
250 | |||
251 | List<UserAccount> ret = new List<UserAccount>(); | ||
252 | |||
253 | foreach (UserAccountData data in d) | ||
254 | ret.Add(MakeUserAccount(data)); | ||
255 | |||
256 | return ret; | ||
257 | } | ||
258 | |||
259 | #endregion | ||
260 | |||
261 | #region Console commands | ||
262 | /// <summary> | ||
263 | /// Create a new user | ||
264 | /// </summary> | ||
265 | /// <param name="cmdparams">string array with parameters: firstname, lastname, password, locationX, locationY, email</param> | ||
266 | protected void HandleCreateUser(string module, string[] cmdparams) | ||
267 | { | ||
268 | string firstName; | ||
269 | string lastName; | ||
270 | string password; | ||
271 | string email; | ||
272 | |||
273 | if (cmdparams.Length < 3) | ||
274 | firstName = MainConsole.Instance.CmdPrompt("First name", "Default"); | ||
275 | else firstName = cmdparams[2]; | ||
276 | |||
277 | if (cmdparams.Length < 4) | ||
278 | lastName = MainConsole.Instance.CmdPrompt("Last name", "User"); | ||
279 | else lastName = cmdparams[3]; | ||
280 | |||
281 | if (cmdparams.Length < 5) | ||
282 | password = MainConsole.Instance.PasswdPrompt("Password"); | ||
283 | else password = cmdparams[4]; | ||
284 | |||
285 | if (cmdparams.Length < 6) | ||
286 | email = MainConsole.Instance.CmdPrompt("Email", ""); | ||
287 | else email = cmdparams[5]; | ||
288 | |||
289 | UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName); | ||
290 | if (null == account) | ||
291 | { | ||
292 | account = new UserAccount(UUID.Zero, firstName, lastName, email); | ||
293 | if (account.ServiceURLs == null || (account.ServiceURLs != null && account.ServiceURLs.Count == 0)) | ||
294 | { | ||
295 | account.ServiceURLs = new Dictionary<string, object>(); | ||
296 | account.ServiceURLs["HomeURI"] = string.Empty; | ||
297 | account.ServiceURLs["GatekeeperURI"] = string.Empty; | ||
298 | account.ServiceURLs["InventoryServerURI"] = string.Empty; | ||
299 | account.ServiceURLs["AssetServerURI"] = string.Empty; | ||
300 | } | ||
301 | |||
302 | if (StoreUserAccount(account)) | ||
303 | { | ||
304 | bool success = false; | ||
305 | if (m_AuthenticationService != null) | ||
306 | success = m_AuthenticationService.SetPassword(account.PrincipalID, password); | ||
307 | if (!success) | ||
308 | m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set password for account {0} {1}.", | ||
309 | firstName, lastName); | ||
310 | |||
311 | GridRegion home = null; | ||
312 | if (m_GridService != null) | ||
313 | { | ||
314 | List<GridRegion> defaultRegions = m_GridService.GetDefaultRegions(UUID.Zero); | ||
315 | if (defaultRegions != null && defaultRegions.Count >= 1) | ||
316 | home = defaultRegions[0]; | ||
317 | |||
318 | if (m_PresenceService != null && home != null) | ||
319 | m_PresenceService.SetHomeLocation(account.PrincipalID.ToString(), home.RegionID, new Vector3(128, 128, 0), new Vector3(0, 1, 0)); | ||
320 | else | ||
321 | m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set home for account {0} {1}.", | ||
322 | firstName, lastName); | ||
323 | |||
324 | } | ||
325 | else | ||
326 | m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to retrieve home region for account {0} {1}.", | ||
327 | firstName, lastName); | ||
328 | |||
329 | if (m_InventoryService != null) | ||
330 | success = m_InventoryService.CreateUserInventory(account.PrincipalID); | ||
331 | if (!success) | ||
332 | m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to create inventory for account {0} {1}.", | ||
333 | firstName, lastName); | ||
334 | |||
335 | |||
336 | m_log.InfoFormat("[USER ACCOUNT SERVICE]: Account {0} {1} created successfully", firstName, lastName); | ||
337 | } | ||
338 | } | ||
339 | else | ||
340 | { | ||
341 | m_log.ErrorFormat("[USER ACCOUNT SERVICE]: A user with the name {0} {1} already exists!", firstName, lastName); | ||
342 | } | ||
343 | |||
344 | } | ||
345 | |||
346 | protected void HandleResetUserPassword(string module, string[] cmdparams) | ||
347 | { | ||
348 | string firstName; | ||
349 | string lastName; | ||
350 | string newPassword; | ||
351 | |||
352 | if (cmdparams.Length < 4) | ||
353 | firstName = MainConsole.Instance.CmdPrompt("First name"); | ||
354 | else firstName = cmdparams[3]; | ||
355 | |||
356 | if (cmdparams.Length < 5) | ||
357 | lastName = MainConsole.Instance.CmdPrompt("Last name"); | ||
358 | else lastName = cmdparams[4]; | ||
359 | |||
360 | if (cmdparams.Length < 6) | ||
361 | newPassword = MainConsole.Instance.PasswdPrompt("New password"); | ||
362 | else newPassword = cmdparams[5]; | ||
363 | |||
364 | UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName); | ||
365 | if (account == null) | ||
366 | m_log.ErrorFormat("[USER ACCOUNT SERVICE]: No such user"); | ||
367 | |||
368 | bool success = false; | ||
369 | if (m_AuthenticationService != null) | ||
370 | success = m_AuthenticationService.SetPassword(account.PrincipalID, newPassword); | ||
371 | if (!success) | ||
372 | m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Unable to reset password for account {0} {1}.", | ||
373 | firstName, lastName); | ||
374 | else | ||
375 | m_log.InfoFormat("[USER ACCOUNT SERVICE]: Password reset for user {0} {1}", firstName, lastName); | ||
376 | } | ||
377 | |||
378 | #endregion | ||
379 | |||
380 | } | ||
381 | } | ||