diff options
author | UbitUmarov | 2015-09-01 11:43:07 +0100 |
---|---|---|
committer | UbitUmarov | 2015-09-01 11:43:07 +0100 |
commit | fb78b182520fc9bb0f971afd0322029c70278ea6 (patch) | |
tree | b4e30d383938fdeef8c92d1d1c2f44bb61d329bd /OpenSim/Services/Connectors/UserAccounts/UserAccountServicesConnector.cs | |
parent | lixo (diff) | |
parent | Mantis #7713: fixed bug introduced by 1st MOSES patch. (diff) | |
download | opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.zip opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.gz opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.bz2 opensim-SC-fb78b182520fc9bb0f971afd0322029c70278ea6.tar.xz |
Merge remote-tracking branch 'os/master'
Diffstat (limited to 'OpenSim/Services/Connectors/UserAccounts/UserAccountServicesConnector.cs')
-rw-r--r-- | OpenSim/Services/Connectors/UserAccounts/UserAccountServicesConnector.cs | 328 |
1 files changed, 328 insertions, 0 deletions
diff --git a/OpenSim/Services/Connectors/UserAccounts/UserAccountServicesConnector.cs b/OpenSim/Services/Connectors/UserAccounts/UserAccountServicesConnector.cs new file mode 100644 index 0000000..560e6c4 --- /dev/null +++ b/OpenSim/Services/Connectors/UserAccounts/UserAccountServicesConnector.cs | |||
@@ -0,0 +1,328 @@ | |||
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 | using OpenSim.Framework.Communications; | ||
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 | { | ||
43 | public class UserAccountServicesConnector : BaseServiceConnector, IUserAccountService | ||
44 | { | ||
45 | private static readonly ILog m_log = | ||
46 | LogManager.GetLogger( | ||
47 | MethodBase.GetCurrentMethod().DeclaringType); | ||
48 | |||
49 | private string m_ServerURI = String.Empty; | ||
50 | |||
51 | public UserAccountServicesConnector() | ||
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 virtual bool StoreUserAccount(UserAccount data) | ||
199 | { | ||
200 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
201 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
202 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
203 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
204 | sendData["METHOD"] = "setaccount"; | ||
205 | |||
206 | Dictionary<string, object> structData = data.ToKeyValuePairs(); | ||
207 | |||
208 | foreach (KeyValuePair<string, object> kvp in structData) | ||
209 | { | ||
210 | if (kvp.Value == null) | ||
211 | { | ||
212 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Null value for {0}", kvp.Key); | ||
213 | continue; | ||
214 | } | ||
215 | sendData[kvp.Key] = kvp.Value.ToString(); | ||
216 | } | ||
217 | |||
218 | if (SendAndGetReply(sendData) != null) | ||
219 | return true; | ||
220 | else | ||
221 | return false; | ||
222 | } | ||
223 | |||
224 | /// <summary> | ||
225 | /// Create user remotely. Note this this is not part of the IUserAccountsService | ||
226 | /// </summary> | ||
227 | /// <param name="first"></param> | ||
228 | /// <param name="last"></param> | ||
229 | /// <param name="password"></param> | ||
230 | /// <param name="email"></param> | ||
231 | /// <param name="scopeID"></param> | ||
232 | /// <returns></returns> | ||
233 | public virtual UserAccount CreateUser(string first, string last, string password, string email, UUID scopeID) | ||
234 | { | ||
235 | Dictionary<string, object> sendData = new Dictionary<string, object>(); | ||
236 | //sendData["SCOPEID"] = scopeID.ToString(); | ||
237 | sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); | ||
238 | sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); | ||
239 | sendData["METHOD"] = "createuser"; | ||
240 | |||
241 | sendData["FirstName"] = first; | ||
242 | sendData["LastName"] = last; | ||
243 | sendData["Password"] = password; | ||
244 | if (!string.IsNullOrEmpty(email)) | ||
245 | sendData["Email"] = first; | ||
246 | sendData["ScopeID"] = scopeID.ToString(); | ||
247 | |||
248 | return SendAndGetReply(sendData); | ||
249 | } | ||
250 | |||
251 | private UserAccount SendAndGetReply(Dictionary<string, object> sendData) | ||
252 | { | ||
253 | string reply = string.Empty; | ||
254 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
255 | string uri = m_ServerURI + "/accounts"; | ||
256 | // m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString); | ||
257 | try | ||
258 | { | ||
259 | reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
260 | uri, | ||
261 | reqString, | ||
262 | m_Auth); | ||
263 | if (reply == null || (reply != null && reply == string.Empty)) | ||
264 | { | ||
265 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: GetUserAccount received null or empty reply"); | ||
266 | return null; | ||
267 | } | ||
268 | } | ||
269 | catch (Exception e) | ||
270 | { | ||
271 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message); | ||
272 | } | ||
273 | |||
274 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
275 | UserAccount account = null; | ||
276 | |||
277 | if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null)) | ||
278 | { | ||
279 | if (replyData["result"] is Dictionary<string, object>) | ||
280 | { | ||
281 | account = new UserAccount((Dictionary<string, object>)replyData["result"]); | ||
282 | } | ||
283 | } | ||
284 | |||
285 | return account; | ||
286 | |||
287 | } | ||
288 | |||
289 | private bool SendAndGetBoolReply(Dictionary<string, object> sendData) | ||
290 | { | ||
291 | string reqString = ServerUtils.BuildQueryString(sendData); | ||
292 | string uri = m_ServerURI + "/accounts"; | ||
293 | //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: queryString = {0}", reqString); | ||
294 | try | ||
295 | { | ||
296 | string reply = SynchronousRestFormsRequester.MakeRequest("POST", | ||
297 | uri, | ||
298 | reqString, | ||
299 | m_Auth); | ||
300 | if (reply != string.Empty) | ||
301 | { | ||
302 | //m_log.DebugFormat("[ACCOUNTS CONNECTOR]: reply = {0}", reply); | ||
303 | Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply); | ||
304 | |||
305 | if (replyData.ContainsKey("result")) | ||
306 | { | ||
307 | if (replyData["result"].ToString().ToLower() == "success") | ||
308 | return true; | ||
309 | else | ||
310 | return false; | ||
311 | } | ||
312 | else | ||
313 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount reply data does not contain result field"); | ||
314 | |||
315 | } | ||
316 | else | ||
317 | m_log.DebugFormat("[ACCOUNTS CONNECTOR]: Set or Create UserAccount received empty reply"); | ||
318 | } | ||
319 | catch (Exception e) | ||
320 | { | ||
321 | m_log.DebugFormat("[ACCOUNT CONNECTOR]: Exception when contacting user accounts server at {0}: {1}", uri, e.Message); | ||
322 | } | ||
323 | |||
324 | return false; | ||
325 | } | ||
326 | |||
327 | } | ||
328 | } | ||