UserAccountService.cs 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 != 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. 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. UserAccount account = GetUserAccount(UUID.Zero, firstName, lastName);
  494. if (null == account)
  495. {
  496. account = new UserAccount(UUID.Zero, principalID, firstName, lastName, email);
  497. if (account.ServiceURLs == null || (account.ServiceURLs != null && account.ServiceURLs.Count == 0))
  498. {
  499. account.ServiceURLs = new Dictionary<string, object>();
  500. account.ServiceURLs["HomeURI"] = string.Empty;
  501. account.ServiceURLs["InventoryServerURI"] = string.Empty;
  502. account.ServiceURLs["AssetServerURI"] = string.Empty;
  503. }
  504. if (StoreUserAccount(account))
  505. {
  506. bool success;
  507. if (m_AuthenticationService != null)
  508. {
  509. success = m_AuthenticationService.SetPassword(account.PrincipalID, password);
  510. if (!success)
  511. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set password for account {0} {1}.",
  512. firstName, lastName);
  513. }
  514. GridRegion home = null;
  515. if (m_GridService != null)
  516. {
  517. List<GridRegion> defaultRegions = m_GridService.GetDefaultRegions(UUID.Zero);
  518. if (defaultRegions != null && defaultRegions.Count >= 1)
  519. home = defaultRegions[0];
  520. if (m_GridUserService != null && home != null)
  521. m_GridUserService.SetHome(account.PrincipalID.ToString(), home.RegionID, new Vector3(128, 128, 0), new Vector3(0, 1, 0));
  522. else
  523. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to set home for account {0} {1}.",
  524. firstName, lastName);
  525. }
  526. else
  527. {
  528. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to retrieve home region for account {0} {1}.",
  529. firstName, lastName);
  530. }
  531. if (m_InventoryService != null)
  532. {
  533. success = m_InventoryService.CreateUserInventory(account.PrincipalID);
  534. if (!success)
  535. {
  536. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Unable to create inventory for account {0} {1}.",
  537. firstName, lastName);
  538. }
  539. else
  540. {
  541. m_log.DebugFormat(
  542. "[USER ACCOUNT SERVICE]: Created user inventory for {0} {1}", firstName, lastName);
  543. }
  544. if (m_CreateDefaultAvatarEntries)
  545. {
  546. if (String.IsNullOrEmpty(model))
  547. CreateDefaultAppearanceEntries(account.PrincipalID);
  548. else
  549. EstablishAppearance(account.PrincipalID, model);
  550. }
  551. }
  552. m_log.InfoFormat(
  553. "[USER ACCOUNT SERVICE]: Account {0} {1} {2} created successfully",
  554. firstName, lastName, account.PrincipalID);
  555. }
  556. else
  557. {
  558. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Account creation failed for account {0} {1}", firstName, lastName);
  559. }
  560. }
  561. else
  562. {
  563. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: A user with the name {0} {1} already exists!", firstName, lastName);
  564. }
  565. return account;
  566. }
  567. protected void CreateDefaultAppearanceEntries(UUID principalID)
  568. {
  569. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Creating default appearance items for {0}", principalID);
  570. InventoryFolderBase bodyPartsFolder = m_InventoryService.GetFolderForType(principalID, FolderType.BodyPart);
  571. // Get Current Outfit folder
  572. InventoryFolderBase currentOutfitFolder = m_InventoryService.GetFolderForType(principalID, FolderType.CurrentOutfit);
  573. InventoryItemBase eyes = new InventoryItemBase(UUID.Random(), principalID);
  574. eyes.AssetID = AvatarWearable.DEFAULT_EYES_ASSET;
  575. eyes.Name = "Default Eyes";
  576. eyes.CreatorId = principalID.ToString();
  577. eyes.AssetType = (int)AssetType.Bodypart;
  578. eyes.InvType = (int)InventoryType.Wearable;
  579. eyes.Folder = bodyPartsFolder.ID;
  580. eyes.BasePermissions = (uint)PermissionMask.All;
  581. eyes.CurrentPermissions = (uint)PermissionMask.All;
  582. eyes.EveryOnePermissions = (uint)PermissionMask.All;
  583. eyes.GroupPermissions = (uint)PermissionMask.All;
  584. eyes.NextPermissions = (uint)PermissionMask.All;
  585. eyes.Flags = (uint)WearableType.Eyes;
  586. m_InventoryService.AddItem(eyes);
  587. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Eyes, eyes.Name, eyes.ID, principalID, currentOutfitFolder.ID);
  588. InventoryItemBase shape = new InventoryItemBase(UUID.Random(), principalID);
  589. shape.AssetID = AvatarWearable.DEFAULT_BODY_ASSET;
  590. shape.Name = "Default Shape";
  591. shape.CreatorId = principalID.ToString();
  592. shape.AssetType = (int)AssetType.Bodypart;
  593. shape.InvType = (int)InventoryType.Wearable;
  594. shape.Folder = bodyPartsFolder.ID;
  595. shape.BasePermissions = (uint)PermissionMask.All;
  596. shape.CurrentPermissions = (uint)PermissionMask.All;
  597. shape.EveryOnePermissions = (uint)PermissionMask.All;
  598. shape.GroupPermissions = (uint)PermissionMask.All;
  599. shape.NextPermissions = (uint)PermissionMask.All;
  600. shape.Flags = (uint)WearableType.Shape;
  601. m_InventoryService.AddItem(shape);
  602. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Shape, shape.Name, shape.ID, principalID, currentOutfitFolder.ID);
  603. InventoryItemBase skin = new InventoryItemBase(UUID.Random(), principalID);
  604. skin.AssetID = AvatarWearable.DEFAULT_SKIN_ASSET;
  605. skin.Name = "Default Skin";
  606. skin.CreatorId = principalID.ToString();
  607. skin.AssetType = (int)AssetType.Bodypart;
  608. skin.InvType = (int)InventoryType.Wearable;
  609. skin.Folder = bodyPartsFolder.ID;
  610. skin.BasePermissions = (uint)PermissionMask.All;
  611. skin.CurrentPermissions = (uint)PermissionMask.All;
  612. skin.EveryOnePermissions = (uint)PermissionMask.All;
  613. skin.GroupPermissions = (uint)PermissionMask.All;
  614. skin.NextPermissions = (uint)PermissionMask.All;
  615. skin.Flags = (uint)WearableType.Skin;
  616. m_InventoryService.AddItem(skin);
  617. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Skin, skin.Name, skin.ID, principalID, currentOutfitFolder.ID);
  618. InventoryItemBase hair = new InventoryItemBase(UUID.Random(), principalID);
  619. hair.AssetID = AvatarWearable.DEFAULT_HAIR_ASSET;
  620. hair.Name = "Default Hair";
  621. hair.CreatorId = principalID.ToString();
  622. hair.AssetType = (int)AssetType.Bodypart;
  623. hair.InvType = (int)InventoryType.Wearable;
  624. hair.Folder = bodyPartsFolder.ID;
  625. hair.BasePermissions = (uint)PermissionMask.All;
  626. hair.CurrentPermissions = (uint)PermissionMask.All;
  627. hair.EveryOnePermissions = (uint)PermissionMask.All;
  628. hair.GroupPermissions = (uint)PermissionMask.All;
  629. hair.NextPermissions = (uint)PermissionMask.All;
  630. hair.Flags = (uint)WearableType.Hair;
  631. m_InventoryService.AddItem(hair);
  632. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Hair, hair.Name, hair.ID, principalID, currentOutfitFolder.ID);
  633. InventoryFolderBase clothingFolder = m_InventoryService.GetFolderForType(principalID, FolderType.Clothing);
  634. InventoryItemBase shirt = new InventoryItemBase(UUID.Random(), principalID);
  635. shirt.AssetID = AvatarWearable.DEFAULT_SHIRT_ASSET;
  636. shirt.Name = "Default Shirt";
  637. shirt.CreatorId = principalID.ToString();
  638. shirt.AssetType = (int)AssetType.Clothing;
  639. shirt.InvType = (int)InventoryType.Wearable;
  640. shirt.Folder = clothingFolder.ID;
  641. shirt.BasePermissions = (uint)PermissionMask.All;
  642. shirt.CurrentPermissions = (uint)PermissionMask.All;
  643. shirt.EveryOnePermissions = (uint)PermissionMask.All;
  644. shirt.GroupPermissions = (uint)PermissionMask.All;
  645. shirt.NextPermissions = (uint)PermissionMask.All;
  646. shirt.Flags = (uint)WearableType.Shirt;
  647. m_InventoryService.AddItem(shirt);
  648. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Shirt, shirt.Name, shirt.ID, principalID, currentOutfitFolder.ID);
  649. InventoryItemBase pants = new InventoryItemBase(UUID.Random(), principalID);
  650. pants.AssetID = AvatarWearable.DEFAULT_PANTS_ASSET;
  651. pants.Name = "Default Pants";
  652. pants.CreatorId = principalID.ToString();
  653. pants.AssetType = (int)AssetType.Clothing;
  654. pants.InvType = (int)InventoryType.Wearable;
  655. pants.Folder = clothingFolder.ID;
  656. pants.BasePermissions = (uint)PermissionMask.All;
  657. pants.CurrentPermissions = (uint)PermissionMask.All;
  658. pants.EveryOnePermissions = (uint)PermissionMask.All;
  659. pants.GroupPermissions = (uint)PermissionMask.All;
  660. pants.NextPermissions = (uint)PermissionMask.All;
  661. pants.Flags = (uint)WearableType.Pants;
  662. m_InventoryService.AddItem(pants);
  663. CreateCurrentOutfitLink((int)InventoryType.Wearable, (uint)WearableType.Pants, pants.Name, pants.ID, principalID, currentOutfitFolder.ID);
  664. if (m_AvatarService != null)
  665. {
  666. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Creating default avatar entries for {0}", principalID);
  667. AvatarWearable[] wearables = new AvatarWearable[6];
  668. wearables[AvatarWearable.EYES] = new AvatarWearable(eyes.ID, eyes.AssetID);
  669. wearables[AvatarWearable.BODY] = new AvatarWearable(shape.ID, shape.AssetID);
  670. wearables[AvatarWearable.SKIN] = new AvatarWearable(skin.ID, skin.AssetID);
  671. wearables[AvatarWearable.HAIR] = new AvatarWearable(hair.ID, hair.AssetID);
  672. wearables[AvatarWearable.SHIRT] = new AvatarWearable(shirt.ID, shirt.AssetID);
  673. wearables[AvatarWearable.PANTS] = new AvatarWearable(pants.ID, pants.AssetID);
  674. AvatarAppearance ap = new AvatarAppearance();
  675. // this loop works, but is questionable
  676. for (int i = 0; i < 6; i++)
  677. {
  678. ap.SetWearable(i, wearables[i]);
  679. }
  680. m_AvatarService.SetAppearance(principalID, ap);
  681. }
  682. }
  683. protected void EstablishAppearance(UUID destinationAgent, string model)
  684. {
  685. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Establishing new appearance for {0} - {1}",
  686. destinationAgent.ToString(), model);
  687. string[] modelSpecifiers = model.Split();
  688. if (modelSpecifiers.Length != 2)
  689. {
  690. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Invalid model name \'{0}\'. Falling back to Ruth for {1}",
  691. model, destinationAgent);
  692. CreateDefaultAppearanceEntries(destinationAgent);
  693. return;
  694. }
  695. // Does the source model exist?
  696. UserAccount modelAccount = GetUserAccount(UUID.Zero, modelSpecifiers[0], modelSpecifiers[1]);
  697. if (modelAccount == null)
  698. {
  699. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Requested model \'{0}\' not found. Falling back to Ruth for {1}",
  700. model, destinationAgent);
  701. CreateDefaultAppearanceEntries(destinationAgent);
  702. return;
  703. }
  704. // Does the source model have an established appearance herself?
  705. AvatarAppearance modelAppearance = m_AvatarService.GetAppearance(modelAccount.PrincipalID);
  706. if (modelAppearance == null)
  707. {
  708. m_log.WarnFormat("USER ACCOUNT SERVICE]: Requested model \'{0}\' does not have an established appearance. Falling back to Ruth for {1}",
  709. model, destinationAgent);
  710. CreateDefaultAppearanceEntries(destinationAgent);
  711. return;
  712. }
  713. try
  714. {
  715. CopyWearablesAndAttachments(destinationAgent, modelAccount.PrincipalID, modelAppearance);
  716. m_AvatarService.SetAppearance(destinationAgent, modelAppearance);
  717. }
  718. catch (Exception e)
  719. {
  720. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring appearance for {0} : {1}",
  721. destinationAgent, e.Message);
  722. }
  723. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Finished establishing appearance for {0}",
  724. destinationAgent.ToString());
  725. }
  726. /// <summary>
  727. /// This method is called by EstablishAppearance to do a copy all inventory items
  728. /// worn or attached to the Clothing inventory folder of the receiving avatar.
  729. /// In parallel the avatar wearables and attachments are updated.
  730. /// </summary>
  731. private void CopyWearablesAndAttachments(UUID destination, UUID source, AvatarAppearance avatarAppearance)
  732. {
  733. AvatarWearable[] wearables = avatarAppearance.Wearables;
  734. if(wearables.Length == 0)
  735. throw new Exception("Model does not have wearables");
  736. // Get Clothing folder of receiver
  737. InventoryFolderBase destinationFolder = m_InventoryService.GetFolderForType(destination, FolderType.Clothing);
  738. if (destinationFolder == null)
  739. throw new Exception("Cannot locate new clothing folder(s)");
  740. // Get Current Outfit folder
  741. InventoryFolderBase currentOutfitFolder = m_InventoryService.GetFolderForType(destination, FolderType.CurrentOutfit);
  742. // wrong destination folder type? create new
  743. if (destinationFolder.Type != (short)FolderType.Clothing)
  744. {
  745. destinationFolder = new InventoryFolderBase();
  746. destinationFolder.ID = UUID.Random();
  747. destinationFolder.Name = "Clothing";
  748. destinationFolder.Owner = destination;
  749. destinationFolder.Type = (short)AssetType.Clothing;
  750. destinationFolder.ParentID = m_InventoryService.GetRootFolder(destination).ID;
  751. destinationFolder.Version = 1;
  752. m_InventoryService.AddFolder(destinationFolder); // store base record
  753. m_log.ErrorFormat("[USER ACCOUNT SERVICE]: Created folder for destination {0} Clothing", source);
  754. }
  755. // Wearables
  756. AvatarWearable basewearable;
  757. WearableItem wearable;
  758. AvatarWearable newbasewearable = new AvatarWearable();
  759. // copy wearables creating new inventory entries
  760. for (int i = 0; i < wearables.Length; i++)
  761. {
  762. basewearable = wearables[i];
  763. if(basewearable == null || basewearable.Count == 0)
  764. continue;
  765. newbasewearable.Clear();
  766. for(int j = 0; j < basewearable.Count; j++)
  767. {
  768. wearable = basewearable[j];
  769. if (wearable.ItemID != UUID.Zero)
  770. {
  771. m_log.DebugFormat("[XXX]: Getting item {0} from avie {1} for {2} {3}",
  772. wearable.ItemID, source, i, j);
  773. // Get inventory item and copy it
  774. InventoryItemBase item = m_InventoryService.GetItem(source, wearable.ItemID);
  775. if(item != null && item.AssetType == (int)AssetType.Link)
  776. {
  777. if(item.AssetID == UUID.Zero )
  778. item = null;
  779. else
  780. item = m_InventoryService.GetItem(source, item.AssetID);
  781. }
  782. if (item != null)
  783. {
  784. InventoryItemBase destinationItem = new InventoryItemBase(UUID.Random(), destination);
  785. destinationItem.Name = item.Name;
  786. destinationItem.Owner = destination;
  787. destinationItem.Description = item.Description;
  788. destinationItem.InvType = item.InvType;
  789. destinationItem.CreatorId = item.CreatorId;
  790. destinationItem.CreatorData = item.CreatorData;
  791. destinationItem.NextPermissions = item.NextPermissions;
  792. destinationItem.CurrentPermissions = item.CurrentPermissions;
  793. destinationItem.BasePermissions = item.BasePermissions;
  794. destinationItem.EveryOnePermissions = item.EveryOnePermissions;
  795. destinationItem.GroupPermissions = item.GroupPermissions;
  796. destinationItem.AssetType = item.AssetType;
  797. destinationItem.AssetID = item.AssetID;
  798. destinationItem.GroupID = item.GroupID;
  799. destinationItem.GroupOwned = item.GroupOwned;
  800. destinationItem.SalePrice = item.SalePrice;
  801. destinationItem.SaleType = item.SaleType;
  802. destinationItem.Flags = item.Flags;
  803. destinationItem.CreationDate = item.CreationDate;
  804. destinationItem.Folder = destinationFolder.ID;
  805. ApplyNextOwnerPermissions(destinationItem);
  806. m_InventoryService.AddItem(destinationItem);
  807. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Added item {0} to folder {1}", destinationItem.ID, destinationFolder.ID);
  808. // Wear item
  809. newbasewearable.Add(destinationItem.ID,wearable.AssetID);
  810. // Add to Current Outfit
  811. CreateCurrentOutfitLink((int)InventoryType.Wearable, item.Flags, item.Name, destinationItem.ID, destination, currentOutfitFolder.ID);
  812. }
  813. else
  814. {
  815. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring {0} to folder {1}", wearable.ItemID, destinationFolder.ID);
  816. }
  817. }
  818. }
  819. avatarAppearance.SetWearable(i, newbasewearable);
  820. }
  821. // Attachments
  822. List<AvatarAttachment> attachments = avatarAppearance.GetAttachments();
  823. avatarAppearance.ClearAttachments();
  824. foreach (AvatarAttachment attachment in attachments)
  825. {
  826. int attachpoint = attachment.AttachPoint;
  827. UUID itemID = attachment.ItemID;
  828. if (itemID != UUID.Zero)
  829. {
  830. // Get inventory item and copy it
  831. InventoryItemBase item = m_InventoryService.GetItem(source, itemID);
  832. if (item != null)
  833. {
  834. InventoryItemBase destinationItem = new InventoryItemBase(UUID.Random(), destination);
  835. destinationItem.Name = item.Name;
  836. destinationItem.Owner = destination;
  837. destinationItem.Description = item.Description;
  838. destinationItem.InvType = item.InvType;
  839. destinationItem.CreatorId = item.CreatorId;
  840. destinationItem.CreatorData = item.CreatorData;
  841. destinationItem.NextPermissions = item.NextPermissions;
  842. destinationItem.CurrentPermissions = item.CurrentPermissions;
  843. destinationItem.BasePermissions = item.BasePermissions;
  844. destinationItem.EveryOnePermissions = item.EveryOnePermissions;
  845. destinationItem.GroupPermissions = item.GroupPermissions;
  846. destinationItem.AssetType = item.AssetType;
  847. destinationItem.AssetID = item.AssetID;
  848. destinationItem.GroupID = item.GroupID;
  849. destinationItem.GroupOwned = item.GroupOwned;
  850. destinationItem.SalePrice = item.SalePrice;
  851. destinationItem.SaleType = item.SaleType;
  852. destinationItem.Flags = item.Flags;
  853. destinationItem.CreationDate = item.CreationDate;
  854. destinationItem.Folder = destinationFolder.ID;
  855. ApplyNextOwnerPermissions(destinationItem);
  856. m_InventoryService.AddItem(destinationItem);
  857. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Added item {0} to folder {1}", destinationItem.ID, destinationFolder.ID);
  858. // Attach item
  859. avatarAppearance.SetAttachment(attachpoint, destinationItem.ID, destinationItem.AssetID);
  860. m_log.DebugFormat("[USER ACCOUNT SERVICE]: Attached {0}", destinationItem.ID);
  861. // Add to Current Outfit
  862. CreateCurrentOutfitLink(destinationItem.InvType, item.Flags, item.Name, destinationItem.ID, destination, currentOutfitFolder.ID);
  863. }
  864. else
  865. {
  866. m_log.WarnFormat("[USER ACCOUNT SERVICE]: Error transferring {0} to folder {1}", itemID, destinationFolder.ID);
  867. }
  868. }
  869. }
  870. }
  871. protected void CreateCurrentOutfitLink(int invType, uint itemType, string name, UUID itemID, UUID userID, UUID currentOutfitFolderUUID)
  872. {
  873. UUID LinkInvItem = UUID.Random();
  874. InventoryItemBase itembase = new InventoryItemBase(LinkInvItem, userID)
  875. {
  876. AssetID = itemID,
  877. AssetType = (int)AssetType.Link,
  878. CreatorId = userID.ToString(),
  879. InvType = invType,
  880. Description = "",
  881. //Folder = m_InventoryService.GetFolderForType(userID, FolderType.CurrentOutfit).ID,
  882. Folder = currentOutfitFolderUUID,
  883. Flags = itemType,
  884. Name = name,
  885. BasePermissions = (uint)PermissionMask.Copy,
  886. CurrentPermissions = (uint)PermissionMask.Copy,
  887. EveryOnePermissions = (uint)PermissionMask.Copy,
  888. GroupPermissions = (uint)PermissionMask.Copy,
  889. NextPermissions = (uint)PermissionMask.Copy
  890. };
  891. m_InventoryService.AddItem(itembase);
  892. }
  893. /// <summary>
  894. /// Apply next owner permissions.
  895. /// </summary>
  896. private void ApplyNextOwnerPermissions(InventoryItemBase item)
  897. {
  898. if (item.InvType == (int)InventoryType.Object)
  899. {
  900. uint perms = item.CurrentPermissions;
  901. item.CurrentPermissions = perms;
  902. }
  903. item.CurrentPermissions &= item.NextPermissions;
  904. item.BasePermissions &= item.NextPermissions;
  905. item.EveryOnePermissions &= item.NextPermissions;
  906. }
  907. }
  908. }