UserAccountService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using Nini.Config;
  31. using OpenSim.Data;
  32. using OpenSim.Services.Interfaces;
  33. using OpenSim.Framework.Console;
  34. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  35. using OpenMetaverse;
  36. using log4net;
  37. namespace OpenSim.Services.UserAccountService
  38. {
  39. public class UserAccountService : UserAccountServiceBase, IUserAccountService
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private static UserAccountService m_RootInstance;
  43. protected IGridService m_GridService;
  44. protected IAuthenticationService m_AuthenticationService;
  45. protected IGridUserService m_GridUserService;
  46. protected IInventoryService m_InventoryService;
  47. public UserAccountService(IConfigSource config)
  48. : base(config)
  49. {
  50. IConfig userConfig = config.Configs["UserAccountService"];
  51. if (userConfig == null)
  52. throw new Exception("No UserAccountService configuration");
  53. // In case there are several instances of this class in the same process,
  54. // the console commands are only registered for the root instance
  55. if (m_RootInstance == null)
  56. {
  57. m_RootInstance = this;
  58. string gridServiceDll = userConfig.GetString("GridService", string.Empty);
  59. if (gridServiceDll != string.Empty)
  60. m_GridService = LoadPlugin<IGridService>(gridServiceDll, new Object[] { config });
  61. string authServiceDll = userConfig.GetString("AuthenticationService", string.Empty);
  62. if (authServiceDll != string.Empty)
  63. m_AuthenticationService = LoadPlugin<IAuthenticationService>(authServiceDll, new Object[] { config });
  64. string presenceServiceDll = userConfig.GetString("GridUserService", string.Empty);
  65. if (presenceServiceDll != string.Empty)
  66. m_GridUserService = LoadPlugin<IGridUserService>(presenceServiceDll, new Object[] { config });
  67. string invServiceDll = userConfig.GetString("InventoryService", string.Empty);
  68. if (invServiceDll != string.Empty)
  69. m_InventoryService = LoadPlugin<IInventoryService>(invServiceDll, new Object[] { config });
  70. if (MainConsole.Instance != null)
  71. {
  72. MainConsole.Instance.Commands.AddCommand("UserService", false,
  73. "create user",
  74. "create user [<first> [<last> [<pass> [<email>]]]]",
  75. "Create a new user", HandleCreateUser);
  76. MainConsole.Instance.Commands.AddCommand("UserService", false, "reset user password",
  77. "reset user password [<first> [<last> [<password>]]]",
  78. "Reset a user password", HandleResetUserPassword);
  79. }
  80. }
  81. }
  82. #region IUserAccountService
  83. public UserAccount GetUserAccount(UUID scopeID, string firstName,
  84. string lastName)
  85. {
  86. // m_log.DebugFormat(
  87. // "[USER ACCOUNT SERVICE]: Retrieving account by username for {0} {1}, scope {2}",
  88. // firstName, lastName, scopeID);
  89. UserAccountData[] d;
  90. if (scopeID != UUID.Zero)
  91. {
  92. d = m_Database.Get(
  93. new string[] { "ScopeID", "FirstName", "LastName" },
  94. new string[] { scopeID.ToString(), firstName, lastName });
  95. if (d.Length < 1)
  96. {
  97. d = m_Database.Get(
  98. new string[] { "ScopeID", "FirstName", "LastName" },
  99. new string[] { UUID.Zero.ToString(), firstName, lastName });
  100. }
  101. }
  102. else
  103. {
  104. d = m_Database.Get(
  105. new string[] { "FirstName", "LastName" },
  106. new string[] { firstName, lastName });
  107. }
  108. if (d.Length < 1)
  109. return null;
  110. return MakeUserAccount(d[0]);
  111. }
  112. private UserAccount MakeUserAccount(UserAccountData d)
  113. {
  114. UserAccount u = new UserAccount();
  115. u.FirstName = d.FirstName;
  116. u.LastName = d.LastName;
  117. u.PrincipalID = d.PrincipalID;
  118. u.ScopeID = d.ScopeID;
  119. if (d.Data.ContainsKey("Email") && d.Data["Email"] != null)
  120. u.Email = d.Data["Email"].ToString();
  121. else
  122. u.Email = string.Empty;
  123. u.Created = Convert.ToInt32(d.Data["Created"].ToString());
  124. if (d.Data.ContainsKey("UserTitle") && d.Data["UserTitle"] != null)
  125. u.UserTitle = d.Data["UserTitle"].ToString();
  126. else
  127. u.UserTitle = string.Empty;
  128. if (d.Data.ContainsKey("UserLevel") && d.Data["UserLevel"] != null)
  129. Int32.TryParse(d.Data["UserLevel"], out u.UserLevel);
  130. if (d.Data.ContainsKey("UserFlags") && d.Data["UserFlags"] != null)
  131. Int32.TryParse(d.Data["UserFlags"], out u.UserFlags);
  132. if (d.Data.ContainsKey("ServiceURLs") && d.Data["ServiceURLs"] != null)
  133. {
  134. string[] URLs = d.Data["ServiceURLs"].ToString().Split(new char[] { ' ' });
  135. u.ServiceURLs = new Dictionary<string, object>();
  136. foreach (string url in URLs)
  137. {
  138. string[] parts = url.Split(new char[] { '=' });
  139. if (parts.Length != 2)
  140. continue;
  141. string name = System.Web.HttpUtility.UrlDecode(parts[0]);
  142. string val = System.Web.HttpUtility.UrlDecode(parts[1]);
  143. u.ServiceURLs[name] = val;
  144. }
  145. }
  146. else
  147. u.ServiceURLs = new Dictionary<string, object>();
  148. return u;
  149. }
  150. public UserAccount GetUserAccount(UUID scopeID, string email)
  151. {
  152. UserAccountData[] d;
  153. if (scopeID != UUID.Zero)
  154. {
  155. d = m_Database.Get(
  156. new string[] { "ScopeID", "Email" },
  157. new string[] { scopeID.ToString(), email });
  158. if (d.Length < 1)
  159. {
  160. d = m_Database.Get(
  161. new string[] { "ScopeID", "Email" },
  162. new string[] { UUID.Zero.ToString(), email });
  163. }
  164. }
  165. else
  166. {
  167. d = m_Database.Get(
  168. new string[] { "Email" },
  169. new string[] { email });
  170. }
  171. if (d.Length < 1)
  172. return null;
  173. return MakeUserAccount(d[0]);
  174. }
  175. public UserAccount GetUserAccount(UUID scopeID, UUID principalID)
  176. {
  177. UserAccountData[] d;
  178. if (scopeID != UUID.Zero)
  179. {
  180. d = m_Database.Get(
  181. new string[] { "ScopeID", "PrincipalID" },
  182. new string[] { scopeID.ToString(), principalID.ToString() });
  183. if (d.Length < 1)
  184. {
  185. d = m_Database.Get(
  186. new string[] { "ScopeID", "PrincipalID" },
  187. new string[] { UUID.Zero.ToString(), principalID.ToString() });
  188. }
  189. }
  190. else
  191. {
  192. d = m_Database.Get(
  193. new string[] { "PrincipalID" },
  194. new string[] { principalID.ToString() });
  195. }
  196. if (d.Length < 1)
  197. {
  198. return null;
  199. }
  200. return MakeUserAccount(d[0]);
  201. }
  202. public bool StoreUserAccount(UserAccount data)
  203. {
  204. // m_log.DebugFormat(
  205. // "[USER ACCOUNT SERVICE]: Storing user account for {0} {1} {2}, scope {3}",
  206. // data.FirstName, data.LastName, data.PrincipalID, data.ScopeID);
  207. UserAccountData d = new UserAccountData();
  208. d.FirstName = data.FirstName;
  209. d.LastName = data.LastName;
  210. d.PrincipalID = data.PrincipalID;
  211. d.ScopeID = data.ScopeID;
  212. d.Data = new Dictionary<string, string>();
  213. d.Data["Email"] = data.Email;
  214. d.Data["Created"] = data.Created.ToString();
  215. d.Data["UserLevel"] = data.UserLevel.ToString();
  216. d.Data["UserFlags"] = data.UserFlags.ToString();
  217. if (data.UserTitle != null)
  218. d.Data["UserTitle"] = data.UserTitle.ToString();
  219. List<string> parts = new List<string>();
  220. foreach (KeyValuePair<string, object> kvp in data.ServiceURLs)
  221. {
  222. string key = System.Web.HttpUtility.UrlEncode(kvp.Key);
  223. string val = System.Web.HttpUtility.UrlEncode(kvp.Value.ToString());
  224. parts.Add(key + "=" + val);
  225. }
  226. d.Data["ServiceURLs"] = string.Join(" ", parts.ToArray());
  227. return m_Database.Store(d);
  228. }
  229. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  230. {
  231. UserAccountData[] d = m_Database.GetUsers(scopeID, query);
  232. if (d == null)
  233. return new List<UserAccount>();
  234. List<UserAccount> ret = new List<UserAccount>();
  235. foreach (UserAccountData data in d)
  236. ret.Add(MakeUserAccount(data));
  237. return ret;
  238. }
  239. #endregion
  240. #region Console commands
  241. /// <summary>
  242. /// Handle the create user command from the console.
  243. /// </summary>
  244. /// <param name="cmdparams">string array with parameters: firstname, lastname, password, locationX, locationY, email</param>
  245. protected void HandleCreateUser(string module, string[] cmdparams)
  246. {
  247. string firstName;
  248. string lastName;
  249. string password;
  250. string email;
  251. List<char> excluded = new List<char>(new char[]{' '});
  252. if (cmdparams.Length < 3)
  253. firstName = MainConsole.Instance.CmdPrompt("First name", "Default", excluded);
  254. else firstName = cmdparams[2];
  255. if (cmdparams.Length < 4)
  256. lastName = MainConsole.Instance.CmdPrompt("Last name", "User", excluded);
  257. else lastName = cmdparams[3];
  258. if (cmdparams.Length < 5)
  259. password = MainConsole.Instance.PasswdPrompt("Password");
  260. else password = cmdparams[4];
  261. if (cmdparams.Length < 6)
  262. email = MainConsole.Instance.CmdPrompt("Email", "");
  263. else email = cmdparams[5];
  264. CreateUser(firstName, lastName, password, email);
  265. }
  266. protected void HandleResetUserPassword(string module, string[] cmdparams)
  267. {
  268. string firstName;
  269. string lastName;
  270. string newPassword;
  271. if (cmdparams.Length < 4)
  272. firstName = MainConsole.Instance.CmdPrompt("First name");
  273. else firstName = cmdparams[3];
  274. if (cmdparams.Length < 5)
  275. lastName = MainConsole.Instance.CmdPrompt("Last name");
  276. else lastName = cmdparams[4];
  277. if (cmdparams.Length < 6)
  278. newPassword = MainConsole.Instance.PasswdPrompt("New password");
  279. else newPassword = cmdparams[5];
  280. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  281. if (account == null)
  282. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: No such user");
  283. bool success = false;
  284. if (m_AuthenticationService != null)
  285. success = m_AuthenticationService.SetPassword(account.PrincipalID, newPassword);
  286. if (!success)
  287. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Unable to reset password for account {0} {1}.",
  288. firstName, lastName);
  289. else
  290. m_log.InfoFormat("[USER ACCOUNT SERVICE]: Password reset for user {0} {1}", firstName, lastName);
  291. }
  292. #endregion
  293. /// <summary>
  294. /// Create a user
  295. /// </summary>
  296. /// <param name="firstName"></param>
  297. /// <param name="lastName"></param>
  298. /// <param name="password"></param>
  299. /// <param name="email"></param>
  300. private void CreateUser(string firstName, string lastName, string password, string email)
  301. {
  302. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  303. if (null == account)
  304. {
  305. account = new UserAccount(UUID.Zero, firstName, lastName, email);
  306. if (account.ServiceURLs == null || (account.ServiceURLs != null && account.ServiceURLs.Count == 0))
  307. {
  308. account.ServiceURLs = new Dictionary<string, object>();
  309. account.ServiceURLs["HomeURI"] = string.Empty;
  310. account.ServiceURLs["GatekeeperURI"] = string.Empty;
  311. account.ServiceURLs["InventoryServerURI"] = string.Empty;
  312. account.ServiceURLs["AssetServerURI"] = string.Empty;
  313. }
  314. if (StoreUserAccount(account))
  315. {
  316. bool success;
  317. if (m_AuthenticationService != null)
  318. {
  319. success = m_AuthenticationService.SetPassword(account.PrincipalID, password);
  320. if (!success)
  321. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set password for account {0} {1}.",
  322. firstName, lastName);
  323. }
  324. GridRegion home = null;
  325. if (m_GridService != null)
  326. {
  327. List<GridRegion> defaultRegions = m_GridService.GetDefaultRegions(UUID.Zero);
  328. if (defaultRegions != null && defaultRegions.Count >= 1)
  329. home = defaultRegions[0];
  330. if (m_GridUserService != null && home != null)
  331. m_GridUserService.SetHome(account.PrincipalID.ToString(), home.RegionID, new Vector3(128, 128, 0), new Vector3(0, 1, 0));
  332. else
  333. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set home for account {0} {1}.",
  334. firstName, lastName);
  335. }
  336. else
  337. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to retrieve home region for account {0} {1}.",
  338. firstName, lastName);
  339. if (m_InventoryService != null)
  340. {
  341. success = m_InventoryService.CreateUserInventory(account.PrincipalID);
  342. if (!success)
  343. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to create inventory for account {0} {1}.",
  344. firstName, lastName);
  345. }
  346. m_log.InfoFormat("[USER ACCOUNT SERVICE]: Account {0} {1} created successfully", firstName, lastName);
  347. } else {
  348. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Account creation failed for account {0} {1}", firstName, lastName);
  349. }
  350. }
  351. else
  352. {
  353. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: A user with the name {0} {1} already exists!", firstName, lastName);
  354. }
  355. }
  356. }
  357. }