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. List<char> excluded = new List<char>(new char[]{' ', '@', '.', ':' }); //Protect user names from using valid HG identifiers.
  329. if (cmdparams.Length < 3)
  330. firstName = MainConsole.Instance.CmdPrompt("First name", "Default", excluded);
  331. else firstName = cmdparams[2];
  332. if (cmdparams.Length < 4)
  333. lastName = MainConsole.Instance.CmdPrompt("Last name", "User", excluded);
  334. else lastName = cmdparams[3];
  335. if (cmdparams.Length < 5)
  336. password = MainConsole.Instance.PasswdPrompt("Password");
  337. else password = cmdparams[4];
  338. if (cmdparams.Length < 6)
  339. email = MainConsole.Instance.CmdPrompt("Email", "");
  340. else email = cmdparams[5];
  341. if (cmdparams.Length < 7)
  342. rawPrincipalId = MainConsole.Instance.CmdPrompt("User ID (enter for random)", "");
  343. else
  344. rawPrincipalId = cmdparams[6];
  345. if (cmdparams.Length < 8)
  346. model = MainConsole.Instance.CmdPrompt("Model name","");
  347. else
  348. model = cmdparams[7];
  349. UUID principalId = UUID.Zero;
  350. if(String.IsNullOrWhiteSpace(rawPrincipalId))
  351. principalId = UUID.Random();
  352. else if (!UUID.TryParse(rawPrincipalId, out principalId))
  353. throw new Exception(string.Format("ID {0} is not a valid UUID", rawPrincipalId));
  354. CreateUser(UUID.Zero, principalId, firstName, lastName, password, email, model);
  355. }
  356. protected void HandleShowAccount(string module, string[] cmdparams)
  357. {
  358. if (cmdparams.Length != 4)
  359. {
  360. MainConsole.Instance.Output("Usage: show account <first-name> <last-name>");
  361. return;
  362. }
  363. string firstName = cmdparams[2];
  364. string lastName = cmdparams[3];
  365. UserAccount ua = GetUserAccount(UUID.Zero, firstName, lastName);
  366. if (ua == null)
  367. {
  368. MainConsole.Instance.OutputFormat("No user named {0} {1}", firstName, lastName);
  369. return;
  370. }
  371. MainConsole.Instance.OutputFormat("Name: {0}", ua.Name);
  372. MainConsole.Instance.OutputFormat("ID: {0}", ua.PrincipalID);
  373. MainConsole.Instance.OutputFormat("Title: {0}", ua.UserTitle);
  374. MainConsole.Instance.OutputFormat("E-mail: {0}", ua.Email);
  375. MainConsole.Instance.OutputFormat("Created: {0}", Utils.UnixTimeToDateTime(ua.Created));
  376. MainConsole.Instance.OutputFormat("Level: {0}", ua.UserLevel);
  377. MainConsole.Instance.OutputFormat("Flags: {0}", ua.UserFlags);
  378. foreach (KeyValuePair<string, Object> kvp in ua.ServiceURLs)
  379. MainConsole.Instance.OutputFormat("{0}: {1}", kvp.Key, kvp.Value);
  380. }
  381. protected void HandleResetUserPassword(string module, string[] cmdparams)
  382. {
  383. string firstName;
  384. string lastName;
  385. string newPassword;
  386. if (cmdparams.Length < 4)
  387. firstName = MainConsole.Instance.CmdPrompt("First name");
  388. else firstName = cmdparams[3];
  389. if (cmdparams.Length < 5)
  390. lastName = MainConsole.Instance.CmdPrompt("Last name");
  391. else lastName = cmdparams[4];
  392. if (cmdparams.Length < 6)
  393. newPassword = MainConsole.Instance.PasswdPrompt("New password");
  394. else newPassword = cmdparams[5];
  395. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  396. if (account == null)
  397. {
  398. MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName);
  399. return;
  400. }
  401. bool success = false;
  402. if (m_AuthenticationService != null)
  403. success = m_AuthenticationService.SetPassword(account.PrincipalID, newPassword);
  404. if (!success)
  405. MainConsole.Instance.OutputFormat("Unable to reset password for account {0} {1}.", firstName, lastName);
  406. else
  407. MainConsole.Instance.OutputFormat("Password reset for user {0} {1}", firstName, lastName);
  408. }
  409. protected void HandleResetUserEmail(string module, string[] cmdparams)
  410. {
  411. string firstName;
  412. string lastName;
  413. string newEmail;
  414. if (cmdparams.Length < 4)
  415. firstName = MainConsole.Instance.CmdPrompt("First name");
  416. else firstName = cmdparams[3];
  417. if (cmdparams.Length < 5)
  418. lastName = MainConsole.Instance.CmdPrompt("Last name");
  419. else lastName = cmdparams[4];
  420. if (cmdparams.Length < 6)
  421. newEmail = MainConsole.Instance.PasswdPrompt("New Email");
  422. else newEmail = cmdparams[5];
  423. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  424. if (account == null)
  425. {
  426. MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName);
  427. return;
  428. }
  429. bool success = false;
  430. account.Email = newEmail;
  431. success = StoreUserAccount(account);
  432. if (!success)
  433. MainConsole.Instance.OutputFormat("Unable to set Email for account {0} {1}.", firstName, lastName);
  434. else
  435. MainConsole.Instance.OutputFormat("User Email set for user {0} {1} to {2}", firstName, lastName, account.Email);
  436. }
  437. protected void HandleSetUserLevel(string module, string[] cmdparams)
  438. {
  439. string firstName;
  440. string lastName;
  441. string rawLevel;
  442. int level;
  443. if (cmdparams.Length < 4)
  444. firstName = MainConsole.Instance.CmdPrompt("First name");
  445. else firstName = cmdparams[3];
  446. if (cmdparams.Length < 5)
  447. lastName = MainConsole.Instance.CmdPrompt("Last name");
  448. else lastName = cmdparams[4];
  449. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  450. if (account == null) {
  451. MainConsole.Instance.OutputFormat("No such user");
  452. return;
  453. }
  454. if (cmdparams.Length < 6)
  455. rawLevel = MainConsole.Instance.CmdPrompt("User level");
  456. else rawLevel = cmdparams[5];
  457. if(int.TryParse(rawLevel, out level) == false) {
  458. MainConsole.Instance.OutputFormat("Invalid user level");
  459. return;
  460. }
  461. account.UserLevel = level;
  462. bool success = StoreUserAccount(account);
  463. if (!success)
  464. MainConsole.Instance.OutputFormat("Unable to set user level for account {0} {1}.", firstName, lastName);
  465. else
  466. MainConsole.Instance.OutputFormat("User level set for user {0} {1} to {2}", firstName, lastName, level);
  467. }
  468. #endregion
  469. /// <summary>
  470. /// Create a user
  471. /// </summary>
  472. /// <param name="scopeID">Allows hosting of multiple grids in a single database. Normally left as UUID.Zero</param>
  473. /// <param name="principalID">ID of the user</param>
  474. /// <param name="firstName"></param>
  475. /// <param name="lastName"></param>
  476. /// <param name="password"></param>
  477. /// <param name="email"></param>
  478. /// <param name="model"></param>
  479. public UserAccount CreateUser(UUID scopeID, UUID principalID, string firstName, string lastName, string password, string email, string model = "")
  480. {
  481. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  482. if (null == account)
  483. {
  484. account = new UserAccount(UUID.Zero, principalID, firstName, lastName, email);
  485. if (account.ServiceURLs == null || (account.ServiceURLs != null && account.ServiceURLs.Count == 0))
  486. {
  487. account.ServiceURLs = new Dictionary<string, object>();
  488. account.ServiceURLs["HomeURI"] = string.Empty;
  489. account.ServiceURLs["InventoryServerURI"] = string.Empty;
  490. account.ServiceURLs["AssetServerURI"] = string.Empty;
  491. }
  492. if (StoreUserAccount(account))
  493. {
  494. bool success;
  495. if (m_AuthenticationService != null)
  496. {
  497. success = m_AuthenticationService.SetPassword(account.PrincipalID, password);
  498. if (!success)
  499. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set password for account {0} {1}.",
  500. firstName, lastName);
  501. }
  502. GridRegion home = null;
  503. if (m_GridService != null)
  504. {
  505. List<GridRegion> defaultRegions = m_GridService.GetDefaultRegions(UUID.Zero);
  506. if (defaultRegions != null && defaultRegions.Count >= 1)
  507. home = defaultRegions[0];
  508. if (m_GridUserService != null && home != null)
  509. m_GridUserService.SetHome(account.PrincipalID.ToString(), home.RegionID, new Vector3(128, 128, 0), new Vector3(0, 1, 0));
  510. else
  511. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set home for account {0} {1}.",
  512. firstName, lastName);
  513. }
  514. else
  515. {
  516. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to retrieve home region for account {0} {1}.",
  517. firstName, lastName);
  518. }
  519. if (m_InventoryService != null)
  520. {
  521. success = m_InventoryService.CreateUserInventory(account.PrincipalID);
  522. if (!success)
  523. {
  524. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to create inventory for account {0} {1}.",
  525. firstName, lastName);
  526. }
  527. else
  528. {
  529. m_log.DebugFormat(
  530. "[USER ACCOUNT SERVICE]: Created user inventory for {0} {1}", firstName, lastName);
  531. }
  532. if (m_CreateDefaultAvatarEntries)
  533. {
  534. if (String.IsNullOrEmpty(model))
  535. CreateDefaultAppearanceEntries(account.PrincipalID);
  536. else
  537. EstablishAppearance(account.PrincipalID, model);
  538. }
  539. }
  540. m_log.InfoFormat(
  541. "[USER ACCOUNT SERVICE]: Account {0} {1} {2} created successfully",
  542. firstName, lastName, account.PrincipalID);
  543. }
  544. else
  545. {
  546. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Account creation failed for account {0} {1}", firstName, lastName);
  547. }
  548. }
  549. else
  550. {
  551. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: A user with the name {0} {1} already exists!", firstName, lastName);
  552. }
  553. return account;
  554. }
  555. protected void CreateDefaultAppearanceEntries(UUID principalID)
  556. {
  557. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Creating default appearance items for {0}", principalID);
  558. InventoryFolderBase bodyPartsFolder = m_InventoryService.GetFolderForType(principalID, FolderType.BodyPart);
  559. // Get Current Outfit folder
  560. InventoryFolderBase currentOutfitFolder = m_InventoryService.GetFolderForType(principalID, FolderType.CurrentOutfit);
  561. InventoryItemBase eyes = new InventoryItemBase(UUID.Random(), principalID);
  562. eyes.AssetID = AvatarWearable.DEFAULT_EYES_ASSET;
  563. eyes.Name = "Default Eyes";
  564. eyes.CreatorId = principalID.ToString();
  565. eyes.AssetType = (int)AssetType.Bodypart;
  566. eyes.InvType = (int)InventoryType.Wearable;
  567. eyes.Folder = bodyPartsFolder.ID;
  568. eyes.BasePermissions = (uint)PermissionMask.All;
  569. eyes.CurrentPermissions = (uint)PermissionMask.All;
  570. eyes.EveryOnePermissions = (uint)PermissionMask.All;
  571. eyes.GroupPermissions = (uint)PermissionMask.All;
  572. eyes.NextPermissions = (uint)PermissionMask.All;
  573. eyes.Flags = (uint)WearableType.Eyes;
  574. m_InventoryService.AddItem(eyes);
  575. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Eyes, eyes.Name, eyes.ID, principalID, currentOutfitFolder.ID);
  576. InventoryItemBase shape = new InventoryItemBase(UUID.Random(), principalID);
  577. shape.AssetID = AvatarWearable.DEFAULT_BODY_ASSET;
  578. shape.Name = "Default Shape";
  579. shape.CreatorId = principalID.ToString();
  580. shape.AssetType = (int)AssetType.Bodypart;
  581. shape.InvType = (int)InventoryType.Wearable;
  582. shape.Folder = bodyPartsFolder.ID;
  583. shape.BasePermissions = (uint)PermissionMask.All;
  584. shape.CurrentPermissions = (uint)PermissionMask.All;
  585. shape.EveryOnePermissions = (uint)PermissionMask.All;
  586. shape.GroupPermissions = (uint)PermissionMask.All;
  587. shape.NextPermissions = (uint)PermissionMask.All;
  588. shape.Flags = (uint)WearableType.Shape;
  589. m_InventoryService.AddItem(shape);
  590. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Shape, shape.Name, shape.ID, principalID, currentOutfitFolder.ID);
  591. InventoryItemBase skin = new InventoryItemBase(UUID.Random(), principalID);
  592. skin.AssetID = AvatarWearable.DEFAULT_SKIN_ASSET;
  593. skin.Name = "Default Skin";
  594. skin.CreatorId = principalID.ToString();
  595. skin.AssetType = (int)AssetType.Bodypart;
  596. skin.InvType = (int)InventoryType.Wearable;
  597. skin.Folder = bodyPartsFolder.ID;
  598. skin.BasePermissions = (uint)PermissionMask.All;
  599. skin.CurrentPermissions = (uint)PermissionMask.All;
  600. skin.EveryOnePermissions = (uint)PermissionMask.All;
  601. skin.GroupPermissions = (uint)PermissionMask.All;
  602. skin.NextPermissions = (uint)PermissionMask.All;
  603. skin.Flags = (uint)WearableType.Skin;
  604. m_InventoryService.AddItem(skin);
  605. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Skin, skin.Name, skin.ID, principalID, currentOutfitFolder.ID);
  606. InventoryItemBase hair = new InventoryItemBase(UUID.Random(), principalID);
  607. hair.AssetID = AvatarWearable.DEFAULT_HAIR_ASSET;
  608. hair.Name = "Default Hair";
  609. hair.CreatorId = principalID.ToString();
  610. hair.AssetType = (int)AssetType.Bodypart;
  611. hair.InvType = (int)InventoryType.Wearable;
  612. hair.Folder = bodyPartsFolder.ID;
  613. hair.BasePermissions = (uint)PermissionMask.All;
  614. hair.CurrentPermissions = (uint)PermissionMask.All;
  615. hair.EveryOnePermissions = (uint)PermissionMask.All;
  616. hair.GroupPermissions = (uint)PermissionMask.All;
  617. hair.NextPermissions = (uint)PermissionMask.All;
  618. hair.Flags = (uint)WearableType.Hair;
  619. m_InventoryService.AddItem(hair);
  620. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Hair, hair.Name, hair.ID, principalID, currentOutfitFolder.ID);
  621. InventoryFolderBase clothingFolder = m_InventoryService.GetFolderForType(principalID, FolderType.Clothing);
  622. InventoryItemBase shirt = new InventoryItemBase(UUID.Random(), principalID);
  623. shirt.AssetID = AvatarWearable.DEFAULT_SHIRT_ASSET;
  624. shirt.Name = "Default Shirt";
  625. shirt.CreatorId = principalID.ToString();
  626. shirt.AssetType = (int)AssetType.Clothing;
  627. shirt.InvType = (int)InventoryType.Wearable;
  628. shirt.Folder = clothingFolder.ID;
  629. shirt.BasePermissions = (uint)PermissionMask.All;
  630. shirt.CurrentPermissions = (uint)PermissionMask.All;
  631. shirt.EveryOnePermissions = (uint)PermissionMask.All;
  632. shirt.GroupPermissions = (uint)PermissionMask.All;
  633. shirt.NextPermissions = (uint)PermissionMask.All;
  634. shirt.Flags = (uint)WearableType.Shirt;
  635. m_InventoryService.AddItem(shirt);
  636. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Shirt, shirt.Name, shirt.ID, principalID, currentOutfitFolder.ID);
  637. InventoryItemBase pants = new InventoryItemBase(UUID.Random(), principalID);
  638. pants.AssetID = AvatarWearable.DEFAULT_PANTS_ASSET;
  639. pants.Name = "Default Pants";
  640. pants.CreatorId = principalID.ToString();
  641. pants.AssetType = (int)AssetType.Clothing;
  642. pants.InvType = (int)InventoryType.Wearable;
  643. pants.Folder = clothingFolder.ID;
  644. pants.BasePermissions = (uint)PermissionMask.All;
  645. pants.CurrentPermissions = (uint)PermissionMask.All;
  646. pants.EveryOnePermissions = (uint)PermissionMask.All;
  647. pants.GroupPermissions = (uint)PermissionMask.All;
  648. pants.NextPermissions = (uint)PermissionMask.All;
  649. pants.Flags = (uint)WearableType.Pants;
  650. m_InventoryService.AddItem(pants);
  651. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Pants, pants.Name, pants.ID, principalID, currentOutfitFolder.ID);
  652. if (m_AvatarService != null)
  653. {
  654. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Creating default avatar entries for {0}", principalID);
  655. AvatarWearable[] wearables = new AvatarWearable[6];
  656. wearables[AvatarWearable.EYES] = new AvatarWearable(eyes.ID, eyes.AssetID);
  657. wearables[AvatarWearable.BODY] = new AvatarWearable(shape.ID, shape.AssetID);
  658. wearables[AvatarWearable.SKIN] = new AvatarWearable(skin.ID, skin.AssetID);
  659. wearables[AvatarWearable.HAIR] = new AvatarWearable(hair.ID, hair.AssetID);
  660. wearables[AvatarWearable.SHIRT] = new AvatarWearable(shirt.ID, shirt.AssetID);
  661. wearables[AvatarWearable.PANTS] = new AvatarWearable(pants.ID, pants.AssetID);
  662. AvatarAppearance ap = new AvatarAppearance();
  663. // this loop works, but is questionable
  664. for (int i = 0; i < 6; i++)
  665. {
  666. ap.SetWearable(i, wearables[i]);
  667. }
  668. m_AvatarService.SetAppearance(principalID, ap);
  669. }
  670. }
  671. protected void EstablishAppearance(UUID destinationAgent, string model)
  672. {
  673. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Establishing new appearance for {0} - {1}",
  674. destinationAgent.ToString(), model);
  675. string[] modelSpecifiers = model.Split();
  676. if (modelSpecifiers.Length != 2)
  677. {
  678. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Invalid model name \'{0}\'. Falling back to Ruth for {1}",
  679. model, destinationAgent);
  680. CreateDefaultAppearanceEntries(destinationAgent);
  681. return;
  682. }
  683. // Does the source model exist?
  684. UserAccount modelAccount = GetUserAccount(UUID.Zero, modelSpecifiers[0], modelSpecifiers[1]);
  685. if (modelAccount == null)
  686. {
  687. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Requested model \'{0}\' not found. Falling back to Ruth for {1}",
  688. model, destinationAgent);
  689. CreateDefaultAppearanceEntries(destinationAgent);
  690. return;
  691. }
  692. // Does the source model have an established appearance herself?
  693. AvatarAppearance modelAppearance = m_AvatarService.GetAppearance(modelAccount.PrincipalID);
  694. if (modelAppearance == null)
  695. {
  696. m_log.WarnFormat("USER ACCOUNT SERVICE]: Requested model \'{0}\' does not have an established appearance. Falling back to Ruth for {1}",
  697. model, destinationAgent);
  698. CreateDefaultAppearanceEntries(destinationAgent);
  699. return;
  700. }
  701. try
  702. {
  703. CopyWearablesAndAttachments(destinationAgent, modelAccount.PrincipalID, modelAppearance);
  704. m_AvatarService.SetAppearance(destinationAgent, modelAppearance);
  705. }
  706. catch (Exception e)
  707. {
  708. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring appearance for {0} : {1}",
  709. destinationAgent, e.Message);
  710. }
  711. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Finished establishing appearance for {0}",
  712. destinationAgent.ToString());
  713. }
  714. /// <summary>
  715. /// This method is called by EstablishAppearance to do a copy all inventory items
  716. /// worn or attached to the Clothing inventory folder of the receiving avatar.
  717. /// In parallel the avatar wearables and attachments are updated.
  718. /// </summary>
  719. private void CopyWearablesAndAttachments(UUID destination, UUID source, AvatarAppearance avatarAppearance)
  720. {
  721. AvatarWearable[] wearables = avatarAppearance.Wearables;
  722. if(wearables.Length == 0)
  723. throw new Exception("Model does not have wearables");
  724. // Get Clothing folder of receiver
  725. InventoryFolderBase destinationFolder = m_InventoryService.GetFolderForType(destination, FolderType.Clothing);
  726. if (destinationFolder == null)
  727. throw new Exception("Cannot locate new clothing folder(s)");
  728. // Get Current Outfit folder
  729. InventoryFolderBase currentOutfitFolder = m_InventoryService.GetFolderForType(destination, FolderType.CurrentOutfit);
  730. // wrong destination folder type? create new
  731. if (destinationFolder.Type != (short)FolderType.Clothing)
  732. {
  733. destinationFolder = new InventoryFolderBase();
  734. destinationFolder.ID = UUID.Random();
  735. destinationFolder.Name = "Clothing";
  736. destinationFolder.Owner = destination;
  737. destinationFolder.Type = (short)AssetType.Clothing;
  738. destinationFolder.ParentID = m_InventoryService.GetRootFolder(destination).ID;
  739. destinationFolder.Version = 1;
  740. m_InventoryService.AddFolder(destinationFolder); // store base record
  741. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Created folder for destination {0} Clothing", source);
  742. }
  743. // Wearables
  744. AvatarWearable basewearable;
  745. WearableItem wearable;
  746. AvatarWearable newbasewearable = new AvatarWearable();
  747. // copy wearables creating new inventory entries
  748. for (int i = 0; i < wearables.Length; i++)
  749. {
  750. basewearable = wearables[i];
  751. if(basewearable == null || basewearable.Count == 0)
  752. continue;
  753. newbasewearable.Clear();
  754. for(int j = 0; j < basewearable.Count; j++)
  755. {
  756. wearable = basewearable[j];
  757. if (wearable.ItemID != UUID.Zero)
  758. {
  759. m_log.DebugFormat("[XXX]: Getting item {0} from avie {1} for {2} {3}",
  760. wearable.ItemID, source, i, j);
  761. // Get inventory item and copy it
  762. InventoryItemBase item = m_InventoryService.GetItem(source, wearable.ItemID);
  763. if(item != null && item.AssetType == (int)AssetType.Link)
  764. {
  765. if(item.AssetID == UUID.Zero )
  766. item = null;
  767. else
  768. item = m_InventoryService.GetItem(source, item.AssetID);
  769. }
  770. if (item != null)
  771. {
  772. InventoryItemBase destinationItem = new InventoryItemBase(UUID.Random(), destination);
  773. destinationItem.Name = item.Name;
  774. destinationItem.Owner = destination;
  775. destinationItem.Description = item.Description;
  776. destinationItem.InvType = item.InvType;
  777. destinationItem.CreatorId = item.CreatorId;
  778. destinationItem.CreatorData = item.CreatorData;
  779. destinationItem.NextPermissions = item.NextPermissions;
  780. destinationItem.CurrentPermissions = item.CurrentPermissions;
  781. destinationItem.BasePermissions = item.BasePermissions;
  782. destinationItem.EveryOnePermissions = item.EveryOnePermissions;
  783. destinationItem.GroupPermissions = item.GroupPermissions;
  784. destinationItem.AssetType = item.AssetType;
  785. destinationItem.AssetID = item.AssetID;
  786. destinationItem.GroupID = item.GroupID;
  787. destinationItem.GroupOwned = item.GroupOwned;
  788. destinationItem.SalePrice = item.SalePrice;
  789. destinationItem.SaleType = item.SaleType;
  790. destinationItem.Flags = item.Flags;
  791. destinationItem.CreationDate = item.CreationDate;
  792. destinationItem.Folder = destinationFolder.ID;
  793. ApplyNextOwnerPermissions(destinationItem);
  794. m_InventoryService.AddItem(destinationItem);
  795. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Added item {0} to folder {1}", destinationItem.ID, destinationFolder.ID);
  796. // Wear item
  797. newbasewearable.Add(destinationItem.ID,wearable.AssetID);
  798. // Add to Current Outfit
  799. CreateCurrentOutfitLink((int)InventoryType.Wearable, item.Flags, item.Name, destinationItem.ID, destination, currentOutfitFolder.ID);
  800. }
  801. else
  802. {
  803. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring {0} to folder {1}", wearable.ItemID, destinationFolder.ID);
  804. }
  805. }
  806. }
  807. avatarAppearance.SetWearable(i, newbasewearable);
  808. }
  809. // Attachments
  810. List<AvatarAttachment> attachments = avatarAppearance.GetAttachments();
  811. avatarAppearance.ClearAttachments();
  812. foreach (AvatarAttachment attachment in attachments)
  813. {
  814. int attachpoint = attachment.AttachPoint;
  815. UUID itemID = attachment.ItemID;
  816. if (itemID != UUID.Zero)
  817. {
  818. // Get inventory item and copy it
  819. InventoryItemBase item = m_InventoryService.GetItem(source, itemID);
  820. if (item != null)
  821. {
  822. InventoryItemBase destinationItem = new InventoryItemBase(UUID.Random(), destination);
  823. destinationItem.Name = item.Name;
  824. destinationItem.Owner = destination;
  825. destinationItem.Description = item.Description;
  826. destinationItem.InvType = item.InvType;
  827. destinationItem.CreatorId = item.CreatorId;
  828. destinationItem.CreatorData = item.CreatorData;
  829. destinationItem.NextPermissions = item.NextPermissions;
  830. destinationItem.CurrentPermissions = item.CurrentPermissions;
  831. destinationItem.BasePermissions = item.BasePermissions;
  832. destinationItem.EveryOnePermissions = item.EveryOnePermissions;
  833. destinationItem.GroupPermissions = item.GroupPermissions;
  834. destinationItem.AssetType = item.AssetType;
  835. destinationItem.AssetID = item.AssetID;
  836. destinationItem.GroupID = item.GroupID;
  837. destinationItem.GroupOwned = item.GroupOwned;
  838. destinationItem.SalePrice = item.SalePrice;
  839. destinationItem.SaleType = item.SaleType;
  840. destinationItem.Flags = item.Flags;
  841. destinationItem.CreationDate = item.CreationDate;
  842. destinationItem.Folder = destinationFolder.ID;
  843. ApplyNextOwnerPermissions(destinationItem);
  844. m_InventoryService.AddItem(destinationItem);
  845. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Added item {0} to folder {1}", destinationItem.ID, destinationFolder.ID);
  846. // Attach item
  847. avatarAppearance.SetAttachment(attachpoint, destinationItem.ID, destinationItem.AssetID);
  848. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Attached {0}", destinationItem.ID);
  849. // Add to Current Outfit
  850. CreateCurrentOutfitLink(destinationItem.InvType, item.Flags, item.Name, destinationItem.ID, destination, currentOutfitFolder.ID);
  851. }
  852. else
  853. {
  854. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring {0} to folder {1}", itemID, destinationFolder.ID);
  855. }
  856. }
  857. }
  858. }
  859. protected void CreateCurrentOutfitLink(int invType, uint itemType, string name, UUID itemID, UUID userID, UUID currentOutfitFolderUUID)
  860. {
  861. UUID LinkInvItem = UUID.Random();
  862. InventoryItemBase itembase = new InventoryItemBase(LinkInvItem, userID)
  863. {
  864. AssetID = itemID,
  865. AssetType = (int)AssetType.Link,
  866. CreatorId = userID.ToString(),
  867. InvType = invType,
  868. Description = "",
  869. //Folder = m_InventoryService.GetFolderForType(userID, FolderType.CurrentOutfit).ID,
  870. Folder = currentOutfitFolderUUID,
  871. Flags = itemType,
  872. Name = name,
  873. BasePermissions = (uint)PermissionMask.Copy,
  874. CurrentPermissions = (uint)PermissionMask.Copy,
  875. EveryOnePermissions = (uint)PermissionMask.Copy,
  876. GroupPermissions = (uint)PermissionMask.Copy,
  877. NextPermissions = (uint)PermissionMask.Copy
  878. };
  879. m_InventoryService.AddItem(itembase);
  880. }
  881. /// <summary>
  882. /// Apply next owner permissions.
  883. /// </summary>
  884. private void ApplyNextOwnerPermissions(InventoryItemBase item)
  885. {
  886. if (item.InvType == (int)InventoryType.Object)
  887. {
  888. uint perms = item.CurrentPermissions;
  889. item.CurrentPermissions = perms;
  890. }
  891. item.CurrentPermissions &= item.NextPermissions;
  892. item.BasePermissions &= item.NextPermissions;
  893. item.EveryOnePermissions &= item.NextPermissions;
  894. }
  895. }
  896. }