CustomPrincipal.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Bowin.Common;
  6. using System.Security.Principal;
  7. using System.Web;
  8. namespace EMIS.Utility.FormValidate
  9. {
  10. public class CustomPrincipal : ICustomPrincipal
  11. {
  12. IIdentity iIdentity;
  13. public Guid UserID { get; set; }
  14. public string LoginID { get; set; }
  15. public string Name { get; set; }
  16. public Guid? StaffID { get; set; }
  17. public Guid? StudentID { get; set; }
  18. public Guid? CollegeID { get; set; }
  19. public Guid? CampusID { get; set; }
  20. public Guid? UniversityID { get; set; }
  21. //For teacher
  22. public Guid? DepartmentID { get; set; }
  23. public string DepartmentHierarchyID { get; set; }
  24. public string DepartmentName { get; set; }
  25. public bool IsStudent {
  26. get { return StudentID.HasValue; }
  27. }
  28. public Guid? RoleID { get; set; }
  29. public bool? NeedChangePwd { get; set; }
  30. public string NeedChangePwdReason { get; set; }
  31. public void ProcessData(System.Security.Principal.IIdentity id, string data)
  32. {
  33. this.iIdentity = id;
  34. //Pengzw Add 2012-11-26
  35. var set = data.Split('|');
  36. this.LoginID = set[0];
  37. this.UserID = string.IsNullOrEmpty(set[1]) ? new Guid() : new Guid(set[1]);
  38. this.Name = set[2];
  39. this.StaffID = string.IsNullOrEmpty(set[3]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[3]));
  40. this.StudentID = string.IsNullOrEmpty(set[4]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[4]));
  41. this.DepartmentID = string.IsNullOrEmpty(set[5]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[5]));
  42. this.DepartmentHierarchyID = set[6];
  43. this.DepartmentName = set[7];
  44. this.NeedChangePwd = string.IsNullOrEmpty(set[8]) ? false : (set[8] == "1");
  45. this.NeedChangePwdReason = set[9];
  46. this.CollegeID = string.IsNullOrEmpty(set[10]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[10]));
  47. //this.CampusID = string.IsNullOrEmpty(set[9]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[9]));
  48. //this.UniversityID = string.IsNullOrEmpty(set[10]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[10]));
  49. if (set.Length == 12)
  50. {
  51. this.RoleID = string.IsNullOrEmpty(set[11]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[11]));
  52. }
  53. }
  54. public void ProcessData(string data)
  55. {
  56. var set = data.Split('|');
  57. this.LoginID = set[0];
  58. this.UserID = string.IsNullOrEmpty(set[1]) ? new Guid() : new Guid(set[1]);
  59. this.Name = set[2];
  60. this.StaffID = string.IsNullOrEmpty(set[3]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[3]));
  61. this.StudentID = string.IsNullOrEmpty(set[4]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[4]));
  62. this.DepartmentID = string.IsNullOrEmpty(set[5]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[5]));
  63. this.DepartmentHierarchyID = set[6];
  64. this.DepartmentName = set[7];
  65. this.NeedChangePwd = string.IsNullOrEmpty(set[8]) ? false : (set[8] == "1");
  66. this.NeedChangePwdReason = set[9];
  67. this.CollegeID = string.IsNullOrEmpty(set[10]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[10]));
  68. //this.CampusID = string.IsNullOrEmpty(set[9]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[9]));
  69. //this.UniversityID = string.IsNullOrEmpty(set[10]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[10]));
  70. if (set.Length == 12)
  71. {
  72. this.RoleID = string.IsNullOrEmpty(set[11]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[11]));
  73. }
  74. _current = this;
  75. }
  76. public List<string> GetUserData()
  77. {
  78. var userData = new List<string>();
  79. userData.Add(this.LoginID);
  80. userData.Add(this.UserID.ToString());
  81. userData.Add(this.Name);
  82. userData.Add(this.StaffID == null ? "" : this.StaffID.ToString());
  83. userData.Add(this.StudentID == null ? "" : this.StudentID.ToString());
  84. userData.Add(this.DepartmentID == null ? "" : this.DepartmentID.ToString());
  85. userData.Add(this.DepartmentHierarchyID);
  86. userData.Add(this.DepartmentName);
  87. userData.Add(this.NeedChangePwd == null ? "" : this.NeedChangePwd.Value ? "1" : "");
  88. userData.Add(this.NeedChangePwdReason);
  89. userData.Add(this.CollegeID == null ? "" : this.CollegeID.ToString());
  90. //userData.Add(this.CampusID == null ? "" : this.CampusID.ToString());
  91. //userData.Add(this.UniversityID == null ? "" : this.UniversityID.ToString());
  92. userData.Add(this.RoleID == null ? "" : this.RoleID.ToString());
  93. return userData;
  94. }
  95. public System.Security.Principal.IIdentity Identity
  96. {
  97. get { return iIdentity; }
  98. }
  99. public bool IsInRole(string role)
  100. {
  101. if (!string.IsNullOrEmpty(Name))
  102. {
  103. return true;
  104. }
  105. else
  106. {
  107. return false;
  108. }
  109. }
  110. private static CustomPrincipal _current;
  111. public static CustomPrincipal Current
  112. {
  113. get
  114. {
  115. try
  116. {
  117. if (HttpContext.Current == null)
  118. {
  119. return _current;
  120. }
  121. return ((CustomPrincipal)HttpContext.Current.User);
  122. }
  123. catch
  124. {
  125. return null;
  126. }
  127. }
  128. }
  129. }
  130. }