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;
40 using OpenMetaverse.StructuredData;
42 namespace OpenSim.Services.Connectors.SimianGrid
48 [Extension(Path =
"/OpenSim/RegionModules", NodeName =
"RegionModule", Id =
"SimianUserAccountServiceConnector")]
51 private const double CACHE_EXPIRATION_SECONDS = 120.0;
53 private static readonly ILog m_log =
55 MethodBase.GetCurrentMethod().DeclaringType);
57 private string m_serverUrl = String.Empty;
58 private ExpiringCache<UUID, UserAccount> m_accountCache =
new ExpiringCache<UUID,UserAccount>();
59 private bool m_Enabled;
61 #region ISharedRegionModule
63 public Type ReplaceableInterface {
get {
return null; } }
69 public string Name {
get {
return "SimianUserAccountServiceConnector"; } }
73 #endregion ISharedRegionModule
82 IConfig moduleConfig = source.Configs[
"Modules"];
83 if (moduleConfig != null)
85 string name = moduleConfig.GetString(
"UserAccountServices",
"");
91 private void CommonInit(IConfigSource source)
93 IConfig gridConfig = source.Configs[
"UserAccountService"];
94 if (gridConfig != null)
96 string serviceUrl = gridConfig.GetString(
"UserAccountServerURI");
97 if (!
String.IsNullOrEmpty(serviceUrl))
99 if (!serviceUrl.EndsWith(
"/") && !serviceUrl.EndsWith(
"="))
100 serviceUrl = serviceUrl +
'/';
101 m_serverUrl = serviceUrl;
106 if (
String.IsNullOrEmpty(m_serverUrl))
107 m_log.Info(
"[SIMIAN ACCOUNT CONNECTOR]: No UserAccountServerURI specified, disabling connector");
112 NameValueCollection requestArgs =
new NameValueCollection
114 {
"RequestMethod",
"GetUser" },
115 {
"Name", firstName +
' ' + lastName }
118 return GetUser(requestArgs);
123 NameValueCollection requestArgs =
new NameValueCollection
125 {
"RequestMethod",
"GetUser" },
129 return GetUser(requestArgs);
136 if (m_accountCache.TryGetValue(userID, out account))
139 NameValueCollection requestArgs =
new NameValueCollection
141 {
"RequestMethod",
"GetUser" },
142 {
"UserID", userID.ToString() }
145 account = GetUser(requestArgs);
150 m_accountCache.AddOrUpdate(userID, null, CACHE_EXPIRATION_SECONDS);
158 List<UserAccount> accounts =
new List<UserAccount>();
162 NameValueCollection requestArgs =
new NameValueCollection
164 {
"RequestMethod",
"GetUsers" },
165 {
"NameQuery", query }
168 OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
169 if (response[
"Success"].AsBoolean())
172 if (array != null && array.Count > 0)
174 for (
int i = 0; i < array.Count; i++)
178 accounts.Add(account);
183 m_log.Warn(
"[SIMIAN ACCOUNT CONNECTOR]: Account search failed, response data was in an invalid format");
188 m_log.Warn(
"[SIMIAN ACCOUNT CONNECTOR]: Failed to search for account data by name " + query);
196 m_accountCache.Remove(userID);
208 NameValueCollection requestArgs =
new NameValueCollection
210 {
"RequestMethod",
"AddUser" },
211 {
"UserID", data.PrincipalID.ToString() },
212 {
"Name", data.Name },
213 {
"Email", data.Email },
214 {
"AccessLevel", data.UserLevel.ToString() }
217 OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
219 if (response[
"Success"].AsBoolean())
221 m_log.InfoFormat(
"[SIMIAN ACCOUNT CONNECTOR]: Storing user account data for " + data.Name);
223 requestArgs =
new NameValueCollection
225 {
"RequestMethod",
"AddUserData" },
226 {
"UserID", data.PrincipalID.ToString() },
227 {
"CreationDate", data.Created.ToString() },
228 {
"UserFlags", data.UserFlags.ToString() },
229 {
"UserTitle", data.UserTitle }
232 response = SimianGrid.PostToService(m_serverUrl, requestArgs);
233 bool success = response[
"Success"].AsBoolean();
238 m_accountCache.AddOrUpdate(data.PrincipalID, data, CACHE_EXPIRATION_SECONDS);
242 m_log.Warn(
"[SIMIAN ACCOUNT CONNECTOR]: Failed to store user account data for " + data.Name +
": " + response[
"Message"].AsString());
249 m_log.Warn(
"[SIMIAN ACCOUNT CONNECTOR]: Failed to store user account for " + data.Name +
": " + response[
"Message"].AsString());
260 private UserAccount GetUser(NameValueCollection requestArgs)
262 string lookupValue = (requestArgs.Count > 1) ? requestArgs[1] :
"(Unknown)";
265 OSDMap response = SimianGrid.PostToService(m_serverUrl, requestArgs);
266 if (response[
"Success"].AsBoolean())
270 return ResponseToUserAccount(user);
272 m_log.Warn(
"[SIMIAN ACCOUNT CONNECTOR]: Account search failed, response data was in an invalid format");
276 m_log.Warn(
"[SIMIAN ACCOUNT CONNECTOR]: Failed to lookup user account with query: " + lookupValue);
289 if (response == null)
293 account.PrincipalID = response[
"UserID"].AsUUID();
294 account.Created = response[
"CreationDate"].AsInteger();
295 account.Email = response[
"Email"].AsString();
296 account.ServiceURLs =
new Dictionary<string, object>(0);
297 account.UserFlags = response[
"UserFlags"].AsInteger();
298 account.UserLevel = response[
"AccessLevel"].AsInteger();
299 account.UserTitle = response[
"UserTitle"].AsString();
300 account.LocalToGrid =
true;
301 if (response.ContainsKey(
"LocalToGrid"))
302 account.
LocalToGrid = (response[
"LocalToGrid"].AsString() ==
"true" ?
true :
false);
304 GetFirstLastName(response[
"Name"].AsString(), out account.
FirstName, out account.
LastName);
307 m_accountCache.AddOrUpdate(account.PrincipalID, account, CACHE_EXPIRATION_SECONDS);
318 private static void GetFirstLastName(
string name, out
string firstName, out
string lastName)
320 if (
String.IsNullOrEmpty(name))
322 firstName = String.Empty;
323 lastName = String.Empty;
327 string[] names = name.Split(
' ');
329 if (names.Length == 2)
331 firstName = names[0];
336 firstName = String.Empty;
OpenMetaverse.StructuredData.OSDArray OSDArray
UserAccount GetUserAccount(UUID scopeID, string email)
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
bool StoreUserAccount(UserAccount data)
Store the data given, wich replaces the stored data, therefore must be complete.
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
OpenMetaverse.StructuredData.OSDMap OSDMap
SimianUserAccountServiceConnector(IConfigSource source)
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
void InvalidateCache(UUID userID)
SimianUserAccountServiceConnector()
Connects user account data (creating new users, looking up existing users) to the SimianGrid backend ...
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
UserAccount GetUserAccount(UUID scopeID, UUID userID)
List< UserAccount > GetUserAccounts(UUID scopeID, string query)
Returns the list of avatars that matches both the search criterion and the scope ID passed ...
List< UserAccount > GetUserAccountsWhere(UUID scopeID, string query)
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)