PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Dev/AionCxxLibrary/DAO/AccountDAO.cs

http://aioncxx.codeplex.com
C# | 231 lines | 210 code | 8 blank | 13 comment | 6 complexity | bde7abec012710db03b37f7565e8f238 MD5 | raw file
  1. /* Copyright (c) 2011-2012, Zetatron Consulting
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  4. * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  5. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  6. * Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  7. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  8. */
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Data.SqlClient;
  14. using System.Data;
  15. using AionCxxLibrary.Utilities;
  16. using System.Configuration;
  17. using AionCxxLibrary.Models;
  18. namespace AionCxxLibrary.DAO
  19. {
  20. public class AccountDAO
  21. {
  22. public static Account LoadAccount(int accountId)
  23. {
  24. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  25. SqlCommand cmd = new SqlCommand("SELECT Name,Password FROM dbo.Accounts WHERE ID = @AcctID", con);
  26. cmd.Parameters.Add("@AcctID", SqlDbType.BigInt).Value = accountId;
  27. Account res = null;
  28. try
  29. {
  30. con.Open();
  31. SqlDataReader rdr = cmd.ExecuteReader();
  32. if (rdr.Read())
  33. {
  34. res = new Account(accountId);
  35. res.Name = rdr["Name"].ToString();
  36. res.Password = rdr["Password"].ToString();
  37. }
  38. }
  39. catch (Exception e)
  40. {
  41. Log.Error("Cannot load account " + accountId + "!", e);
  42. res = null;
  43. }
  44. finally
  45. {
  46. cmd.Dispose();
  47. con.Close();
  48. }
  49. return res;
  50. }
  51. public static int Authenticate(string username, string password)
  52. {
  53. int result = 0;
  54. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  55. SqlCommand cmd = new SqlCommand("SELECT ID FROM dbo.Accounts WHERE Name = @Name and Password = @Password", con);
  56. cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = username.ToUpper();
  57. cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = Utility.EncodePassword(username, password);
  58. try
  59. {
  60. con.Open();
  61. object res = cmd.ExecuteScalar();
  62. if (res != null) result = int.Parse(res.ToString());
  63. }
  64. catch (Exception e)
  65. {
  66. Log.Error("Cannot authenticate user!", e);
  67. result = 0;
  68. }
  69. finally
  70. {
  71. cmd.Dispose();
  72. con.Close();
  73. }
  74. return result;
  75. }
  76. public static int CreateAccount(string username, string password)
  77. {
  78. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  79. SqlCommand cmd = new SqlCommand("INSERT INTO dbo.Accounts(Name, Password) VALUES(@Name, @Password); SELECT @@IDENTITY;", con);
  80. cmd.Parameters.Add("@Name", System.Data.SqlDbType.VarChar).Value = username.ToUpper();
  81. cmd.Parameters.Add("@Password", System.Data.SqlDbType.VarChar).Value = Utility.EncodePassword(username, password);
  82. int result = 0;
  83. try
  84. {
  85. con.Open();
  86. result = int.Parse(cmd.ExecuteScalar().ToString());
  87. }
  88. catch (Exception e)
  89. {
  90. Log.Error("Error while creating account!", e);
  91. result = 0;
  92. }
  93. finally
  94. {
  95. cmd.Dispose();
  96. con.Close();
  97. }
  98. return result;
  99. }
  100. public static void UpdateSession(int accountId, int keyLogin, int keyWorld1, int keyWorld2, int keyReconnect)
  101. {
  102. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  103. SqlCommand cmd = new SqlCommand("DELETE FROM dbo.Sessions WHERE AccountID = @AcctID; INSERT INTO dbo.Sessions(AccountID, KeyLogin, KeyWorld1, KeyWorld2, KeyReconnect) VALUES(@AcctID, @KL, @KW1, @KW2, @KR);", con);
  104. cmd.Parameters.Add("@AcctID", System.Data.SqlDbType.BigInt).Value = accountId;
  105. cmd.Parameters.Add("@KL", System.Data.SqlDbType.Int).Value = keyLogin;
  106. cmd.Parameters.Add("@KW1", System.Data.SqlDbType.Int).Value = keyWorld1;
  107. cmd.Parameters.Add("@KW2", System.Data.SqlDbType.Int).Value = keyWorld2;
  108. cmd.Parameters.Add("@KR", SqlDbType.Int).Value = keyReconnect;
  109. try
  110. {
  111. con.Open();
  112. cmd.ExecuteNonQuery();
  113. }
  114. catch (Exception e)
  115. {
  116. Log.Error("Cannot clear sessions for account " + accountId + " !", e);
  117. }
  118. finally
  119. {
  120. cmd.Dispose();
  121. con.Close();
  122. }
  123. }
  124. public static bool CheckSession(int accountId, int keyLogin, int keyWorld1, int keyWorld2)
  125. {
  126. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  127. SqlCommand cmd = new SqlCommand("SELECT COUNT(SessionInternalID) FROM dbo.Sessions WHERE AccountID = @AcctID AND KeyLogin = @KL AND KeyWorld1 = @KW1 AND KeyWorld2 = @KW2", con);
  128. cmd.Parameters.Add("@AcctID", SqlDbType.BigInt).Value = accountId;
  129. cmd.Parameters.Add("@KL", SqlDbType.Int).Value = keyLogin;
  130. cmd.Parameters.Add("@KW1", SqlDbType.Int).Value = keyWorld1;
  131. cmd.Parameters.Add("@KW2", SqlDbType.Int).Value = keyWorld2;
  132. bool result = false;
  133. try
  134. {
  135. con.Open();
  136. int res = int.Parse(cmd.ExecuteScalar().ToString());
  137. if (res > 0) result = true;
  138. }
  139. catch (Exception e)
  140. {
  141. Log.Error("Cannot check session!", e);
  142. result = false;
  143. }
  144. finally
  145. {
  146. cmd.Dispose();
  147. con.Close();
  148. }
  149. return result;
  150. }
  151. public static bool CheckSession(int accountId, int keyLogin)
  152. {
  153. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  154. SqlCommand cmd = new SqlCommand("SELECT COUNT(SessionInternalID) FROM dbo.Sessions WHERE AccountID = @AcctID AND KeyLogin = @KL", con);
  155. cmd.Parameters.Add("@AcctID", SqlDbType.BigInt).Value = accountId;
  156. cmd.Parameters.Add("@KL", SqlDbType.Int).Value = keyLogin;
  157. bool result = false;
  158. try
  159. {
  160. con.Open();
  161. int res = int.Parse(cmd.ExecuteScalar().ToString());
  162. if (res > 0) result = true;
  163. }
  164. catch (Exception e)
  165. {
  166. Log.Error("Cannot check session!", e);
  167. result = false;
  168. }
  169. finally
  170. {
  171. cmd.Dispose();
  172. con.Close();
  173. }
  174. return result;
  175. }
  176. public static bool CheckSession(int accountId, int keyLogin, int keyReconnect)
  177. {
  178. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  179. SqlCommand cmd = new SqlCommand("SELECT COUNT(SessionInternalID) FROM dbo.Sessions WHERE AccountID = @AcctID AND KeyLogin = @KL AND KeyReconnect = @KR", con);
  180. cmd.Parameters.Add("@AcctID", SqlDbType.BigInt).Value = accountId;
  181. cmd.Parameters.Add("@KL", SqlDbType.Int).Value = keyLogin;
  182. cmd.Parameters.Add("@KR", SqlDbType.Int).Value = keyReconnect;
  183. bool result = false;
  184. try
  185. {
  186. con.Open();
  187. int res = int.Parse(cmd.ExecuteScalar().ToString());
  188. if (res > 0) result = true;
  189. }
  190. catch (Exception e)
  191. {
  192. Log.Error("Cannot check session!", e);
  193. result = false;
  194. }
  195. finally
  196. {
  197. cmd.Dispose();
  198. con.Close();
  199. }
  200. return result;
  201. }
  202. public static void ClearSessionsForAccount(int accountId)
  203. {
  204. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["AuthenticationCatalog"].ToString());
  205. SqlCommand cmd = new SqlCommand("DELETE FROM dbo.Sessions WHERE AccountID = @AcctID", con);
  206. cmd.Parameters.Add("@AcctID", SqlDbType.BigInt).Value = accountId;
  207. try
  208. {
  209. con.Open();
  210. cmd.ExecuteNonQuery();
  211. }
  212. catch (Exception e)
  213. {
  214. Log.Error("Cannot clear sessions for account " + accountId, e);
  215. }
  216. }
  217. }
  218. }