UserAccountService.cs 47 KB

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