/ASPNETVN.PORTAL.VS2K8/Components/Security/User.cs

# · C# · 347 lines · 267 code · 51 blank · 29 comment · 9 complexity · 7b7d52e7225230c9ca2c5fa1274c117e MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Data.SqlClient;
  6. using Microsoft.Practices.EnterpriseLibrary.Data;
  7. using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
  8. using ASPNETVN.PORTAL.Components.Helpers;
  9. namespace ASPNETVN.PORTAL.Components.Security
  10. {
  11. public enum UserStatus
  12. {
  13. ACTIVE,
  14. DEACTIVE,
  15. LOCK
  16. }
  17. public partial class User
  18. {
  19. public override string ToString()
  20. {
  21. return this._Name + "|" + this._Email + "|" + this._ID;
  22. }
  23. public static DataSet SelectBy_RoleID(string roleID)
  24. {
  25. if (roleID == "ALL")
  26. {
  27. return SelectAll();
  28. }
  29. const string spName = "p_SYSTEM_User_SelectBy_RoleID";
  30. Database db = DatabaseFactory.CreateDatabase();
  31. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  32. db.AddInParameter(dbCommand, "@RoleID", DbType.String, roleID);
  33. return db.ExecuteDataSet(dbCommand);
  34. }
  35. public static DataSet SelectBy_ID(int id)
  36. {
  37. const string spName = "p_SYSTEM_User_Load";
  38. Database db = DatabaseFactory.CreateDatabase();
  39. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  40. db.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
  41. return db.ExecuteDataSet(dbCommand);
  42. }
  43. // Check Exist By Email and PortalID.
  44. public static bool CheckExist(string email)
  45. {
  46. const string spName = "p_SYSTEM_User_CheckExistBy_Email_PortalID";
  47. Database db = DatabaseFactory.CreateDatabase();
  48. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  49. db.AddInParameter(dbCommand, "@Email", DbType.String, email);
  50. db.AddInParameter(dbCommand, "@PortalID", DbType.Guid, Global.PortalID);
  51. IDataReader reader = db.ExecuteReader(dbCommand);
  52. bool returnValue = false;
  53. if (reader.Read())
  54. {
  55. returnValue = true;
  56. }
  57. reader.Close();
  58. dbCommand.Connection.Close();
  59. return returnValue;
  60. }
  61. // Reset Password By Email and PortalID with random new password.
  62. public static string ResetPassword(string email)
  63. {
  64. // Generate new password.
  65. Random rnd = new Random();
  66. string newPassword = string.Empty;
  67. for (int n = 0; n < 8; n++)
  68. {
  69. newPassword += rnd.Next(0, 9).ToString();
  70. }
  71. return ResetPassword(email, newPassword);
  72. }
  73. // Reset Password By Email and PortalID with specific password.
  74. public static string ResetPassword(string email, string newPassword)
  75. {
  76. const string spName = "p_SYSTEM_User_ResetPasswordBy_Email_PortalID";
  77. Database db = DatabaseFactory.CreateDatabase();
  78. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  79. db.AddInParameter(dbCommand, "@Email", DbType.String, email);
  80. db.AddInParameter(dbCommand, "@PortalID", DbType.Guid, Global.PortalID);
  81. db.AddInParameter(dbCommand, "@NewPassword", DbType.String, PortalSecurity.Encrypt(newPassword));
  82. int i = db.ExecuteNonQuery(dbCommand);
  83. if (i <= 0) newPassword = string.Empty;
  84. return newPassword;
  85. }
  86. public int Register()
  87. {
  88. const string spName = "p_SYSTEM_User_Register";
  89. SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
  90. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  91. db.AddInParameter(dbCommand, "@Name", SqlDbType.VarChar, this._Name);
  92. db.AddInParameter(dbCommand, "@Email", SqlDbType.VarChar, this._Email);
  93. db.AddInParameter(dbCommand, "@Password", SqlDbType.VarChar, PortalSecurity.Encrypt(this._Password));
  94. db.AddInParameter(dbCommand, "@PortalID", SqlDbType.UniqueIdentifier, Global.PortalID);
  95. db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);
  96. db.ExecuteNonQuery(dbCommand);
  97. return (int) db.GetParameterValue(dbCommand, "@ID");
  98. }
  99. public static DataSet Search(string whereExpression)
  100. {
  101. string query = "SELECT * FROM t_SYSTEM_Users WHERE " + whereExpression;
  102. Database db = DatabaseFactory.CreateDatabase();
  103. DbCommand dbCommand = db.GetSqlStringCommand(query);
  104. return db.ExecuteDataSet(dbCommand);
  105. }
  106. //-----------------------------------------------------------------------------------------
  107. public static bool Active(string activeCode)
  108. {
  109. try
  110. {
  111. const string spName = "p_SYSTEM_User_Active";
  112. Database db = DatabaseFactory.CreateDatabase();
  113. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  114. db.AddInParameter(dbCommand, "@Email", DbType.String, DecryptHelper.Decrypt(activeCode));
  115. db.AddInParameter(dbCommand, "@PortalID", DbType.Guid, Global.PortalID);
  116. int i = db.ExecuteNonQuery(dbCommand);
  117. return i > 0;
  118. }
  119. catch
  120. {
  121. return false;
  122. }
  123. }
  124. public static void UpdateStatus(int id, UserStatus status)
  125. {
  126. const string spName = "p_SYSTEM_User_UpdateStatus";
  127. Database db = DatabaseFactory.CreateDatabase();
  128. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  129. db.AddInParameter(dbCommand, "@Status", DbType.String, status.ToString());
  130. db.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
  131. db.ExecuteNonQuery(dbCommand);
  132. }
  133. // Change Password By Email and PortalID.
  134. public static bool ChangePassword(string email, string oldPassword, string newPassword)
  135. {
  136. const string spName = "p_SYSTEM_User_ChangePassword";
  137. Database db = DatabaseFactory.CreateDatabase();
  138. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  139. db.AddInParameter(dbCommand, "@PortalID", DbType.Guid, Global.PortalID);
  140. db.AddInParameter(dbCommand, "@Email", DbType.String, email);
  141. db.AddInParameter(dbCommand, "@NewPassword", DbType.String, PortalSecurity.Encrypt(newPassword));
  142. db.AddInParameter(dbCommand, "@OldPassword", DbType.String, PortalSecurity.Encrypt(oldPassword));
  143. int i = db.ExecuteNonQuery(dbCommand);
  144. return i > 0;
  145. }
  146. public static bool AddToRole(int userID, string roleID)
  147. {
  148. const string spName = "p_SYSTEM_User_Role_Insert";
  149. Database db = DatabaseFactory.CreateDatabase();
  150. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  151. db.AddInParameter(dbCommand, "@UserID", DbType.Int32, userID);
  152. db.AddInParameter(dbCommand, "@RoleID", DbType.String, roleID);
  153. int i = db.ExecuteNonQuery(dbCommand);
  154. return i > 0;
  155. }
  156. public static void AddToRole(int userID, int roleID)
  157. {
  158. AddToRole(null, userID, roleID);
  159. }
  160. private static void DeleteRole(DbTransaction transaction, int userID)
  161. {
  162. const string spName = "p_SYSTEM_Users_Roles_DeleteBy_UserID";
  163. SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
  164. SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
  165. db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID);
  166. db.ExecuteNonQuery(dbCommand, transaction);
  167. }
  168. public static int RemoveRole(int userID, int roleID)
  169. {
  170. const string spName = "p_SYSTEM_Users_Roles_DeleteBy_UserID_RoleID";
  171. SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
  172. SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
  173. db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID);
  174. db.AddInParameter(dbCommand, "@RoleID", SqlDbType.Int, roleID);
  175. return db.ExecuteNonQuery(dbCommand);
  176. }
  177. private static void AddToRole(DbTransaction transaction, int userID, int roleID)
  178. {
  179. const string spName = "p_SYSTEM_User_AddToRole";
  180. SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
  181. SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
  182. db.AddInParameter(dbCommand, "@UserID", SqlDbType.Int, userID);
  183. db.AddInParameter(dbCommand, "@RoleID", SqlDbType.Int, roleID);
  184. if (transaction != null)
  185. db.ExecuteNonQuery(dbCommand, transaction);
  186. else
  187. db.ExecuteNonQuery(dbCommand);
  188. }
  189. public static bool AddToRoles(int userID, ArrayList roles)
  190. {
  191. bool returnValue;
  192. SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
  193. using (SqlConnection connection = (SqlConnection)db.CreateConnection())
  194. {
  195. connection.Open();
  196. SqlTransaction transaction = connection.BeginTransaction();
  197. {
  198. try
  199. {
  200. // Clear all role of user before insert new role.
  201. DeleteRole(transaction, userID);
  202. foreach (string role in roles)
  203. {
  204. AddToRole(transaction, userID, Convert.ToInt32(role));
  205. }
  206. transaction.Commit();
  207. returnValue = true;
  208. }
  209. catch
  210. {
  211. transaction.Rollback();
  212. returnValue = false;
  213. }
  214. finally
  215. {
  216. connection.Close();
  217. }
  218. }
  219. }
  220. return returnValue;
  221. }
  222. //---------------------------------------------------------------------------------------------
  223. //// Get Roles By Email and PortalID.
  224. //public static string[] GetRoles(string email)
  225. //{
  226. // string spName = "p_SYSTEM_User_GetRolesBy_Email_PortalID";
  227. // Database db = DatabaseFactory.CreateDatabase();
  228. // DbCommand dbCommand = db.GetStoredProcCommand(spName);
  229. // db.AddInParameter(dbCommand, "@Email", DbType.String, email);
  230. // db.AddInParameter(dbCommand, "@PortalID", DbType.Int32, Global.PortalID);
  231. // IDataReader reader = db.ExecuteReader(dbCommand);
  232. // ArrayList userRoles = new ArrayList();
  233. // while (reader.Read())
  234. // {
  235. // userRoles.Add(reader["ID"].ToString());
  236. // }
  237. // reader.Close();
  238. // dbCommand.Connection.Close();
  239. // return (string[]) userRoles.ToArray(typeof (string));
  240. //}
  241. public static string[] GetRoles(int userID)
  242. {
  243. const string spName = "p_SYSTEM_User_GetRolesBy_UserID";
  244. Database db = DatabaseFactory.CreateDatabase();
  245. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  246. db.AddInParameter(dbCommand, "@UserID", DbType.String, userID);
  247. IDataReader reader = db.ExecuteReader(dbCommand);
  248. ArrayList userRoles = new ArrayList();
  249. while (reader.Read())
  250. {
  251. userRoles.Add(reader["ID"].ToString());
  252. }
  253. reader.Close();
  254. dbCommand.Connection.Close();
  255. return (string[])userRoles.ToArray(typeof(string));
  256. }
  257. public static DataSet SelectRolesBy_UserID(int userID)
  258. {
  259. const string spName = "p_SYSTEM_User_GetRolesBy_UserID";
  260. Database db = DatabaseFactory.CreateDatabase();
  261. DbCommand dbCommand = db.GetStoredProcCommand(spName);
  262. db.AddInParameter(dbCommand, "@UserID", DbType.String, userID);
  263. return db.ExecuteDataSet(dbCommand);
  264. }
  265. //-----------------------------------------------------------------------------------------
  266. // Login By Email and Password and PortalID.
  267. public static IdentityUser Login(string email, string password)
  268. {
  269. const string spName = "p_SYSTEM_User_Login";
  270. SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
  271. SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
  272. db.AddInParameter(dbCommand, "@PortalID", SqlDbType.UniqueIdentifier, Global.PortalID);
  273. db.AddInParameter(dbCommand, "@Email", SqlDbType.VarChar, email);
  274. db.AddInParameter(dbCommand, "@Password", SqlDbType.VarChar, PortalSecurity.Encrypt(password));
  275. IDataReader reader = db.ExecuteReader(dbCommand);
  276. IdentityUser returnValue = null;
  277. if (reader.Read())
  278. {
  279. returnValue = new IdentityUser(Convert.ToInt32(reader["ID"]), reader["Email"].ToString());
  280. }
  281. reader.Close();
  282. dbCommand.Connection.Close();
  283. return returnValue;
  284. }
  285. //-----------------------------------------------------------------------------------------
  286. public static int ChangeDepartment(int userID, string departmentID)
  287. {
  288. const string spName = "p_SYSTEM_User_ChangeDepartment";
  289. SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
  290. SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
  291. db.AddInParameter(dbCommand, "@ID", SqlDbType.Int, userID);
  292. db.AddInParameter(dbCommand, "@DepartmentID", SqlDbType.VarChar, departmentID);
  293. return db.ExecuteNonQuery(dbCommand);
  294. }
  295. }
  296. }