NullUserAccountData.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. }
  47. /// <summary>
  48. /// Tries to implement the Get [] semantics, but it cuts corners like crazy.
  49. /// Specifically, it relies on the knowledge that the only Gets used are
  50. /// keyed on PrincipalID, Email, and FirstName+LastName.
  51. /// </summary>
  52. /// <param name="fields"></param>
  53. /// <param name="values"></param>
  54. /// <returns></returns>
  55. public UserAccountData[] Get(string[] fields, string[] values)
  56. {
  57. //if (m_log.IsDebugEnabled)
  58. //{
  59. // m_log.DebugFormat(
  60. // "[NULL USER ACCOUNT DATA]: Called Get with fields [{0}], values [{1}]",
  61. // string.Join(", ", fields), string.Join(", ", values));
  62. //}
  63. try
  64. {
  65. UserAccountData uad;
  66. int i = Array.FindIndex(fields, (fn) => fn == "PrincipalID");
  67. if (i >= 0)
  68. {
  69. if (UUID.TryParse(values[i], out UUID id) && m_DataByUUID.TryGetValue(id, out uad))
  70. return new UserAccountData[] { uad };
  71. }
  72. i = Array.FindIndex(fields, (fn) => fn == "FirstName");
  73. if (i >= 0)
  74. {
  75. int lindex = Array.FindIndex(fields, i + 1, (fn) => fn == "LastName");
  76. if(lindex >= 0 && m_DataByName.TryGetValue(values[i] + " " + values[lindex], out uad))
  77. return new UserAccountData[] { uad };
  78. }
  79. i = Array.FindIndex(fields, (fn) => fn == "Email");
  80. if (i >= 0 && m_DataByEmail.TryGetValue(values[i], out uad))
  81. return new UserAccountData[] { uad };
  82. }
  83. catch { }
  84. return Array.Empty<UserAccountData>();
  85. }
  86. public bool Store(UserAccountData data)
  87. {
  88. if (data == null)
  89. return false;
  90. m_log.DebugFormat(
  91. "[NULL USER ACCOUNT DATA]: Storing user account {0} {1} {2} {3}",
  92. data.FirstName, data.LastName, data.PrincipalID, this.GetHashCode());
  93. m_DataByUUID[data.PrincipalID] = data;
  94. m_DataByName[data.FirstName + " " + data.LastName] = data;
  95. if (data.Data.TryGetValue("Email", out string semail) && !string.IsNullOrEmpty(semail))
  96. m_DataByEmail[semail] = data;
  97. // m_log.DebugFormat("m_DataByUUID count is {0}, m_DataByName count is {1}", m_DataByUUID.Count, m_DataByName.Count);
  98. return true;
  99. }
  100. public UserAccountData[] GetUsers(UUID scopeID, string query)
  101. {
  102. //m_log.DebugFormat(
  103. // "[NULL USER ACCOUNT DATA]: Called GetUsers with scope [{0}], query [{1}]", scopeID, query);
  104. string[] words = query.Split();
  105. for (int i = 0; i < words.Length; i++)
  106. {
  107. if (words[i].Length < 3)
  108. {
  109. if (i != words.Length - 1)
  110. Array.Copy(words, i + 1, words, i, words.Length - i - 1);
  111. Array.Resize(ref words, words.Length - 1);
  112. }
  113. }
  114. if (words.Length == 0)
  115. return new UserAccountData[0];
  116. if (words.Length > 2)
  117. return new UserAccountData[0];
  118. List<string> lst = new List<string>(m_DataByName.Keys);
  119. if (words.Length == 1)
  120. {
  121. lst = lst.FindAll(delegate(string s) { return s.StartsWith(words[0]); });
  122. }
  123. else
  124. {
  125. lst = lst.FindAll(delegate(string s) { return s.Contains(words[0]) || s.Contains(words[1]); });
  126. }
  127. if (lst == null || (lst != null && lst.Count == 0))
  128. return new UserAccountData[0];
  129. UserAccountData[] result = new UserAccountData[lst.Count];
  130. int n = 0;
  131. foreach (string key in lst)
  132. result[n++] = m_DataByName[key];
  133. return result;
  134. }
  135. public bool Delete(string field, string val)
  136. {
  137. // Only delete by PrincipalID
  138. if (field.Equals("PrincipalID"))
  139. {
  140. if (UUID.TryParse(val, out UUID uuid) && m_DataByUUID.TryGetValue(uuid, out UserAccountData account))
  141. {
  142. m_DataByUUID.Remove(uuid);
  143. m_DataByName.Remove(account.FirstName + " " + account.LastName);
  144. if (account.Data.TryGetValue("Email", out string semail) && !string.IsNullOrEmpty(semail))
  145. m_DataByEmail.Remove(semail);
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. public UserAccountData[] GetUsersWhere(UUID scopeID, string where)
  152. {
  153. return null;
  154. }
  155. }
  156. }