PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/BaliEnterpriseSystems/BaliEnterpriseSystems/BestObjects/CurrentUser.cs

https://github.com/sirivedula/BEST
C# | 219 lines | 194 code | 22 blank | 3 comment | 10 complexity | 1ec51e8ff806b4df4fe5f55fdef67ee9 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Collections.Generic;
  13. using BaliEnterpriseSystems.BestObjects;
  14. using System.Data.OleDb;
  15. namespace BaliEnterpriseSystems.BestObjects
  16. {
  17. public class CurrentUser
  18. {
  19. private string _IPAddress;
  20. private BestUser _bestuser;
  21. public UtilEMail emailUtil = new UtilEMail();
  22. public CurrentUser(string username)
  23. {
  24. this.UserName = username;
  25. }
  26. public BestUser BestUser
  27. {
  28. get
  29. {
  30. if (_bestuser == null)
  31. {
  32. _bestuser = new BestUser();
  33. BestField p1 = new BestField() { fieldName = "username", fieldSize = 40, fieldType = "System.String", paramOledbType = System.Data.OleDb.OleDbType.VarChar, displayField = false };
  34. p1.fieldValue = this.UserName;
  35. List<BestField> listp = new List<BestField>();
  36. listp.Add(p1);
  37. _bestuser.LoadRows("username = ?", listp);
  38. }
  39. return _bestuser;
  40. }
  41. }
  42. private ApplicationException _error;
  43. public ApplicationException Error
  44. {
  45. get
  46. {
  47. return _error;
  48. }
  49. }
  50. private string hashedPassword(string pwd)
  51. {
  52. return Utils.GetMD5Hash(pwd);
  53. }
  54. public Boolean loginIsValid(string password)
  55. {
  56. if (BestUser.logindisabled)
  57. {
  58. _error = new ApplicationException("Your account has been disabled the administrator.");
  59. return false;
  60. }
  61. string checkPassword = password;
  62. if (String.IsNullOrEmpty(this.BestUser.password) || !this.BestUser.password.Equals(this.hashedPassword(checkPassword), StringComparison.InvariantCulture))
  63. {
  64. _error = new ApplicationException("The username and/or password is not correct.");
  65. return false;
  66. }
  67. //check for account expiration
  68. if (this.BestUser.passwordExpiration < DateTime.Today)
  69. {
  70. _error = new ApplicationException("This account has expired. Please contact your center.");
  71. return false;
  72. }
  73. return true;
  74. }
  75. public bool loggedIn
  76. {
  77. get;
  78. set;
  79. }
  80. public string IPAddress
  81. {
  82. get { return _IPAddress; }
  83. set { _IPAddress = value; }
  84. }
  85. public void SleepWithMax(Int32 sleepMilliseconds, Int32 maxMillisecondstosleep)
  86. {
  87. System.Threading.Thread.Sleep(Math.Min(sleepMilliseconds, maxMillisecondstosleep));
  88. }
  89. /* Current User will be the person who logged-in into the System. */
  90. /* Implements Each Page Secure and Allow View, Add, Edit, Delete Rights */
  91. private List<CurrentUserRole> curuserRoles = null;
  92. public CurrentUserRole UserRoleByName(string rolename)
  93. {
  94. CurrentUserRole result = new CurrentUserRole();
  95. if (curuserRoles == null)
  96. {
  97. curuserRoles = new List<CurrentUserRole>();
  98. OleDbCommand myCmd = new BestDatabase().dbCmd;
  99. myCmd.CommandText = "select service, allowadd, allowedit, allowdelete, allowview from BestUserRoles where username = ?";
  100. OleDbParameter p1 = new OleDbParameter("username", OleDbType.VarChar, 200);
  101. p1.Value = this.UserName;
  102. myCmd.Parameters.Add(p1);
  103. OleDbDataReader reader = myCmd.ExecuteReader();
  104. while (reader.Read())
  105. {
  106. CurrentUserRole trole = new CurrentUserRole();
  107. trole.service = reader.GetValue(0).ToString();
  108. trole.allowAdd = reader.GetValue(1).ToString().Equals("True");
  109. trole.allowEdit = reader.GetValue(2).ToString().Equals("True");
  110. trole.allowDelete = reader.GetValue(3).ToString().Equals("True");
  111. trole.allowView = reader.GetValue(4).ToString().Equals("True");
  112. curuserRoles.Add(trole);
  113. }
  114. }
  115. foreach (CurrentUserRole role in curuserRoles)
  116. {
  117. if (role.service.Equals(rolename))
  118. {
  119. result = role;
  120. }
  121. }
  122. return result;
  123. }
  124. public string UserName
  125. {
  126. set;
  127. get;
  128. }
  129. public string CenterId
  130. {
  131. get
  132. {
  133. return HttpContext.Current.Session["UserCenter"].ToString();
  134. }
  135. }
  136. public List<BestField> CIdParam
  137. {
  138. get
  139. {
  140. List<BestField> lstParam = new List<BestField>();
  141. BestField bfld = new BestField()
  142. {
  143. fieldName = "CenterId",
  144. fieldSize = 50,
  145. fieldType = "System.String",
  146. paramOledbType = OleDbType.VarChar
  147. };
  148. bfld.fieldValue = this.CenterId;
  149. lstParam.Add(bfld);
  150. return lstParam;
  151. }
  152. }
  153. public BestField CenterIdField
  154. {
  155. get
  156. {
  157. BestField bfld = new BestField()
  158. {
  159. fieldName = "CenterId",
  160. fieldSize=50,
  161. fieldType = "System.String",
  162. paramOledbType = OleDbType.VarChar
  163. };
  164. bfld.fieldValue = this.CenterId;
  165. return bfld;
  166. }
  167. }
  168. public string[] Services
  169. {
  170. get
  171. {
  172. string[] result = { "1.Programs", "2.Students", "3.Tutors", "4.Payments", "5.Schedules", "6.Setup",
  173. "Payment - Details", "Payment - Reports", "Payment - Types", "Program - Information",
  174. "Program - Types", "Schedules - Attendance", "Setup - Center Information", "Setup - EMail Template",
  175. "Setup - Services", "Setup - User Roles", "Setup - Users", "Student - Information",
  176. "Student - Notes", "Student - Reports", "Tutor - Information", "Tutor - Reports" };
  177. return result;
  178. }
  179. }
  180. }
  181. public class CurrentUserRole
  182. {
  183. public string service;
  184. public bool allowAdd;
  185. public bool allowEdit;
  186. public bool allowView;
  187. public bool allowDelete;
  188. public CurrentUserRole()
  189. {
  190. service = "";
  191. allowAdd = false;
  192. allowDelete = false;
  193. allowEdit = false;
  194. allowView = false;
  195. }
  196. }
  197. }