NullUserAccountData.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Text;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Data;
  36. namespace OpenSim.Data.Null
  37. {
  38. public class NullUserAccountData : IUserAccountData
  39. {
  40. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private Dictionary<UUID, UserAccountData> m_DataByUUID = new Dictionary<UUID, UserAccountData>();
  42. private Dictionary<string, UserAccountData> m_DataByName = new Dictionary<string, UserAccountData>();
  43. private Dictionary<string, UserAccountData> m_DataByEmail = new Dictionary<string, UserAccountData>();
  44. public NullUserAccountData(string connectionString, string realm)
  45. {
  46. // m_log.DebugFormat(
  47. // "[NULL USER ACCOUNT DATA]: Initializing new NullUserAccountData with connectionString [{0}], realm [{1}]",
  48. // connectionString, realm);
  49. }
  50. /// <summary>
  51. /// Tries to implement the Get [] semantics, but it cuts corners like crazy.
  52. /// Specifically, it relies on the knowledge that the only Gets used are
  53. /// keyed on PrincipalID, Email, and FirstName+LastName.
  54. /// </summary>
  55. /// <param name="fields"></param>
  56. /// <param name="values"></param>
  57. /// <returns></returns>
  58. public UserAccountData[] Get(string[] fields, string[] values)
  59. {
  60. // if (m_log.IsDebugEnabled)
  61. // {
  62. // m_log.DebugFormat(
  63. // "[NULL USER ACCOUNT DATA]: Called Get with fields [{0}], values [{1}]",
  64. // string.Join(", ", fields), string.Join(", ", values));
  65. // }
  66. UserAccountData[] userAccounts = new UserAccountData[0];
  67. List<string> fieldsLst = new List<string>(fields);
  68. if (fieldsLst.Contains("PrincipalID"))
  69. {
  70. int i = fieldsLst.IndexOf("PrincipalID");
  71. UUID id = UUID.Zero;
  72. if (UUID.TryParse(values[i], out id))
  73. if (m_DataByUUID.ContainsKey(id))
  74. userAccounts = new UserAccountData[] { m_DataByUUID[id] };
  75. }
  76. else if (fieldsLst.Contains("FirstName") && fieldsLst.Contains("LastName"))
  77. {
  78. int findex = fieldsLst.IndexOf("FirstName");
  79. int lindex = fieldsLst.IndexOf("LastName");
  80. if (m_DataByName.ContainsKey(values[findex] + " " + values[lindex]))
  81. {
  82. userAccounts = new UserAccountData[] { m_DataByName[values[findex] + " " + values[lindex]] };
  83. }
  84. }
  85. else if (fieldsLst.Contains("Email"))
  86. {
  87. int i = fieldsLst.IndexOf("Email");
  88. if (m_DataByEmail.ContainsKey(values[i]))
  89. userAccounts = new UserAccountData[] { m_DataByEmail[values[i]] };
  90. }
  91. // if (m_log.IsDebugEnabled)
  92. // {
  93. // StringBuilder sb = new StringBuilder();
  94. // foreach (UserAccountData uad in userAccounts)
  95. // sb.AppendFormat("({0} {1} {2}) ", uad.FirstName, uad.LastName, uad.PrincipalID);
  96. //
  97. // m_log.DebugFormat(
  98. // "[NULL USER ACCOUNT DATA]: Returning {0} user accounts out of {1}: [{2}]", userAccounts.Length, m_DataByName.Count, sb);
  99. // }
  100. return userAccounts;
  101. }
  102. public bool Store(UserAccountData data)
  103. {
  104. if (data == null)
  105. return false;
  106. m_log.DebugFormat(
  107. "[NULL USER ACCOUNT DATA]: Storing user account {0} {1} {2} {3}",
  108. data.FirstName, data.LastName, data.PrincipalID, this.GetHashCode());
  109. m_DataByUUID[data.PrincipalID] = data;
  110. m_DataByName[data.FirstName + " " + data.LastName] = data;
  111. if (data.Data.ContainsKey("Email") && data.Data["Email"] != null && data.Data["Email"] != string.Empty)
  112. m_DataByEmail[data.Data["Email"]] = data;
  113. // m_log.DebugFormat("m_DataByUUID count is {0}, m_DataByName count is {1}", m_DataByUUID.Count, m_DataByName.Count);
  114. return true;
  115. }
  116. public UserAccountData[] GetUsers(UUID scopeID, string query)
  117. {
  118. // m_log.DebugFormat(
  119. // "[NULL USER ACCOUNT DATA]: Called GetUsers with scope [{0}], query [{1}]", scopeID, query);
  120. string[] words = query.Split(new char[] { ' ' });
  121. for (int i = 0; i < words.Length; i++)
  122. {
  123. if (words[i].Length < 3)
  124. {
  125. if (i != words.Length - 1)
  126. Array.Copy(words, i + 1, words, i, words.Length - i - 1);
  127. Array.Resize(ref words, words.Length - 1);
  128. }
  129. }
  130. if (words.Length == 0)
  131. return new UserAccountData[0];
  132. if (words.Length > 2)
  133. return new UserAccountData[0];
  134. List<string> lst = new List<string>(m_DataByName.Keys);
  135. if (words.Length == 1)
  136. {
  137. lst = lst.FindAll(delegate(string s) { return s.StartsWith(words[0]); });
  138. }
  139. else
  140. {
  141. lst = lst.FindAll(delegate(string s) { return s.Contains(words[0]) || s.Contains(words[1]); });
  142. }
  143. if (lst == null || (lst != null && lst.Count == 0))
  144. return new UserAccountData[0];
  145. UserAccountData[] result = new UserAccountData[lst.Count];
  146. int n = 0;
  147. foreach (string key in lst)
  148. result[n++] = m_DataByName[key];
  149. return result;
  150. }
  151. public bool Delete(string field, string val)
  152. {
  153. // Only delete by PrincipalID
  154. if (field.Equals("PrincipalID"))
  155. {
  156. UUID uuid = UUID.Zero;
  157. if (UUID.TryParse(val, out uuid) && m_DataByUUID.ContainsKey(uuid))
  158. {
  159. UserAccountData account = m_DataByUUID[uuid];
  160. m_DataByUUID.Remove(uuid);
  161. if (m_DataByName.ContainsKey(account.FirstName + " " + account.LastName))
  162. m_DataByName.Remove(account.FirstName + " " + account.LastName);
  163. if (account.Data.ContainsKey("Email") && account.Data["Email"] != string.Empty && m_DataByEmail.ContainsKey(account.Data["Email"]))
  164. m_DataByEmail.Remove(account.Data["Email"]);
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. }
  171. }