UserAccountService.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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 log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Data;
  34. using OpenSim.Framework;
  35. using OpenSim.Services.Interfaces;
  36. using OpenSim.Framework.Console;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using PermissionMask = OpenSim.Framework.PermissionMask;
  39. namespace OpenSim.Services.UserAccountService
  40. {
  41. public class UserAccountService : UserAccountServiceBase, IUserAccountService
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. private static UserAccountService m_RootInstance;
  45. /// <summary>
  46. /// Should we create default entries (minimum body parts/clothing, avatar wearable entries) for a new avatar?
  47. /// </summary>
  48. private bool m_CreateDefaultAvatarEntries;
  49. protected IGridService m_GridService;
  50. protected IAuthenticationService m_AuthenticationService;
  51. protected IGridUserService m_GridUserService;
  52. protected IInventoryService m_InventoryService;
  53. protected IAvatarService m_AvatarService;
  54. public UserAccountService(IConfigSource config)
  55. : base(config)
  56. {
  57. IConfig userConfig = config.Configs["UserAccountService"];
  58. if (userConfig == null)
  59. throw new Exception("No UserAccountService configuration");
  60. string gridServiceDll = userConfig.GetString("GridService", string.Empty);
  61. if (gridServiceDll != string.Empty)
  62. m_GridService = LoadPlugin<IGridService>(gridServiceDll, new Object[] { config });
  63. string authServiceDll = userConfig.GetString("AuthenticationService", string.Empty);
  64. if (authServiceDll != string.Empty)
  65. m_AuthenticationService = LoadPlugin<IAuthenticationService>(authServiceDll, new Object[] { config });
  66. string presenceServiceDll = userConfig.GetString("GridUserService", string.Empty);
  67. if (presenceServiceDll != string.Empty)
  68. m_GridUserService = LoadPlugin<IGridUserService>(presenceServiceDll, new Object[] { config });
  69. string invServiceDll = userConfig.GetString("InventoryService", string.Empty);
  70. if (invServiceDll != string.Empty)
  71. m_InventoryService = LoadPlugin<IInventoryService>(invServiceDll, new Object[] { config });
  72. string avatarServiceDll = userConfig.GetString("AvatarService", string.Empty);
  73. if (avatarServiceDll != string.Empty)
  74. m_AvatarService = LoadPlugin<IAvatarService>(avatarServiceDll, new Object[] { config });
  75. m_CreateDefaultAvatarEntries = userConfig.GetBoolean("CreateDefaultAvatarEntries", false);
  76. // In case there are several instances of this class in the same process,
  77. // the console commands are only registered for the root instance
  78. if (m_RootInstance == null && MainConsole.Instance != null)
  79. {
  80. m_RootInstance = this;
  81. MainConsole.Instance.Commands.AddCommand("Users", false,
  82. "create user",
  83. "create user [<first> [<last> [<pass> [<email> [<user id> [<model>]]]]]]",
  84. "Create a new user", HandleCreateUser);
  85. MainConsole.Instance.Commands.AddCommand("Users", false,
  86. "reset user password",
  87. "reset user password [<first> [<last> [<password>]]]",
  88. "Reset a user password", HandleResetUserPassword);
  89. MainConsole.Instance.Commands.AddCommand("Users", false,
  90. "reset user email",
  91. "reset user email [<first> [<last> [<email>]]]",
  92. "Reset a user email address", HandleResetUserEmail);
  93. MainConsole.Instance.Commands.AddCommand("Users", false,
  94. "set user level",
  95. "set user level [<first> [<last> [<level>]]]",
  96. "Set user level. If >= 200 and 'allow_grid_gods = true' in OpenSim.ini, "
  97. + "this account will be treated as god-moded. "
  98. + "It will also affect the 'login level' command. ",
  99. HandleSetUserLevel);
  100. MainConsole.Instance.Commands.AddCommand("Users", false,
  101. "show account",
  102. "show account <first> <last>",
  103. "Show account details for the given user", HandleShowAccount);
  104. }
  105. }
  106. #region IUserAccountService
  107. public UserAccount GetUserAccount(UUID scopeID, string firstName,
  108. string lastName)
  109. {
  110. // m_log.DebugFormat(
  111. // "[USER ACCOUNT SERVICE]: Retrieving account by username for {0} {1}, scope {2}",
  112. // firstName, lastName, scopeID);
  113. UserAccountData[] d;
  114. if (scopeID != UUID.Zero)
  115. {
  116. d = m_Database.Get(
  117. new string[] { "ScopeID", "FirstName", "LastName" },
  118. new string[] { scopeID.ToString(), firstName, lastName });
  119. if (d.Length < 1)
  120. {
  121. d = m_Database.Get(
  122. new string[] { "ScopeID", "FirstName", "LastName" },
  123. new string[] { UUID.Zero.ToString(), firstName, lastName });
  124. }
  125. }
  126. else
  127. {
  128. d = m_Database.Get(
  129. new string[] { "FirstName", "LastName" },
  130. new string[] { firstName, lastName });
  131. }
  132. if (d.Length < 1)
  133. return null;
  134. return MakeUserAccount(d[0]);
  135. }
  136. private UserAccount MakeUserAccount(UserAccountData d)
  137. {
  138. UserAccount u = new UserAccount();
  139. u.FirstName = d.FirstName;
  140. u.LastName = d.LastName;
  141. u.PrincipalID = d.PrincipalID;
  142. u.ScopeID = d.ScopeID;
  143. if (d.Data.ContainsKey("Email") && d.Data["Email"] != null)
  144. u.Email = d.Data["Email"].ToString();
  145. else
  146. u.Email = string.Empty;
  147. u.Created = Convert.ToInt32(d.Data["Created"].ToString());
  148. if (d.Data.ContainsKey("UserTitle") && d.Data["UserTitle"] != null)
  149. u.UserTitle = d.Data["UserTitle"].ToString();
  150. else
  151. u.UserTitle = string.Empty;
  152. if (d.Data.ContainsKey("UserLevel") && d.Data["UserLevel"] != null)
  153. Int32.TryParse(d.Data["UserLevel"], out u.UserLevel);
  154. if (d.Data.ContainsKey("UserFlags") && d.Data["UserFlags"] != null)
  155. Int32.TryParse(d.Data["UserFlags"], out u.UserFlags);
  156. if (d.Data.ContainsKey("UserCountry") && d.Data["UserCountry"] != null)
  157. u.UserCountry = d.Data["UserCountry"].ToString();
  158. else
  159. u.UserCountry = string.Empty;
  160. if (d.Data.ContainsKey("ServiceURLs") && d.Data["ServiceURLs"] != null)
  161. {
  162. string[] URLs = d.Data["ServiceURLs"].ToString().Split(new char[] { ' ' });
  163. u.ServiceURLs = new Dictionary<string, object>();
  164. foreach (string url in URLs)
  165. {
  166. string[] parts = url.Split(new char[] { '=' });
  167. if (parts.Length != 2)
  168. continue;
  169. string name = System.Web.HttpUtility.UrlDecode(parts[0]);
  170. string val = System.Web.HttpUtility.UrlDecode(parts[1]);
  171. u.ServiceURLs[name] = val;
  172. }
  173. }
  174. else
  175. u.ServiceURLs = new Dictionary<string, object>();
  176. return u;
  177. }
  178. public UserAccount GetUserAccount(UUID scopeID, string email)
  179. {
  180. UserAccountData[] d;
  181. if (scopeID != UUID.Zero)
  182. {
  183. d = m_Database.Get(
  184. new string[] { "ScopeID", "Email" },
  185. new string[] { scopeID.ToString(), email });
  186. if (d.Length < 1)
  187. {
  188. d = m_Database.Get(
  189. new string[] { "ScopeID", "Email" },
  190. new string[] { UUID.Zero.ToString(), email });
  191. }
  192. }
  193. else
  194. {
  195. d = m_Database.Get(
  196. new string[] { "Email" },
  197. new string[] { email });
  198. }
  199. if (d.Length < 1)
  200. return null;
  201. return MakeUserAccount(d[0]);
  202. }
  203. public UserAccount GetUserAccount(UUID scopeID, UUID principalID)
  204. {
  205. UserAccountData[] d;
  206. if (scopeID != UUID.Zero)
  207. {
  208. d = m_Database.Get(
  209. new string[] { "ScopeID", "PrincipalID" },
  210. new string[] { scopeID.ToString(), principalID.ToString() });
  211. if (d.Length < 1)
  212. {
  213. d = m_Database.Get(
  214. new string[] { "ScopeID", "PrincipalID" },
  215. new string[] { UUID.Zero.ToString(), principalID.ToString() });
  216. }
  217. }
  218. else
  219. {
  220. d = m_Database.Get(
  221. new string[] { "PrincipalID" },
  222. new string[] { principalID.ToString() });
  223. }
  224. if (d.Length < 1)
  225. {
  226. return null;
  227. }
  228. return MakeUserAccount(d[0]);
  229. }
  230. public List<UserAccount> GetUserAccounts(UUID scopeID, List<string> IDs)
  231. {
  232. // do it one at a time db access should be fast, so no need to break its api
  233. List<UserAccount> accs = new List<UserAccount>();
  234. UUID uuid = UUID.Zero;
  235. foreach(string id in IDs)
  236. {
  237. if (UUID.TryParse(id, out uuid) && uuid != UUID.Zero)
  238. accs.Add(GetUserAccount(scopeID, uuid));
  239. }
  240. return accs;
  241. }
  242. public void InvalidateCache(UUID userID)
  243. {
  244. }
  245. public bool StoreUserAccount(UserAccount data)
  246. {
  247. // m_log.DebugFormat(
  248. // "[USER ACCOUNT SERVICE]: Storing user account for {0} {1} {2}, scope {3}",
  249. // data.FirstName, data.LastName, data.PrincipalID, data.ScopeID);
  250. UserAccountData d = new UserAccountData();
  251. d.FirstName = data.FirstName;
  252. d.LastName = data.LastName;
  253. d.PrincipalID = data.PrincipalID;
  254. d.ScopeID = data.ScopeID;
  255. d.Data = new Dictionary<string, string>();
  256. d.Data["Email"] = data.Email;
  257. d.Data["Created"] = data.Created.ToString();
  258. d.Data["UserLevel"] = data.UserLevel.ToString();
  259. d.Data["UserFlags"] = data.UserFlags.ToString();
  260. if (data.UserTitle != null)
  261. d.Data["UserTitle"] = data.UserTitle.ToString();
  262. List<string> parts = new List<string>();
  263. foreach (KeyValuePair<string, object> kvp in data.ServiceURLs)
  264. {
  265. string key = System.Web.HttpUtility.UrlEncode(kvp.Key);
  266. string val = System.Web.HttpUtility.UrlEncode(kvp.Value.ToString());
  267. parts.Add(key + "=" + val);
  268. }
  269. d.Data["ServiceURLs"] = string.Join(" ", parts.ToArray());
  270. return m_Database.Store(d);
  271. }
  272. public List<UserAccount> GetUserAccounts(UUID scopeID, string query)
  273. {
  274. UserAccountData[] d = m_Database.GetUsers(scopeID, query.Trim());
  275. if (d == null)
  276. return new List<UserAccount>();
  277. List<UserAccount> ret = new List<UserAccount>();
  278. foreach (UserAccountData data in d)
  279. ret.Add(MakeUserAccount(data));
  280. return ret;
  281. }
  282. public List<UserAccount> GetUserAccountsWhere(UUID scopeID, string where)
  283. {
  284. UserAccountData[] d = m_Database.GetUsersWhere(scopeID, where);
  285. if (d == null)
  286. return new List<UserAccount>();
  287. List<UserAccount> ret = new List<UserAccount>();
  288. foreach (UserAccountData data in d)
  289. ret.Add(MakeUserAccount(data));
  290. return ret;
  291. }
  292. #endregion
  293. #region Console commands
  294. /// <summary>
  295. /// Handle the create user command from the console.
  296. /// </summary>
  297. /// <param name="cmdparams">string array with parameters: firstname, lastname, password, locationX, locationY, email, userID, model name </param>
  298. protected void HandleCreateUser(string module, string[] cmdparams)
  299. {
  300. string firstName;
  301. string lastName;
  302. string password;
  303. string email;
  304. string rawPrincipalId;
  305. string model;
  306. List<char> excluded = new List<char>(new char[]{' '});
  307. if (cmdparams.Length < 3)
  308. firstName = MainConsole.Instance.CmdPrompt("First name", "Default", excluded);
  309. else firstName = cmdparams[2];
  310. if (cmdparams.Length < 4)
  311. lastName = MainConsole.Instance.CmdPrompt("Last name", "User", excluded);
  312. else lastName = cmdparams[3];
  313. if (cmdparams.Length < 5)
  314. password = MainConsole.Instance.PasswdPrompt("Password");
  315. else password = cmdparams[4];
  316. if (cmdparams.Length < 6)
  317. email = MainConsole.Instance.CmdPrompt("Email", "");
  318. else email = cmdparams[5];
  319. if (cmdparams.Length < 7)
  320. rawPrincipalId = MainConsole.Instance.CmdPrompt("User ID", UUID.Random().ToString());
  321. else
  322. rawPrincipalId = cmdparams[6];
  323. if (cmdparams.Length < 8)
  324. model = MainConsole.Instance.CmdPrompt("Model name","");
  325. else
  326. model = cmdparams[7];
  327. UUID principalId = UUID.Zero;
  328. if (!UUID.TryParse(rawPrincipalId, out principalId))
  329. throw new Exception(string.Format("ID {0} is not a valid UUID", rawPrincipalId));
  330. CreateUser(UUID.Zero, principalId, firstName, lastName, password, email, model);
  331. }
  332. protected void HandleShowAccount(string module, string[] cmdparams)
  333. {
  334. if (cmdparams.Length != 4)
  335. {
  336. MainConsole.Instance.Output("Usage: show account <first-name> <last-name>");
  337. return;
  338. }
  339. string firstName = cmdparams[2];
  340. string lastName = cmdparams[3];
  341. UserAccount ua = GetUserAccount(UUID.Zero, firstName, lastName);
  342. if (ua == null)
  343. {
  344. MainConsole.Instance.OutputFormat("No user named {0} {1}", firstName, lastName);
  345. return;
  346. }
  347. MainConsole.Instance.OutputFormat("Name: {0}", ua.Name);
  348. MainConsole.Instance.OutputFormat("ID: {0}", ua.PrincipalID);
  349. MainConsole.Instance.OutputFormat("Title: {0}", ua.UserTitle);
  350. MainConsole.Instance.OutputFormat("E-mail: {0}", ua.Email);
  351. MainConsole.Instance.OutputFormat("Created: {0}", Utils.UnixTimeToDateTime(ua.Created));
  352. MainConsole.Instance.OutputFormat("Level: {0}", ua.UserLevel);
  353. MainConsole.Instance.OutputFormat("Flags: {0}", ua.UserFlags);
  354. foreach (KeyValuePair<string, Object> kvp in ua.ServiceURLs)
  355. MainConsole.Instance.OutputFormat("{0}: {1}", kvp.Key, kvp.Value);
  356. }
  357. protected void HandleResetUserPassword(string module, string[] cmdparams)
  358. {
  359. string firstName;
  360. string lastName;
  361. string newPassword;
  362. if (cmdparams.Length < 4)
  363. firstName = MainConsole.Instance.CmdPrompt("First name");
  364. else firstName = cmdparams[3];
  365. if (cmdparams.Length < 5)
  366. lastName = MainConsole.Instance.CmdPrompt("Last name");
  367. else lastName = cmdparams[4];
  368. if (cmdparams.Length < 6)
  369. newPassword = MainConsole.Instance.PasswdPrompt("New password");
  370. else newPassword = cmdparams[5];
  371. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  372. if (account == null)
  373. {
  374. MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName);
  375. return;
  376. }
  377. bool success = false;
  378. if (m_AuthenticationService != null)
  379. success = m_AuthenticationService.SetPassword(account.PrincipalID, newPassword);
  380. if (!success)
  381. MainConsole.Instance.OutputFormat("Unable to reset password for account {0} {1}.", firstName, lastName);
  382. else
  383. MainConsole.Instance.OutputFormat("Password reset for user {0} {1}", firstName, lastName);
  384. }
  385. protected void HandleResetUserEmail(string module, string[] cmdparams)
  386. {
  387. string firstName;
  388. string lastName;
  389. string newEmail;
  390. if (cmdparams.Length < 4)
  391. firstName = MainConsole.Instance.CmdPrompt("First name");
  392. else firstName = cmdparams[3];
  393. if (cmdparams.Length < 5)
  394. lastName = MainConsole.Instance.CmdPrompt("Last name");
  395. else lastName = cmdparams[4];
  396. if (cmdparams.Length < 6)
  397. newEmail = MainConsole.Instance.PasswdPrompt("New Email");
  398. else newEmail = cmdparams[5];
  399. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  400. if (account == null)
  401. {
  402. MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName);
  403. return;
  404. }
  405. bool success = false;
  406. account.Email = newEmail;
  407. success = StoreUserAccount(account);
  408. if (!success)
  409. MainConsole.Instance.OutputFormat("Unable to set Email for account {0} {1}.", firstName, lastName);
  410. else
  411. MainConsole.Instance.OutputFormat("User Email set for user {0} {1} to {2}", firstName, lastName, account.Email);
  412. }
  413. protected void HandleSetUserLevel(string module, string[] cmdparams)
  414. {
  415. string firstName;
  416. string lastName;
  417. string rawLevel;
  418. int level;
  419. if (cmdparams.Length < 4)
  420. firstName = MainConsole.Instance.CmdPrompt("First name");
  421. else firstName = cmdparams[3];
  422. if (cmdparams.Length < 5)
  423. lastName = MainConsole.Instance.CmdPrompt("Last name");
  424. else lastName = cmdparams[4];
  425. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  426. if (account == null) {
  427. MainConsole.Instance.OutputFormat("No such user");
  428. return;
  429. }
  430. if (cmdparams.Length < 6)
  431. rawLevel = MainConsole.Instance.CmdPrompt("User level");
  432. else rawLevel = cmdparams[5];
  433. if(int.TryParse(rawLevel, out level) == false) {
  434. MainConsole.Instance.OutputFormat("Invalid user level");
  435. return;
  436. }
  437. account.UserLevel = level;
  438. bool success = StoreUserAccount(account);
  439. if (!success)
  440. MainConsole.Instance.OutputFormat("Unable to set user level for account {0} {1}.", firstName, lastName);
  441. else
  442. MainConsole.Instance.OutputFormat("User level set for user {0} {1} to {2}", firstName, lastName, level);
  443. }
  444. #endregion
  445. /// <summary>
  446. /// Create a user
  447. /// </summary>
  448. /// <param name="scopeID">Allows hosting of multiple grids in a single database. Normally left as UUID.Zero</param>
  449. /// <param name="principalID">ID of the user</param>
  450. /// <param name="firstName"></param>
  451. /// <param name="lastName"></param>
  452. /// <param name="password"></param>
  453. /// <param name="email"></param>
  454. /// <param name="model"></param>
  455. public UserAccount CreateUser(UUID scopeID, UUID principalID, string firstName, string lastName, string password, string email, string model = "")
  456. {
  457. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  458. if (null == account)
  459. {
  460. account = new UserAccount(UUID.Zero, principalID, firstName, lastName, email);
  461. if (account.ServiceURLs == null || (account.ServiceURLs != null && account.ServiceURLs.Count == 0))
  462. {
  463. account.ServiceURLs = new Dictionary<string, object>();
  464. account.ServiceURLs["HomeURI"] = string.Empty;
  465. account.ServiceURLs["InventoryServerURI"] = string.Empty;
  466. account.ServiceURLs["AssetServerURI"] = string.Empty;
  467. }
  468. if (StoreUserAccount(account))
  469. {
  470. bool success;
  471. if (m_AuthenticationService != null)
  472. {
  473. success = m_AuthenticationService.SetPassword(account.PrincipalID, password);
  474. if (!success)
  475. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set password for account {0} {1}.",
  476. firstName, lastName);
  477. }
  478. GridRegion home = null;
  479. if (m_GridService != null)
  480. {
  481. List<GridRegion> defaultRegions = m_GridService.GetDefaultRegions(UUID.Zero);
  482. if (defaultRegions != null && defaultRegions.Count >= 1)
  483. home = defaultRegions[0];
  484. if (m_GridUserService != null && home != null)
  485. m_GridUserService.SetHome(account.PrincipalID.ToString(), home.RegionID, new Vector3(128, 128, 0), new Vector3(0, 1, 0));
  486. else
  487. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set home for account {0} {1}.",
  488. firstName, lastName);
  489. }
  490. else
  491. {
  492. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to retrieve home region for account {0} {1}.",
  493. firstName, lastName);
  494. }
  495. if (m_InventoryService != null)
  496. {
  497. success = m_InventoryService.CreateUserInventory(account.PrincipalID);
  498. if (!success)
  499. {
  500. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to create inventory for account {0} {1}.",
  501. firstName, lastName);
  502. }
  503. else
  504. {
  505. m_log.DebugFormat(
  506. "[USER ACCOUNT SERVICE]: Created user inventory for {0} {1}", firstName, lastName);
  507. }
  508. if (m_CreateDefaultAvatarEntries)
  509. {
  510. if (String.IsNullOrEmpty(model))
  511. CreateDefaultAppearanceEntries(account.PrincipalID);
  512. else
  513. EstablishAppearance(account.PrincipalID, model);
  514. }
  515. }
  516. m_log.InfoFormat(
  517. "[USER ACCOUNT SERVICE]: Account {0} {1} {2} created successfully",
  518. firstName, lastName, account.PrincipalID);
  519. }
  520. else
  521. {
  522. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Account creation failed for account {0} {1}", firstName, lastName);
  523. }
  524. }
  525. else
  526. {
  527. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: A user with the name {0} {1} already exists!", firstName, lastName);
  528. }
  529. return account;
  530. }
  531. protected void CreateDefaultAppearanceEntries(UUID principalID)
  532. {
  533. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Creating default appearance items for {0}", principalID);
  534. InventoryFolderBase bodyPartsFolder = m_InventoryService.GetFolderForType(principalID, FolderType.BodyPart);
  535. InventoryItemBase eyes = new InventoryItemBase(UUID.Random(), principalID);
  536. eyes.AssetID = new UUID("4bb6fa4d-1cd2-498a-a84c-95c1a0e745a7");
  537. eyes.Name = "Default Eyes";
  538. eyes.CreatorId = principalID.ToString();
  539. eyes.AssetType = (int)AssetType.Bodypart;
  540. eyes.InvType = (int)InventoryType.Wearable;
  541. eyes.Folder = bodyPartsFolder.ID;
  542. eyes.BasePermissions = (uint)PermissionMask.All;
  543. eyes.CurrentPermissions = (uint)PermissionMask.All;
  544. eyes.EveryOnePermissions = (uint)PermissionMask.All;
  545. eyes.GroupPermissions = (uint)PermissionMask.All;
  546. eyes.NextPermissions = (uint)PermissionMask.All;
  547. eyes.Flags = (uint)WearableType.Eyes;
  548. m_InventoryService.AddItem(eyes);
  549. InventoryItemBase shape = new InventoryItemBase(UUID.Random(), principalID);
  550. shape.AssetID = AvatarWearable.DEFAULT_BODY_ASSET;
  551. shape.Name = "Default Shape";
  552. shape.CreatorId = principalID.ToString();
  553. shape.AssetType = (int)AssetType.Bodypart;
  554. shape.InvType = (int)InventoryType.Wearable;
  555. shape.Folder = bodyPartsFolder.ID;
  556. shape.BasePermissions = (uint)PermissionMask.All;
  557. shape.CurrentPermissions = (uint)PermissionMask.All;
  558. shape.EveryOnePermissions = (uint)PermissionMask.All;
  559. shape.GroupPermissions = (uint)PermissionMask.All;
  560. shape.NextPermissions = (uint)PermissionMask.All;
  561. shape.Flags = (uint)WearableType.Shape;
  562. m_InventoryService.AddItem(shape);
  563. InventoryItemBase skin = new InventoryItemBase(UUID.Random(), principalID);
  564. skin.AssetID = AvatarWearable.DEFAULT_SKIN_ASSET;
  565. skin.Name = "Default Skin";
  566. skin.CreatorId = principalID.ToString();
  567. skin.AssetType = (int)AssetType.Bodypart;
  568. skin.InvType = (int)InventoryType.Wearable;
  569. skin.Folder = bodyPartsFolder.ID;
  570. skin.BasePermissions = (uint)PermissionMask.All;
  571. skin.CurrentPermissions = (uint)PermissionMask.All;
  572. skin.EveryOnePermissions = (uint)PermissionMask.All;
  573. skin.GroupPermissions = (uint)PermissionMask.All;
  574. skin.NextPermissions = (uint)PermissionMask.All;
  575. skin.Flags = (uint)WearableType.Skin;
  576. m_InventoryService.AddItem(skin);
  577. InventoryItemBase hair = new InventoryItemBase(UUID.Random(), principalID);
  578. hair.AssetID = AvatarWearable.DEFAULT_HAIR_ASSET;
  579. hair.Name = "Default Hair";
  580. hair.CreatorId = principalID.ToString();
  581. hair.AssetType = (int)AssetType.Bodypart;
  582. hair.InvType = (int)InventoryType.Wearable;
  583. hair.Folder = bodyPartsFolder.ID;
  584. hair.BasePermissions = (uint)PermissionMask.All;
  585. hair.CurrentPermissions = (uint)PermissionMask.All;
  586. hair.EveryOnePermissions = (uint)PermissionMask.All;
  587. hair.GroupPermissions = (uint)PermissionMask.All;
  588. hair.NextPermissions = (uint)PermissionMask.All;
  589. hair.Flags = (uint)WearableType.Hair;
  590. m_InventoryService.AddItem(hair);
  591. InventoryFolderBase clothingFolder = m_InventoryService.GetFolderForType(principalID, FolderType.Clothing);
  592. InventoryItemBase shirt = new InventoryItemBase(UUID.Random(), principalID);
  593. shirt.AssetID = AvatarWearable.DEFAULT_SHIRT_ASSET;
  594. shirt.Name = "Default Shirt";
  595. shirt.CreatorId = principalID.ToString();
  596. shirt.AssetType = (int)AssetType.Clothing;
  597. shirt.InvType = (int)InventoryType.Wearable;
  598. shirt.Folder = clothingFolder.ID;
  599. shirt.BasePermissions = (uint)PermissionMask.All;
  600. shirt.CurrentPermissions = (uint)PermissionMask.All;
  601. shirt.EveryOnePermissions = (uint)PermissionMask.All;
  602. shirt.GroupPermissions = (uint)PermissionMask.All;
  603. shirt.NextPermissions = (uint)PermissionMask.All;
  604. shirt.Flags = (uint)WearableType.Shirt;
  605. m_InventoryService.AddItem(shirt);
  606. InventoryItemBase pants = new InventoryItemBase(UUID.Random(), principalID);
  607. pants.AssetID = AvatarWearable.DEFAULT_PANTS_ASSET;
  608. pants.Name = "Default Pants";
  609. pants.CreatorId = principalID.ToString();
  610. pants.AssetType = (int)AssetType.Clothing;
  611. pants.InvType = (int)InventoryType.Wearable;
  612. pants.Folder = clothingFolder.ID;
  613. pants.BasePermissions = (uint)PermissionMask.All;
  614. pants.CurrentPermissions = (uint)PermissionMask.All;
  615. pants.EveryOnePermissions = (uint)PermissionMask.All;
  616. pants.GroupPermissions = (uint)PermissionMask.All;
  617. pants.NextPermissions = (uint)PermissionMask.All;
  618. pants.Flags = (uint)WearableType.Pants;
  619. m_InventoryService.AddItem(pants);
  620. if (m_AvatarService != null)
  621. {
  622. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Creating default avatar entries for {0}", principalID);
  623. AvatarWearable[] wearables = new AvatarWearable[6];
  624. wearables[AvatarWearable.EYES] = new AvatarWearable(eyes.ID, eyes.AssetID);
  625. wearables[AvatarWearable.BODY] = new AvatarWearable(shape.ID, shape.AssetID);
  626. wearables[AvatarWearable.SKIN] = new AvatarWearable(skin.ID, skin.AssetID);
  627. wearables[AvatarWearable.HAIR] = new AvatarWearable(hair.ID, hair.AssetID);
  628. wearables[AvatarWearable.SHIRT] = new AvatarWearable(shirt.ID, shirt.AssetID);
  629. wearables[AvatarWearable.PANTS] = new AvatarWearable(pants.ID, pants.AssetID);
  630. AvatarAppearance ap = new AvatarAppearance();
  631. // this loop works, but is questionable
  632. for (int i = 0; i < 6; i++)
  633. {
  634. ap.SetWearable(i, wearables[i]);
  635. }
  636. m_AvatarService.SetAppearance(principalID, ap);
  637. }
  638. }
  639. protected void EstablishAppearance(UUID destinationAgent, string model)
  640. {
  641. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Establishing new appearance for {0} - {1}",
  642. destinationAgent.ToString(), model);
  643. string[] modelSpecifiers = model.Split();
  644. if (modelSpecifiers.Length != 2)
  645. {
  646. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Invalid model name \'{0}\'. Falling back to Ruth for {1}",
  647. model, destinationAgent);
  648. CreateDefaultAppearanceEntries(destinationAgent);
  649. return;
  650. }
  651. // Does the source model exist?
  652. UserAccount modelAccount = GetUserAccount(UUID.Zero, modelSpecifiers[0], modelSpecifiers[1]);
  653. if (modelAccount == null)
  654. {
  655. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Requested model \'{0}\' not found. Falling back to Ruth for {1}",
  656. model, destinationAgent);
  657. CreateDefaultAppearanceEntries(destinationAgent);
  658. return;
  659. }
  660. // Does the source model have an established appearance herself?
  661. AvatarAppearance modelAppearance = m_AvatarService.GetAppearance(modelAccount.PrincipalID);
  662. if (modelAppearance == null)
  663. {
  664. m_log.WarnFormat("USER ACCOUNT SERVICE]: Requested model \'{0}\' does not have an established appearance. Falling back to Ruth for {1}",
  665. model, destinationAgent);
  666. CreateDefaultAppearanceEntries(destinationAgent);
  667. return;
  668. }
  669. try
  670. {
  671. CopyWearablesAndAttachments(destinationAgent, modelAccount.PrincipalID, modelAppearance);
  672. m_AvatarService.SetAppearance(destinationAgent, modelAppearance);
  673. }
  674. catch (Exception e)
  675. {
  676. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring appearance for {0} : {1}",
  677. destinationAgent, e.Message);
  678. }
  679. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Finished establishing appearance for {0}",
  680. destinationAgent.ToString());
  681. }
  682. /// <summary>
  683. /// This method is called by EstablishAppearance to do a copy all inventory items
  684. /// worn or attached to the Clothing inventory folder of the receiving avatar.
  685. /// In parallel the avatar wearables and attachments are updated.
  686. /// </summary>
  687. private void CopyWearablesAndAttachments(UUID destination, UUID source, AvatarAppearance avatarAppearance)
  688. {
  689. // Get Clothing folder of receiver
  690. InventoryFolderBase destinationFolder = m_InventoryService.GetFolderForType(destination, FolderType.Clothing);
  691. if (destinationFolder == null)
  692. throw new Exception("Cannot locate folder(s)");
  693. // Missing destination folder? This should *never* be the case
  694. if (destinationFolder.Type != (short)FolderType.Clothing)
  695. {
  696. destinationFolder = new InventoryFolderBase();
  697. destinationFolder.ID = UUID.Random();
  698. destinationFolder.Name = "Clothing";
  699. destinationFolder.Owner = destination;
  700. destinationFolder.Type = (short)AssetType.Clothing;
  701. destinationFolder.ParentID = m_InventoryService.GetRootFolder(destination).ID;
  702. destinationFolder.Version = 1;
  703. m_InventoryService.AddFolder(destinationFolder); // store base record
  704. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Created folder for destination {0}", source);
  705. }
  706. // Wearables
  707. AvatarWearable[] wearables = avatarAppearance.Wearables;
  708. AvatarWearable wearable;
  709. for (int i = 0; i < wearables.Length; i++)
  710. {
  711. wearable = wearables[i];
  712. if (wearable[0].ItemID != UUID.Zero)
  713. {
  714. // Get inventory item and copy it
  715. InventoryItemBase item = m_InventoryService.GetItem(source, wearable[0].ItemID);
  716. if (item != null)
  717. {
  718. InventoryItemBase destinationItem = new InventoryItemBase(UUID.Random(), destination);
  719. destinationItem.Name = item.Name;
  720. destinationItem.Owner = destination;
  721. destinationItem.Description = item.Description;
  722. destinationItem.InvType = item.InvType;
  723. destinationItem.CreatorId = item.CreatorId;
  724. destinationItem.CreatorData = item.CreatorData;
  725. destinationItem.NextPermissions = item.NextPermissions;
  726. destinationItem.CurrentPermissions = item.CurrentPermissions;
  727. destinationItem.BasePermissions = item.BasePermissions;
  728. destinationItem.EveryOnePermissions = item.EveryOnePermissions;
  729. destinationItem.GroupPermissions = item.GroupPermissions;
  730. destinationItem.AssetType = item.AssetType;
  731. destinationItem.AssetID = item.AssetID;
  732. destinationItem.GroupID = item.GroupID;
  733. destinationItem.GroupOwned = item.GroupOwned;
  734. destinationItem.SalePrice = item.SalePrice;
  735. destinationItem.SaleType = item.SaleType;
  736. destinationItem.Flags = item.Flags;
  737. destinationItem.CreationDate = item.CreationDate;
  738. destinationItem.Folder = destinationFolder.ID;
  739. ApplyNextOwnerPermissions(destinationItem);
  740. m_InventoryService.AddItem(destinationItem);
  741. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Added item {0} to folder {1}", destinationItem.ID, destinationFolder.ID);
  742. // Wear item
  743. AvatarWearable newWearable = new AvatarWearable();
  744. newWearable.Wear(destinationItem.ID, wearable[0].AssetID);
  745. avatarAppearance.SetWearable(i, newWearable);
  746. }
  747. else
  748. {
  749. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring {0} to folder {1}", wearable[0].ItemID, destinationFolder.ID);
  750. }
  751. }
  752. }
  753. // Attachments
  754. List<AvatarAttachment> attachments = avatarAppearance.GetAttachments();
  755. foreach (AvatarAttachment attachment in attachments)
  756. {
  757. int attachpoint = attachment.AttachPoint;
  758. UUID itemID = attachment.ItemID;
  759. if (itemID != UUID.Zero)
  760. {
  761. // Get inventory item and copy it
  762. InventoryItemBase item = m_InventoryService.GetItem(source, itemID);
  763. if (item != null)
  764. {
  765. InventoryItemBase destinationItem = new InventoryItemBase(UUID.Random(), destination);
  766. destinationItem.Name = item.Name;
  767. destinationItem.Owner = destination;
  768. destinationItem.Description = item.Description;
  769. destinationItem.InvType = item.InvType;
  770. destinationItem.CreatorId = item.CreatorId;
  771. destinationItem.CreatorData = item.CreatorData;
  772. destinationItem.NextPermissions = item.NextPermissions;
  773. destinationItem.CurrentPermissions = item.CurrentPermissions;
  774. destinationItem.BasePermissions = item.BasePermissions;
  775. destinationItem.EveryOnePermissions = item.EveryOnePermissions;
  776. destinationItem.GroupPermissions = item.GroupPermissions;
  777. destinationItem.AssetType = item.AssetType;
  778. destinationItem.AssetID = item.AssetID;
  779. destinationItem.GroupID = item.GroupID;
  780. destinationItem.GroupOwned = item.GroupOwned;
  781. destinationItem.SalePrice = item.SalePrice;
  782. destinationItem.SaleType = item.SaleType;
  783. destinationItem.Flags = item.Flags;
  784. destinationItem.CreationDate = item.CreationDate;
  785. destinationItem.Folder = destinationFolder.ID;
  786. ApplyNextOwnerPermissions(destinationItem);
  787. m_InventoryService.AddItem(destinationItem);
  788. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Added item {0} to folder {1}", destinationItem.ID, destinationFolder.ID);
  789. // Attach item
  790. avatarAppearance.SetAttachment(attachpoint, destinationItem.ID, destinationItem.AssetID);
  791. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Attached {0}", destinationItem.ID);
  792. }
  793. else
  794. {
  795. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring {0} to folder {1}", itemID, destinationFolder.ID);
  796. }
  797. }
  798. }
  799. }
  800. /// <summary>
  801. /// Apply next owner permissions.
  802. /// </summary>
  803. private void ApplyNextOwnerPermissions(InventoryItemBase item)
  804. {
  805. if (item.InvType == (int)InventoryType.Object)
  806. {
  807. uint perms = item.CurrentPermissions;
  808. PermissionsUtil.ApplyFoldedPermissions(item.CurrentPermissions, ref perms);
  809. item.CurrentPermissions = perms;
  810. }
  811. item.CurrentPermissions &= item.NextPermissions;
  812. item.BasePermissions &= item.NextPermissions;
  813. item.EveryOnePermissions &= item.NextPermissions;
  814. }
  815. }
  816. }