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