diff options
Diffstat (limited to 'OpenSim/Services/Connectors/SimianGrid/SimianUserAccountServiceConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/SimianGrid/SimianUserAccountServiceConnector.cs | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/SimianGrid/SimianUserAccountServiceConnector.cs b/OpenSim/Services/Connectors/SimianGrid/SimianUserAccountServiceConnector.cs new file mode 100644 index 0000000..698c4c0 --- /dev/null +++ b/OpenSim/Services/Connectors/SimianGrid/SimianUserAccountServiceConnector.cs | |||
@@ -0,0 +1,337 @@ | |||
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 void InvalidateCache(UUID userID) | ||
195 | { | ||
196 | m_accountCache.Remove(userID); | ||
197 | } | ||
198 | |||
199 | public bool StoreUserAccount(UserAccount data) | ||
200 | { | ||
201 | // m_log.InfoFormat("[SIMIAN ACCOUNT CONNECTOR]: Storing user account for " + data.Name); | ||
202 | |||
203 | NameValueCollection requestArgs = new NameValueCollection | ||
204 | { | ||
205 | { "RequestMethod", "AddUser" }, | ||
206 | { "UserID", data.PrincipalID.ToString() }, | ||
207 | { "Name", data.Name }, | ||
208 | { "Email", data.Email }, | ||
209 | { "AccessLevel", data.UserLevel.ToString() } | ||
210 | }; | ||
211 | |||
212 | OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs); | ||
213 | |||
214 | if (response["Success"].AsBoolean()) | ||
215 | { | ||
216 | m_log.InfoFormat("[SIMIAN ACCOUNT CONNECTOR]: Storing user account data for " + data.Name); | ||
217 | |||
218 | requestArgs = new NameValueCollection | ||
219 | { | ||
220 | { "RequestMethod", "AddUserData" }, | ||
221 | { "UserID", data.PrincipalID.ToString() }, | ||
222 | { "CreationDate", data.Created.ToString() }, | ||
223 | { "UserFlags", data.UserFlags.ToString() }, | ||
224 | { "UserTitle", data.UserTitle } | ||
225 | }; | ||
226 | |||
227 | response = SimianGrid.PostToService(m_serverUrl, requestArgs); | ||
228 | bool success = response["Success"].AsBoolean(); | ||
229 | |||
230 | if (success) | ||
231 | { | ||
232 | // Cache the user account info | ||
233 | m_accountCache.AddOrUpdate(data.PrincipalID, data, CACHE_EXPIRATION_SECONDS); | ||
234 | } | ||
235 | else | ||
236 | { | ||
237 | m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to store user account data for " + data.Name + ": " + response["Message"].AsString()); | ||
238 | } | ||
239 | |||
240 | return success; | ||
241 | } | ||
242 | else | ||
243 | { | ||
244 | m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to store user account for " + data.Name + ": " + response["Message"].AsString()); | ||
245 | } | ||
246 | |||
247 | return false; | ||
248 | } | ||
249 | |||
250 | /// <summary> | ||
251 | /// Helper method for the various ways of retrieving a user account | ||
252 | /// </summary> | ||
253 | /// <param name="requestArgs">Service query parameters</param> | ||
254 | /// <returns>A UserAccount object on success, null on failure</returns> | ||
255 | private UserAccount GetUser(NameValueCollection requestArgs) | ||
256 | { | ||
257 | string lookupValue = (requestArgs.Count > 1) ? requestArgs[1] : "(Unknown)"; | ||
258 | // m_log.DebugFormat("[SIMIAN ACCOUNT CONNECTOR]: Looking up user account with query: " + lookupValue); | ||
259 | |||
260 | OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs); | ||
261 | if (response["Success"].AsBoolean()) | ||
262 | { | ||
263 | OSDMap user = response["User"] as OSDMap; | ||
264 | if (user != null) | ||
265 | return ResponseToUserAccount(user); | ||
266 | else | ||
267 | m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Account search failed, response data was in an invalid format"); | ||
268 | } | ||
269 | else | ||
270 | { | ||
271 | m_log.Warn("[SIMIAN ACCOUNT CONNECTOR]: Failed to lookup user account with query: " + lookupValue); | ||
272 | } | ||
273 | |||
274 | return null; | ||
275 | } | ||
276 | |||
277 | /// <summary> | ||
278 | /// Convert a User object in LLSD format to a UserAccount | ||
279 | /// </summary> | ||
280 | /// <param name="response">LLSD containing user account data</param> | ||
281 | /// <returns>A UserAccount object on success, null on failure</returns> | ||
282 | private UserAccount ResponseToUserAccount(OSDMap response) | ||
283 | { | ||
284 | if (response == null) | ||
285 | return null; | ||
286 | |||
287 | UserAccount account = new UserAccount(); | ||
288 | account.PrincipalID = response["UserID"].AsUUID(); | ||
289 | account.Created = response["CreationDate"].AsInteger(); | ||
290 | account.Email = response["Email"].AsString(); | ||
291 | account.ServiceURLs = new Dictionary<string, object>(0); | ||
292 | account.UserFlags = response["UserFlags"].AsInteger(); | ||
293 | account.UserLevel = response["AccessLevel"].AsInteger(); | ||
294 | account.UserTitle = response["UserTitle"].AsString(); | ||
295 | account.LocalToGrid = true; | ||
296 | if (response.ContainsKey("LocalToGrid")) | ||
297 | account.LocalToGrid = (response["LocalToGrid"].AsString() == "true" ? true : false); | ||
298 | |||
299 | GetFirstLastName(response["Name"].AsString(), out account.FirstName, out account.LastName); | ||
300 | |||
301 | // Cache the user account info | ||
302 | m_accountCache.AddOrUpdate(account.PrincipalID, account, CACHE_EXPIRATION_SECONDS); | ||
303 | |||
304 | return account; | ||
305 | } | ||
306 | |||
307 | /// <summary> | ||
308 | /// Convert a name with a single space in it to a first and last name | ||
309 | /// </summary> | ||
310 | /// <param name="name">A full name such as "John Doe"</param> | ||
311 | /// <param name="firstName">First name</param> | ||
312 | /// <param name="lastName">Last name (surname)</param> | ||
313 | private static void GetFirstLastName(string name, out string firstName, out string lastName) | ||
314 | { | ||
315 | if (String.IsNullOrEmpty(name)) | ||
316 | { | ||
317 | firstName = String.Empty; | ||
318 | lastName = String.Empty; | ||
319 | } | ||
320 | else | ||
321 | { | ||
322 | string[] names = name.Split(' '); | ||
323 | |||
324 | if (names.Length == 2) | ||
325 | { | ||
326 | firstName = names[0]; | ||
327 | lastName = names[1]; | ||
328 | } | ||
329 | else | ||
330 | { | ||
331 | firstName = String.Empty; | ||
332 | lastName = name; | ||
333 | } | ||
334 | } | ||
335 | } | ||
336 | } | ||
337 | } \ No newline at end of file | ||