UserAccountService.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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,
  77. "reset user password",
  78. "reset user password [<first> [<last> [<password>]]]",
  79. "Reset a user password", HandleResetUserPassword);
  80. MainConsole.Instance.Commands.AddCommand("UserService", false,
  81. "set user level",
  82. "set user level [<first> [<last> [<level>]]]",
  83. "Set user level. If >= 200 and 'allow_grid_gods = true' in OpenSim.ini, "
  84. + "this account will be treated as god-moded. "
  85. + "It will also affect the 'login level' command. ",
  86. HandleSetUserLevel);
  87. MainConsole.Instance.Commands.AddCommand("UserService", false,
  88. "show account",
  89. "show account <first> <last>",
  90. "Show account details for the given user", HandleShowAccount);
  91. }
  92. }
  93. }
  94. #region IUserAccountService
  95. public UserAccount GetUserAccount(UUID scopeID, string firstName,
  96. string lastName)
  97. {
  98. // m_log.DebugFormat(
  99. // "[USER ACCOUNT SERVICE]: Retrieving account by username for {0} {1}, scope {2}",
  100. // firstName, lastName, scopeID);
  101. UserAccountData[] d;
  102. if (scopeID != UUID.Zero)
  103. {
  104. d = m_Database.Get(
  105. new string[] { "ScopeID", "FirstName", "LastName" },
  106. new string[] { scopeID.ToString(), firstName, lastName });
  107. if (d.Length < 1)
  108. {
  109. d = m_Database.Get(
  110. new string[] { "ScopeID", "FirstName", "LastName" },
  111. new string[] { UUID.Zero.ToString(), firstName, lastName });
  112. }
  113. }
  114. else
  115. {
  116. d = m_Database.Get(
  117. new string[] { "FirstName", "LastName" },
  118. new string[] { firstName, lastName });
  119. }
  120. if (d.Length < 1)
  121. return null;
  122. return MakeUserAccount(d[0]);
  123. }
  124. private UserAccount MakeUserAccount(UserAccountData d)
  125. {
  126. UserAccount u = new UserAccount();
  127. u.FirstName = d.FirstName;
  128. u.LastName = d.LastName;
  129. u.PrincipalID = d.PrincipalID;
  130. u.ScopeID = d.ScopeID;
  131. if (d.Data.ContainsKey("Email") && d.Data["Email"] != null)
  132. u.Email = d.Data["Email"].ToString();
  133. else
  134. u.Email = string.Empty;
  135. u.Created = Convert.ToInt32(d.Data["Created"].ToString());
  136. if (d.Data.ContainsKey("UserTitle") && d.Data["UserTitle"] != null)
  137. u.UserTitle = d.Data["UserTitle"].ToString();
  138. else
  139. u.UserTitle = string.Empty;
  140. if (d.Data.ContainsKey("UserLevel") && d.Data["UserLevel"] != null)
  141. Int32.TryParse(d.Data["UserLevel"], out u.UserLevel);
  142. if (d.Data.ContainsKey("UserFlags") && d.Data["UserFlags"] != null)
  143. Int32.TryParse(d.Data["UserFlags"], out u.UserFlags);
  144. if (d.Data.ContainsKey("ServiceURLs") && d.Data["ServiceURLs"] != null)
  145. {
  146. string[] URLs = d.Data["ServiceURLs"].ToString().Split(new char[] { ' ' });
  147. u.ServiceURLs = new Dictionary<string, object>();
  148. foreach (string url in URLs)
  149. {
  150. string[] parts = url.Split(new char[] { '=' });
  151. if (parts.Length != 2)
  152. continue;
  153. string name = System.Web.HttpUtility.UrlDecode(parts[0]);
  154. string val = System.Web.HttpUtility.UrlDecode(parts[1]);
  155. u.ServiceURLs[name] = val;
  156. }
  157. }
  158. else
  159. u.ServiceURLs = new Dictionary<string, object>();
  160. return u;
  161. }
  162. public UserAccount GetUserAccount(UUID scopeID, string email)
  163. {
  164. UserAccountData[] d;
  165. if (scopeID != UUID.Zero)
  166. {
  167. d = m_Database.Get(
  168. new string[] { "ScopeID", "Email" },
  169. new string[] { scopeID.ToString(), email });
  170. if (d.Length < 1)
  171. {
  172. d = m_Database.Get(
  173. new string[] { "ScopeID", "Email" },
  174. new string[] { UUID.Zero.ToString(), email });
  175. }
  176. }
  177. else
  178. {
  179. d = m_Database.Get(
  180. new string[] { "Email" },
  181. new string[] { email });
  182. }
  183. if (d.Length < 1)
  184. return null;
  185. return MakeUserAccount(d[0]);
  186. }
  187. public UserAccount GetUserAccount(UUID scopeID, UUID principalID)
  188. {
  189. UserAccountData[] d;
  190. if (scopeID != UUID.Zero)
  191. {
  192. d = m_Database.Get(
  193. new string[] { "ScopeID", "PrincipalID" },
  194. new string[] { scopeID.ToString(), principalID.ToString() });
  195. if (d.Length < 1)
  196. {
  197. d = m_Database.Get(
  198. new string[] { "ScopeID", "PrincipalID" },
  199. new string[] { UUID.Zero.ToString(), principalID.ToString() });
  200. }
  201. }
  202. else
  203. {
  204. d = m_Database.Get(
  205. new string[] { "PrincipalID" },
  206. new string[] { principalID.ToString() });
  207. }
  208. if (d.Length < 1)
  209. {
  210. return null;
  211. }
  212. return MakeUserAccount(d[0]);
  213. }
  214. public bool StoreUserAccount(UserAccount data)
  215. {
  216. // m_log.DebugFormat(
  217. // "[USER ACCOUNT SERVICE]: Storing user account for {0} {1} {2}, scope {3}",
  218. // data.FirstName, data.LastName, data.PrincipalID, data.ScopeID);
  219. UserAccountData d = new UserAccountData();
  220. d.FirstName = data.FirstName;
  221. d.LastName = data.LastName;
  222. d.PrincipalID = data.PrincipalID;
  223. d.ScopeID = data.ScopeID;
  224. d.Data = new Dictionary<string, string>();
  225. d.Data["Email"] = data.Email;
  226. d.Data["Created"] = data.Created.ToString();
  227. d.Data["UserLevel"] = data.UserLevel.ToString();
  228. d.Data["UserFlags"] = data.UserFlags.ToString();
  229. if (data.UserTitle != null)
  230. d.Data["UserTitle"] = data.UserTitle.ToString();
  231. List<string> parts = new List<string>();
  232. foreach (KeyValuePair<string, object> kvp in data.ServiceURLs)
  233. {
  234. string key = System.Web.HttpUtility.UrlEncode(kvp.Key);
  235. string val = System.Web.HttpUtility.UrlEncode(kvp.Value.ToString());
  236. parts.Add(key + "=" + val);
  237. }
  238. d.Data["ServiceURLs"] = string.Join(" ", parts.ToArray());
  239. return m_Database.Store(d);
  240. }
  241. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  242. {
  243. UserAccountData[] d = m_Database.GetUsers(scopeID, query);
  244. if (d == null)
  245. return new List<UserAccount>();
  246. List<UserAccount> ret = new List<UserAccount>();
  247. foreach (UserAccountData data in d)
  248. ret.Add(MakeUserAccount(data));
  249. return ret;
  250. }
  251. #endregion
  252. #region Console commands
  253. /// <summary>
  254. /// Handle the create user command from the console.
  255. /// </summary>
  256. /// <param name="cmdparams">string array with parameters: firstname, lastname, password, locationX, locationY, email</param>
  257. protected void HandleCreateUser(string module, string[] cmdparams)
  258. {
  259. string firstName;
  260. string lastName;
  261. string password;
  262. string email;
  263. List<char> excluded = new List<char>(new char[]{' '});
  264. if (cmdparams.Length < 3)
  265. firstName = MainConsole.Instance.CmdPrompt("First name", "Default", excluded);
  266. else firstName = cmdparams[2];
  267. if (cmdparams.Length < 4)
  268. lastName = MainConsole.Instance.CmdPrompt("Last name", "User", excluded);
  269. else lastName = cmdparams[3];
  270. if (cmdparams.Length < 5)
  271. password = MainConsole.Instance.PasswdPrompt("Password");
  272. else password = cmdparams[4];
  273. if (cmdparams.Length < 6)
  274. email = MainConsole.Instance.CmdPrompt("Email", "");
  275. else email = cmdparams[5];
  276. CreateUser(firstName, lastName, password, email);
  277. }
  278. protected void HandleShowAccount(string module, string[] cmdparams)
  279. {
  280. if (cmdparams.Length != 4)
  281. {
  282. MainConsole.Instance.Output("Usage: show account <first-name> <last-name>");
  283. return;
  284. }
  285. string firstName = cmdparams[2];
  286. string lastName = cmdparams[3];
  287. UserAccount ua = GetUserAccount(UUID.Zero, firstName, lastName);
  288. if (ua == null)
  289. {
  290. MainConsole.Instance.OutputFormat("No user named {0} {1}", firstName, lastName);
  291. return;
  292. }
  293. MainConsole.Instance.OutputFormat("Name: {0}", ua.Name);
  294. MainConsole.Instance.OutputFormat("ID: {0}", ua.PrincipalID);
  295. MainConsole.Instance.OutputFormat("Title: {0}", ua.UserTitle);
  296. MainConsole.Instance.OutputFormat("E-mail: {0}", ua.Email);
  297. MainConsole.Instance.OutputFormat("Created: {0}", Utils.UnixTimeToDateTime(ua.Created));
  298. MainConsole.Instance.OutputFormat("Level: {0}", ua.UserLevel);
  299. MainConsole.Instance.OutputFormat("Flags: {0}", ua.UserFlags);
  300. foreach (KeyValuePair<string, Object> kvp in ua.ServiceURLs)
  301. MainConsole.Instance.OutputFormat("{0}: {1}", kvp.Key, kvp.Value);
  302. }
  303. protected void HandleResetUserPassword(string module, string[] cmdparams)
  304. {
  305. string firstName;
  306. string lastName;
  307. string newPassword;
  308. if (cmdparams.Length < 4)
  309. firstName = MainConsole.Instance.CmdPrompt("First name");
  310. else firstName = cmdparams[3];
  311. if (cmdparams.Length < 5)
  312. lastName = MainConsole.Instance.CmdPrompt("Last name");
  313. else lastName = cmdparams[4];
  314. if (cmdparams.Length < 6)
  315. newPassword = MainConsole.Instance.PasswdPrompt("New password");
  316. else newPassword = cmdparams[5];
  317. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  318. if (account == null)
  319. {
  320. MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName);
  321. return;
  322. }
  323. bool success = false;
  324. if (m_AuthenticationService != null)
  325. success = m_AuthenticationService.SetPassword(account.PrincipalID, newPassword);
  326. if (!success)
  327. MainConsole.Instance.OutputFormat("Unable to reset password for account {0} {1}.", firstName, lastName);
  328. else
  329. MainConsole.Instance.OutputFormat("Password reset for user {0} {1}", firstName, lastName);
  330. }
  331. protected void HandleSetUserLevel(string module, string[] cmdparams)
  332. {
  333. string firstName;
  334. string lastName;
  335. string rawLevel;
  336. int level;
  337. if (cmdparams.Length < 4)
  338. firstName = MainConsole.Instance.CmdPrompt("First name");
  339. else firstName = cmdparams[3];
  340. if (cmdparams.Length < 5)
  341. lastName = MainConsole.Instance.CmdPrompt("Last name");
  342. else lastName = cmdparams[4];
  343. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  344. if (account == null) {
  345. MainConsole.Instance.OutputFormat("No such user");
  346. return;
  347. }
  348. if (cmdparams.Length < 6)
  349. rawLevel = MainConsole.Instance.CmdPrompt("User level");
  350. else rawLevel = cmdparams[5];
  351. if(int.TryParse(rawLevel, out level) == false) {
  352. MainConsole.Instance.OutputFormat("Invalid user level");
  353. return;
  354. }
  355. account.UserLevel = level;
  356. bool success = StoreUserAccount(account);
  357. if (!success)
  358. MainConsole.Instance.OutputFormat("Unable to set user level for account {0} {1}.", firstName, lastName);
  359. else
  360. MainConsole.Instance.OutputFormat("User level set for user {0} {1} to {2}", firstName, lastName, level);
  361. }
  362. #endregion
  363. /// <summary>
  364. /// Create a user
  365. /// </summary>
  366. /// <param name="firstName"></param>
  367. /// <param name="lastName"></param>
  368. /// <param name="password"></param>
  369. /// <param name="email"></param>
  370. private void CreateUser(string firstName, string lastName, string password, string email)
  371. {
  372. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  373. if (null == account)
  374. {
  375. account = new UserAccount(UUID.Zero, firstName, lastName, email);
  376. if (account.ServiceURLs == null || (account.ServiceURLs != null && account.ServiceURLs.Count == 0))
  377. {
  378. account.ServiceURLs = new Dictionary<string, object>();
  379. account.ServiceURLs["HomeURI"] = string.Empty;
  380. account.ServiceURLs["GatekeeperURI"] = string.Empty;
  381. account.ServiceURLs["InventoryServerURI"] = string.Empty;
  382. account.ServiceURLs["AssetServerURI"] = string.Empty;
  383. }
  384. if (StoreUserAccount(account))
  385. {
  386. bool success;
  387. if (m_AuthenticationService != null)
  388. {
  389. success = m_AuthenticationService.SetPassword(account.PrincipalID, password);
  390. if (!success)
  391. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set password for account {0} {1}.",
  392. firstName, lastName);
  393. }
  394. GridRegion home = null;
  395. if (m_GridService != null)
  396. {
  397. List<GridRegion> defaultRegions = m_GridService.GetDefaultRegions(UUID.Zero);
  398. if (defaultRegions != null && defaultRegions.Count >= 1)
  399. home = defaultRegions[0];
  400. if (m_GridUserService != null && home != null)
  401. m_GridUserService.SetHome(account.PrincipalID.ToString(), home.RegionID, new Vector3(128, 128, 0), new Vector3(0, 1, 0));
  402. else
  403. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set home for account {0} {1}.",
  404. firstName, lastName);
  405. }
  406. else
  407. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to retrieve home region for account {0} {1}.",
  408. firstName, lastName);
  409. if (m_InventoryService != null)
  410. {
  411. success = m_InventoryService.CreateUserInventory(account.PrincipalID);
  412. if (!success)
  413. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to create inventory for account {0} {1}.",
  414. firstName, lastName);
  415. }
  416. m_log.InfoFormat("[USER ACCOUNT SERVICE]: Account {0} {1} created successfully", firstName, lastName);
  417. } else {
  418. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Account creation failed for account {0} {1}", firstName, lastName);
  419. }
  420. }
  421. else
  422. {
  423. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: A user with the name {0} {1} already exists!", firstName, lastName);
  424. }
  425. }
  426. }
  427. }