1
0

UserAccountService.cs 48 KB

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