OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
UserAccountServicesConnector.cs
Go to the documentation of this file.
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 log4net;
29 using System;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Reflection;
33 using Nini.Config;
34 using OpenSim.Framework;
35 
36 using OpenSim.Framework.ServiceAuth;
37 using OpenSim.Server.Base;
38 using OpenSim.Services.Interfaces;
39 using OpenMetaverse;
40 
41 namespace OpenSim.Services.Connectors
42 {
44  {
45  private static readonly ILog m_log =
46  LogManager.GetLogger(
47  MethodBase.GetCurrentMethod().DeclaringType);
48 
49  private string m_ServerURI = String.Empty;
50 
52  {
53  }
54 
55  public UserAccountServicesConnector(string serverURI)
56  {
57  m_ServerURI = serverURI.TrimEnd('/');
58  }
59 
60  public UserAccountServicesConnector(IConfigSource source)
61  {
62  Initialise(source);
63  }
64 
65  public virtual void Initialise(IConfigSource source)
66  {
67  IConfig assetConfig = source.Configs["UserAccountService"];
68  if (assetConfig == null)
69  {
70  m_log.Error("[ACCOUNT CONNECTOR]: UserAccountService missing from OpenSim.ini");
71  throw new Exception("User account connector init error");
72  }
73 
74  string serviceURI = assetConfig.GetString("UserAccountServerURI",
75  String.Empty);
76 
77  if (serviceURI == String.Empty)
78  {
79  m_log.Error("[ACCOUNT CONNECTOR]: No Server URI named in section UserAccountService");
80  throw new Exception("User account connector init error");
81  }
82  m_ServerURI = serviceURI;
83 
84  base.Initialise(source, "UserAccountService");
85  }
86 
87  public virtual UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)
88  {
89  Dictionary<string, object> sendData = new Dictionary<string, object>();
90  //sendData["SCOPEID"] = scopeID.ToString();
91  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
92  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
93  sendData["METHOD"] = "getaccount";
94 
95  sendData["ScopeID"] = scopeID;
96  sendData["FirstName"] = firstName.ToString();
97  sendData["LastName"] = lastName.ToString();
98 
99  return SendAndGetReply(sendData);
100  }
101 
102  public virtual UserAccount GetUserAccount(UUID scopeID, string email)
103  {
104  Dictionary<string, object> sendData = new Dictionary<string, object>();
105  //sendData["SCOPEID"] = scopeID.ToString();
106  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
107  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
108  sendData["METHOD"] = "getaccount";
109 
110  sendData["ScopeID"] = scopeID;
111  sendData["Email"] = email;
112 
113  return SendAndGetReply(sendData);
114  }
115 
116  public virtual UserAccount GetUserAccount(UUID scopeID, UUID userID)
117  {
118  //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccount {0}", userID);
119  Dictionary<string, object> sendData = new Dictionary<string, object>();
120  //sendData["SCOPEID"] = scopeID.ToString();
121  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
122  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
123  sendData["METHOD"] = "getaccount";
124 
125  sendData["ScopeID"] = scopeID;
126  sendData["UserID"] = userID.ToString();
127 
128  return SendAndGetReply(sendData);
129  }
130 
131  public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
132  {
133  Dictionary<string, object> sendData = new Dictionary<string, object>();
134  //sendData["SCOPEID"] = scopeID.ToString();
135  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
136  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
137  sendData["METHOD"] = "getaccounts";
138 
139  sendData["ScopeID"] = scopeID.ToString();
140  sendData["query"] = query;
141 
142  string reply = string.Empty;
143  string reqString = ServerUtils.BuildQueryString(sendData);
144  string uri = m_ServerURI + "/accounts";
145  // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
146  try
147  {
148  reply = SynchronousRestFormsRequester.MakeRequest("POST",
149  uri,
150  reqString,
151  m_Auth);
152  if (reply == null || (reply != null && reply == string.Empty))
153  {
154  m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received null or empty reply");
155  return null;
156  }
157  }
158  catch (Exception e)
159  {
160  m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
161  }
162 
163  List<UserAccount> accounts = new List<UserAccount>();
164 
165  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
166 
167  if (replyData != null)
168  {
169  if (replyData.ContainsKey("result") && replyData["result"].ToString() == "null")
170  {
171  return accounts;
172  }
173 
174  Dictionary<string, object>.ValueCollection accountList = replyData.Values;
175  //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
176  foreach (object acc in accountList)
177  {
178  if (acc is Dictionary<string, object>)
179  {
180  UserAccount pinfo = new UserAccount((Dictionary<string, object>)acc);
181  accounts.Add(pinfo);
182  }
183  else
184  m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccounts received invalid response type {0}",
185  acc.GetType());
186  }
187  }
188  else
189  m_log.DebugFormat("[ACCOUNTS CONNECTOR]: GetUserAccounts received null response");
190 
191  return accounts;
192  }
193 
194  public void InvalidateCache(UUID userID)
195  {
196  }
197 
198  public List<UserAccount> GetUserAccountsWhere(UUID scopeID, string where)
199  {
200  return null; // Not implemented for regions
201  }
202 
203  public virtual bool StoreUserAccount(UserAccount data)
204  {
205  Dictionary<string, object> sendData = new Dictionary<string, object>();
206  //sendData["SCOPEID"] = scopeID.ToString();
207  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
208  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
209  sendData["METHOD"] = "setaccount";
210 
211  Dictionary<string, object> structData = data.ToKeyValuePairs();
212 
213  foreach (KeyValuePair<string, object> kvp in structData)
214  {
215  if (kvp.Value == null)
216  {
217  m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Null value for {0}", kvp.Key);
218  continue;
219  }
220  sendData[kvp.Key] = kvp.Value.ToString();
221  }
222 
223  if (SendAndGetReply(sendData) != null)
224  return true;
225  else
226  return false;
227  }
228 
238  public virtual UserAccount CreateUser(string first, string last, string password, string email, UUID scopeID)
239  {
240  Dictionary<string, object> sendData = new Dictionary<string, object>();
241  //sendData["SCOPEID"] = scopeID.ToString();
242  sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
243  sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
244  sendData["METHOD"] = "createuser";
245 
246  sendData["FirstName"] = first;
247  sendData["LastName"] = last;
248  sendData["Password"] = password;
249  if (!string.IsNullOrEmpty(email))
250  sendData["Email"] = first;
251  sendData["ScopeID"] = scopeID.ToString();
252 
253  return SendAndGetReply(sendData);
254  }
255 
256  private UserAccount SendAndGetReply(Dictionary<string, object> sendData)
257  {
258  string reply = string.Empty;
259  string reqString = ServerUtils.BuildQueryString(sendData);
260  string uri = m_ServerURI + "/accounts";
261  // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
262  try
263  {
264  reply = SynchronousRestFormsRequester.MakeRequest("POST",
265  uri,
266  reqString,
267  m_Auth);
268  if (reply == null || (reply != null && reply == string.Empty))
269  {
270  m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccount received null or empty reply");
271  return null;
272  }
273  }
274  catch (Exception e)
275  {
276  m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
277  }
278 
279  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
280  UserAccount account = null;
281 
282  if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
283  {
284  if (replyData["result"] is Dictionary<string, object>)
285  {
286  account = new UserAccount((Dictionary<string, object>)replyData["result"]);
287  }
288  }
289 
290  return account;
291 
292  }
293 
294  private bool SendAndGetBoolReply(Dictionary<string, object> sendData)
295  {
296  string reqString = ServerUtils.BuildQueryString(sendData);
297  string uri = m_ServerURI + "/accounts";
298  //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString);
299  try
300  {
301  string reply = SynchronousRestFormsRequester.MakeRequest("POST",
302  uri,
303  reqString,
304  m_Auth);
305  if (reply != string.Empty)
306  {
307  //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: reply = {0}", reply);
308  Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
309 
310  if (replyData.ContainsKey("result"))
311  {
312  if (replyData["result"].ToString().ToLower() == "success")
313  return true;
314  else
315  return false;
316  }
317  else
318  m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount reply data does not contain result field");
319 
320  }
321  else
322  m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount received empty reply");
323  }
324  catch (Exception e)
325  {
326  m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message);
327  }
328 
329  return false;
330  }
331 
332  }
333 }
List< UserAccount > GetUserAccountsWhere(UUID scopeID, string where)
virtual bool StoreUserAccount(UserAccount data)
Store the data given, wich replaces the stored data, therefore must be complete.
List< UserAccount > GetUserAccounts(UUID scopeID, string query)
Returns the list of avatars that matches both the search criterion and the scope ID passed ...
virtual UserAccount GetUserAccount(UUID scopeID, string firstName, string lastName)
virtual UserAccount GetUserAccount(UUID scopeID, string email)
virtual UserAccount GetUserAccount(UUID scopeID, UUID userID)
virtual UserAccount CreateUser(string first, string last, string password, string email, UUID scopeID)
Create user remotely. Note this this is not part of the IUserAccountsService