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