opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
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 System;
29 using System.Collections.Generic;
30 using System.Collections.Specialized;
31 using System.Reflection;
32 using OpenSim.Framework;
33 using OpenSim.Region.Framework.Interfaces;
34 using OpenSim.Region.Framework.Scenes;
35 using OpenSim.Services.Interfaces;
36 using log4net;
37 using Mono.Addins;
38 using Nini.Config;
39 using OpenMetaverse;
40 using OpenMetaverse.StructuredData;
41  
42 namespace OpenSim.Services.Connectors.SimianGrid
43 {
44 /// <summary>
45 /// Connects user account data (creating new users, looking up existing
46 /// users) to the SimianGrid backend
47 /// </summary>
48 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SimianUserAccountServiceConnector")]
49 public class SimianUserAccountServiceConnector : IUserAccountService, ISharedRegionModule
50 {
51 private const double CACHE_EXPIRATION_SECONDS = 120.0;
52  
53 private static readonly ILog m_log =
54 LogManager.GetLogger(
55 MethodBase.GetCurrentMethod().DeclaringType);
56  
57 private string m_serverUrl = String.Empty;
58 private ExpiringCache<UUID, UserAccount> m_accountCache = new ExpiringCache<UUID,UserAccount>();
59 private bool m_Enabled;
60  
61 #region ISharedRegionModule
62  
63 public Type ReplaceableInterface { get { return null; } }
64 public void RegionLoaded(Scene scene) { }
65 public void PostInitialise() { }
66 public void Close() { }
67  
68 public SimianUserAccountServiceConnector() { }
69 public string Name { get { return "SimianUserAccountServiceConnector"; } }
70 public void AddRegion(Scene scene) { if (m_Enabled) { scene.RegisterModuleInterface<IUserAccountService>(this); } }
71 public void RemoveRegion(Scene scene) { if (m_Enabled) { scene.UnregisterModuleInterface<IUserAccountService>(this); } }
72  
73 #endregion ISharedRegionModule
74  
75 public SimianUserAccountServiceConnector(IConfigSource source)
76 {
77 CommonInit(source);
78 }
79  
80 public void Initialise(IConfigSource source)
81 {
82 IConfig moduleConfig = source.Configs["Modules"];
83 if (moduleConfig != null)
84 {
85 string name = moduleConfig.GetString("UserAccountServices", "");
86 if (name == Name)
87 CommonInit(source);
88 }
89 }
90  
91 private void CommonInit(IConfigSource source)
92 {
93 IConfig gridConfig = source.Configs["UserAccountService"];
94 if (gridConfig != null)
95 {
96 string serviceUrl = gridConfig.GetString("UserAccountServerURI");
97 if (!String.IsNullOrEmpty(serviceUrl))
98 {
99 if (!serviceUrl.EndsWith("/") && !serviceUrl.EndsWith("="))
100 serviceUrl = serviceUrl + '/';
101 m_serverUrl = serviceUrl;
102 m_Enabled = true;
103 }
104 }
105  
106 if (String.IsNullOrEmpty(m_serverUrl))
107 m_log.Info("[SIMIAN ACCOUNT CONNECTOR]: No UserAccountServerURI specified, disabling connector");
108 }
109  
110 public UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)
111 {
112 NameValueCollection requestArgs = new NameValueCollection
113 {
114 { "RequestMethod", "GetUser" },
115 { "Name", firstName + ' ' + lastName }
116 };
117  
118 return GetUser(requestArgs);
119 }
120  
121 public UserAccount GetUserAccount(UUID scopeID, string email)
122 {
123 NameValueCollection requestArgs = new NameValueCollection
124 {
125 { "RequestMethod", "GetUser" },
126 { "Email", email }
127 };
128  
129 return GetUser(requestArgs);
130 }
131  
132 public UserAccount GetUserAccount(UUID scopeID, UUID userID)
133 {
134 // Cache check
135 UserAccount account;
136 if (m_accountCache.TryGetValue(userID, out account))
137 return account;
138  
139 NameValueCollection requestArgs = new NameValueCollection
140 {
141 { "RequestMethod", "GetUser" },
142 { "UserID", userID.ToString() }
143 };
144  
145 account = GetUser(requestArgs);
146  
147 if (account == null)
148 {
149 // Store null responses too, to avoid repeated lookups for missing accounts
150 m_accountCache.AddOrUpdate(userID, null, CACHE_EXPIRATION_SECONDS);
151 }
152  
153 return account;
154 }
155  
156 public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
157 {
158 List<UserAccount> accounts = new List<UserAccount>();
159  
160 // m_log.DebugFormat("[SIMIAN ACCOUNT CONNECTOR]: Searching for user accounts with name query " + query);
161  
162 NameValueCollection requestArgs = new NameValueCollection
163 {
164 { "RequestMethod", "GetUsers" },
165 { "NameQuery", query }
166 };
167  
168 OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
169 if (response["Success"].AsBoolean())
170 {
171 OSDArray array = response["Users"] as OSDArray;
172 if (array != null && array.Count > 0)
173 {
174 for (int i = 0; i < array.Count; i++)
175 {
176 UserAccount account = ResponseToUserAccount(array[i] as OSDMap);
177 if (account != null)
178 accounts.Add(account);
179 }
180 }
181 else
182 {
183 m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Account search failed, response data was in an invalid format");
184 }
185 }
186 else
187 {
188 m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to search for account data by name " + query);
189 }
190  
191 return accounts;
192 }
193  
194 public bool StoreUserAccount(UserAccount data)
195 {
196 // m_log.InfoFormat("[SIMIAN ACCOUNT CONNECTOR]: Storing user account for " + data.Name);
197  
198 NameValueCollection requestArgs = new NameValueCollection
199 {
200 { "RequestMethod", "AddUser" },
201 { "UserID", data.PrincipalID.ToString() },
202 { "Name", data.Name },
203 { "Email", data.Email },
204 { "AccessLevel", data.UserLevel.ToString() }
205 };
206  
207 OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
208  
209 if (response["Success"].AsBoolean())
210 {
211 m_log.InfoFormat("[SIMIAN ACCOUNT CONNECTOR]: Storing user account data for " + data.Name);
212  
213 requestArgs = new NameValueCollection
214 {
215 { "RequestMethod", "AddUserData" },
216 { "UserID", data.PrincipalID.ToString() },
217 { "CreationDate", data.Created.ToString() },
218 { "UserFlags", data.UserFlags.ToString() },
219 { "UserTitle", data.UserTitle }
220 };
221  
222 response = SimianGrid.PostToService(m_serverUrl, requestArgs);
223 bool success = response["Success"].AsBoolean();
224  
225 if (success)
226 {
227 // Cache the user account info
228 m_accountCache.AddOrUpdate(data.PrincipalID, data, CACHE_EXPIRATION_SECONDS);
229 }
230 else
231 {
232 m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to store user account data for " + data.Name + ": " + response["Message"].AsString());
233 }
234  
235 return success;
236 }
237 else
238 {
239 m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to store user account for " + data.Name + ": " + response["Message"].AsString());
240 }
241  
242 return false;
243 }
244  
245 /// <summary>
246 /// Helper method for the various ways of retrieving a user account
247 /// </summary>
248 /// <param name="requestArgs">Service query parameters</param>
249 /// <returns>A UserAccount object on success, null on failure</returns>
250 private UserAccount GetUser(NameValueCollection requestArgs)
251 {
252 string lookupValue = (requestArgs.Count > 1) ? requestArgs[1] : "(Unknown)";
253 // m_log.DebugFormat("[SIMIAN ACCOUNT CONNECTOR]: Looking up user account with query: " + lookupValue);
254  
255 OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
256 if (response["Success"].AsBoolean())
257 {
258 OSDMap user = response["User"] as OSDMap;
259 if (user != null)
260 return ResponseToUserAccount(user);
261 else
262 m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Account search failed, response data was in an invalid format");
263 }
264 else
265 {
266 m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to lookup user account with query: " + lookupValue);
267 }
268  
269 return null;
270 }
271  
272 /// <summary>
273 /// Convert a User object in LLSD format to a UserAccount
274 /// </summary>
275 /// <param name="response">LLSD containing user account data</param>
276 /// <returns>A UserAccount object on success, null on failure</returns>
277 private UserAccount ResponseToUserAccount(OSDMap response)
278 {
279 if (response == null)
280 return null;
281  
282 UserAccount account = new UserAccount();
283 account.PrincipalID = response["UserID"].AsUUID();
284 account.Created = response["CreationDate"].AsInteger();
285 account.Email = response["Email"].AsString();
286 account.ServiceURLs = new Dictionary<string, object>(0);
287 account.UserFlags = response["UserFlags"].AsInteger();
288 account.UserLevel = response["AccessLevel"].AsInteger();
289 account.UserTitle = response["UserTitle"].AsString();
290 account.LocalToGrid = true;
291 if (response.ContainsKey("LocalToGrid"))
292 account.LocalToGrid = (response["LocalToGrid"].AsString() == "true" ? true : false);
293  
294 GetFirstLastName(response["Name"].AsString(), out account.FirstName, out account.LastName);
295  
296 // Cache the user account info
297 m_accountCache.AddOrUpdate(account.PrincipalID, account, CACHE_EXPIRATION_SECONDS);
298  
299 return account;
300 }
301  
302 /// <summary>
303 /// Convert a name with a single space in it to a first and last name
304 /// </summary>
305 /// <param name="name">A full name such as "John Doe"</param>
306 /// <param name="firstName">First name</param>
307 /// <param name="lastName">Last name (surname)</param>
308 private static void GetFirstLastName(string name, out string firstName, out string lastName)
309 {
310 if (String.IsNullOrEmpty(name))
311 {
312 firstName = String.Empty;
313 lastName = String.Empty;
314 }
315 else
316 {
317 string[] names = name.Split(' ');
318  
319 if (names.Length == 2)
320 {
321 firstName = names[0];
322 lastName = names[1];
323 }
324 else
325 {
326 firstName = String.Empty;
327 lastName = name;
328 }
329 }
330 }
331 }
332 }