CustomPrincipal.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 void ProcessData(System.Security.Principal.IIdentity id, string data)
  30. {
  31. this.iIdentity = id;
  32. //Pengzw Add 2012-11-26
  33. var set = data.Split('|');
  34. this.LoginID = set[0];
  35. this.UserID = string.IsNullOrEmpty(set[1]) ? new Guid() : new Guid(set[1]);
  36. this.Name = set[2];
  37. this.StaffID = string.IsNullOrEmpty(set[3]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[3]));
  38. this.StudentID = string.IsNullOrEmpty(set[4]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[4]));
  39. this.DepartmentID = string.IsNullOrEmpty(set[5]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[5]));
  40. this.DepartmentHierarchyID = set[6];
  41. this.DepartmentName = set[7];
  42. this.CollegeID = string.IsNullOrEmpty(set[8]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[8]));
  43. //this.CampusID = string.IsNullOrEmpty(set[9]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[9]));
  44. //this.UniversityID = string.IsNullOrEmpty(set[10]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[10]));
  45. if (set.Length == 10)
  46. {
  47. this.RoleID = string.IsNullOrEmpty(set[9]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[9]));
  48. }
  49. }
  50. public void ProcessData(string data)
  51. {
  52. var set = data.Split('|');
  53. this.LoginID = set[0];
  54. this.UserID = string.IsNullOrEmpty(set[1]) ? new Guid() : new Guid(set[1]);
  55. this.Name = set[2];
  56. this.StaffID = string.IsNullOrEmpty(set[3]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[3]));
  57. this.StudentID = string.IsNullOrEmpty(set[4]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[4]));
  58. this.DepartmentID = string.IsNullOrEmpty(set[5]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[5]));
  59. this.DepartmentHierarchyID = set[6];
  60. this.DepartmentName = set[7];
  61. this.CollegeID = string.IsNullOrEmpty(set[8]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[8]));
  62. //this.CampusID = string.IsNullOrEmpty(set[9]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[9]));
  63. //this.UniversityID = string.IsNullOrEmpty(set[10]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[10]));
  64. if (set.Length == 10)
  65. {
  66. this.RoleID = string.IsNullOrEmpty(set[9]) ? new Nullable<Guid>() : new Nullable<Guid>(Guid.Parse(set[9]));
  67. }
  68. _current = this;
  69. }
  70. public List<string> GetUserData()
  71. {
  72. var userData = new List<string>();
  73. userData.Add(this.LoginID);
  74. userData.Add(this.UserID.ToString());
  75. userData.Add(this.Name);
  76. userData.Add(this.StaffID == null ? "" : this.StaffID.ToString());
  77. userData.Add(this.StudentID == null ? "" : this.StudentID.ToString());
  78. userData.Add(this.DepartmentID == null ? "" : this.DepartmentID.ToString());
  79. userData.Add(this.DepartmentHierarchyID);
  80. userData.Add(this.DepartmentName);
  81. userData.Add(this.CollegeID == null ? "" : this.CollegeID.ToString());
  82. //userData.Add(this.CampusID == null ? "" : this.CampusID.ToString());
  83. //userData.Add(this.UniversityID == null ? "" : this.UniversityID.ToString());
  84. userData.Add(this.RoleID == null ? "" : this.RoleID.ToString());
  85. return userData;
  86. }
  87. public System.Security.Principal.IIdentity Identity
  88. {
  89. get { return iIdentity; }
  90. }
  91. public bool IsInRole(string role)
  92. {
  93. if (!string.IsNullOrEmpty(Name))
  94. {
  95. return true;
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. private static CustomPrincipal _current;
  103. public static CustomPrincipal Current
  104. {
  105. get
  106. {
  107. try
  108. {
  109. if (HttpContext.Current == null)
  110. {
  111. return _current;
  112. }
  113. return ((CustomPrincipal)HttpContext.Current.User);
  114. }
  115. catch
  116. {
  117. return null;
  118. }
  119. }
  120. }
  121. }
  122. }