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