1
0

NullUserAccountData.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Data;
  33. namespace OpenSim.Data.Null
  34. {
  35. public class NullUserAccountData : IUserAccountData
  36. {
  37. private static Dictionary<UUID, UserAccountData> m_DataByUUID = new Dictionary<UUID, UserAccountData>();
  38. private static Dictionary<string, UserAccountData> m_DataByName = new Dictionary<string, UserAccountData>();
  39. private static Dictionary<string, UserAccountData> m_DataByEmail = new Dictionary<string, UserAccountData>();
  40. public NullUserAccountData(string connectionString, string realm)
  41. {
  42. }
  43. /// <summary>
  44. /// Tries to implement the Get [] semantics, but it cuts corners like crazy.
  45. /// Specifically, it relies on the knowledge that the only Gets used are
  46. /// keyed on PrincipalID, Email, and FirstName+LastName.
  47. /// </summary>
  48. /// <param name="fields"></param>
  49. /// <param name="values"></param>
  50. /// <returns></returns>
  51. public UserAccountData[] Get(string[] fields, string[] values)
  52. {
  53. List<string> fieldsLst = new List<string>(fields);
  54. if (fieldsLst.Contains("PrincipalID"))
  55. {
  56. int i = fieldsLst.IndexOf("PrincipalID");
  57. UUID id = UUID.Zero;
  58. if (UUID.TryParse(values[i], out id))
  59. if (m_DataByUUID.ContainsKey(id))
  60. return new UserAccountData[] { m_DataByUUID[id] };
  61. }
  62. if (fieldsLst.Contains("FirstName") && fieldsLst.Contains("LastName"))
  63. {
  64. int findex = fieldsLst.IndexOf("FirstName");
  65. int lindex = fieldsLst.IndexOf("LastName");
  66. if (m_DataByName.ContainsKey(values[findex] + " " + values[lindex]))
  67. return new UserAccountData[] { m_DataByName[values[findex] + " " + values[lindex]] };
  68. }
  69. if (fieldsLst.Contains("Email"))
  70. {
  71. int i = fieldsLst.IndexOf("Email");
  72. if (m_DataByEmail.ContainsKey(values[i]))
  73. return new UserAccountData[] { m_DataByEmail[values[i]] };
  74. }
  75. // Fail
  76. return new UserAccountData[0];
  77. }
  78. public bool Store(UserAccountData data)
  79. {
  80. if (data == null)
  81. return false;
  82. m_DataByUUID[data.PrincipalID] = data;
  83. m_DataByName[data.FirstName + " " + data.LastName] = data;
  84. if (data.Data.ContainsKey("Email") && data.Data["Email"] != null && data.Data["Email"] != string.Empty)
  85. m_DataByEmail[data.Data["Email"]] = data;
  86. return true;
  87. }
  88. public UserAccountData[] GetUsers(UUID scopeID, string query)
  89. {
  90. string[] words = query.Split(new char[] { ' ' });
  91. for (int i = 0; i < words.Length; i++)
  92. {
  93. if (words[i].Length < 3)
  94. {
  95. if (i != words.Length - 1)
  96. Array.Copy(words, i + 1, words, i, words.Length - i - 1);
  97. Array.Resize(ref words, words.Length - 1);
  98. }
  99. }
  100. if (words.Length == 0)
  101. return new UserAccountData[0];
  102. if (words.Length > 2)
  103. return new UserAccountData[0];
  104. List<string> lst = new List<string>(m_DataByName.Keys);
  105. if (words.Length == 1)
  106. {
  107. lst = lst.FindAll(delegate(string s) { return s.StartsWith(words[0]); });
  108. }
  109. else
  110. {
  111. lst = lst.FindAll(delegate(string s) { return s.Contains(words[0]) || s.Contains(words[1]); });
  112. }
  113. if (lst == null || (lst != null && lst.Count == 0))
  114. return new UserAccountData[0];
  115. UserAccountData[] result = new UserAccountData[lst.Count];
  116. int n = 0;
  117. foreach (string key in lst)
  118. result[n++] = m_DataByName[key];
  119. return result;
  120. }
  121. public bool Delete(string field, string val)
  122. {
  123. // Only delete by PrincipalID
  124. if (field.Equals("PrincipalID"))
  125. {
  126. UUID uuid = UUID.Zero;
  127. if (UUID.TryParse(val, out uuid) && m_DataByUUID.ContainsKey(uuid))
  128. {
  129. UserAccountData account = m_DataByUUID[uuid];
  130. m_DataByUUID.Remove(uuid);
  131. if (m_DataByName.ContainsKey(account.FirstName + " " + account.LastName))
  132. m_DataByName.Remove(account.FirstName + " " + account.LastName);
  133. if (account.Data.ContainsKey("Email") && account.Data["Email"] != string.Empty && m_DataByEmail.ContainsKey(account.Data["Email"]))
  134. m_DataByEmail.Remove(account.Data["Email"]);
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. }
  141. }