Advertisement
Guest User

Untitled

a guest
Jun 6th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. public override bool ValidateUser(string username, string password)
  2. {
  3. bool isAuth = false;
  4.  
  5. if (GenUtil.IsNull(username) || GenUtil.IsNull(password))
  6. return isAuth;
  7. else
  8. {
  9. SPSecurity.RunWithElevatedPrivileges(delegate()
  10. {
  11.  
  12. var dbUser = db.CustomDbUsersLists.Where(x => x.username.Trim().ToLower() == username.Trim().ToLower()).FirstOrDefault();
  13.  
  14. if (dbUser != null)
  15. if (dbUser.password == password) // case sensitive
  16. isAuth = true;
  17.  
  18. });
  19. }
  20.  
  21. return isAuth; // isAuth is true in debug
  22. }
  23.  
  24. class CustomDbUsersRP : RoleProvider
  25. {
  26.  
  27. private CustomDbUsersContextDataContext db = new CustomDbUsersContextDataContext(@"Data Source=MYLOCALSERVERSharePoint;Initial Catalog=CustomDbUsers;Integrated Security=True;Enlist=False;Pooling=True;Min Pool Size=0;Max Pool Size=100;Connect Timeout=15");
  28.  
  29. private string theRoleName = "DynamicAdmins";
  30.  
  31. public override string[] GetUsersInRole(string roleName)
  32. {
  33. if (roleName.Trim().ToLower() != theRoleName.Trim().ToLower())
  34. return new string[] { };
  35. else
  36. {
  37. string[] users = new string[] { };
  38.  
  39. SPSecurity.RunWithElevatedPrivileges(delegate()
  40. {
  41.  
  42. users = db.CustomDbUsersLists.Where(x => x.isadmin == 1).Select(x => x.username).ToArray<string>();
  43.  
  44. });
  45.  
  46. return users;
  47.  
  48. }
  49. }
  50.  
  51. public override bool IsUserInRole(string username, string roleName)
  52. {
  53. if (roleName.Trim().ToLower() != theRoleName.Trim().ToLower() ||
  54. GenUtil.IsNull(username))
  55. return false;
  56. else
  57. {
  58.  
  59. bool ret = false;
  60.  
  61. SPSecurity.RunWithElevatedPrivileges(delegate()
  62. {
  63. if (db.CustomDbUsersLists.Where(x =>
  64. x.isadmin == 1 &&
  65. x.username.Trim().ToLower() == username.Trim().ToLower())
  66. .Any())
  67. ret = true;
  68. });
  69.  
  70. return ret;
  71.  
  72. }
  73. }
  74.  
  75. public override string[] GetAllRoles()
  76. {
  77. return new string[] { theRoleName };
  78. }
  79.  
  80. public override string[] FindUsersInRole(string roleName, string usernameToMatch)
  81. {
  82. if (roleName.Trim().ToLower() != theRoleName.Trim().ToLower() ||
  83. GenUtil.IsNull(usernameToMatch))
  84. return new string[] { };
  85. else
  86. {
  87. string[] users = new string[] { };
  88.  
  89. SPSecurity.RunWithElevatedPrivileges(delegate()
  90. {
  91.  
  92. users = db.CustomDbUsersLists.Where(x =>
  93. x.isadmin == 1 &&
  94. x.username.Trim().ToLower() == usernameToMatch.Trim().ToLower())
  95. .Select(x => x.username).ToArray<string>();
  96.  
  97. });
  98.  
  99. return users;
  100.  
  101. }
  102. }
  103.  
  104. public override string[] GetRolesForUser(string username)
  105. {
  106. if (GenUtil.IsNull(username))
  107. return new string[] { };
  108. else
  109. {
  110. bool isAdmin = false;
  111.  
  112. SPSecurity.RunWithElevatedPrivileges(delegate()
  113. {
  114. if (db.CustomDbUsersLists.Where(x =>
  115. x.isadmin == 1 &&
  116. x.username.Trim().ToLower() == username.Trim().ToLower())
  117. .Any())
  118. isAdmin = true;
  119. });
  120.  
  121. if (isAdmin)
  122. return new string[] { theRoleName };
  123. else
  124. return new string[] { };
  125.  
  126. }
  127. }
  128.  
  129. public override bool RoleExists(string roleName)
  130. {
  131. return roleName.Trim().ToLower() == theRoleName.Trim().ToLower();
  132. }
  133.  
  134. #region "NOT IMPLEMENTED"
  135.  
  136. public override void AddUsersToRoles(string[] usernames, string[] roleNames)
  137. {
  138. throw new NotImplementedException();
  139. }
  140.  
  141. public override string ApplicationName
  142. {
  143. get
  144. {
  145. throw new NotImplementedException();
  146. }
  147. set
  148. {
  149. throw new NotImplementedException();
  150. }
  151. }
  152.  
  153. public override void CreateRole(string roleName)
  154. {
  155. throw new NotImplementedException();
  156. }
  157.  
  158. public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
  159. {
  160. throw new NotImplementedException();
  161. }
  162.  
  163. public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
  164. {
  165. throw new NotImplementedException();
  166. }
  167.  
  168. #endregion
  169.  
  170. }
  171.  
  172. }
  173.  
  174. new string[] { theRoleName };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement