UserAccountService.cs 46 KB

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