PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Gradebook.Security/Providers/RoleProvider.cs

https://bitbucket.org/academium/gradebook
C# | 147 lines | 122 code | 25 blank | 0 comment | 13 complexity | 849184cd31e285405119774bf7409fb9 MD5 | raw file
  1. using Gradebook.Contracts.Repositories;
  2. using Gradebook.Model;
  3. using System;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using System.Web.Security;
  7. namespace Gradebook.Security.Providers
  8. {
  9. public class AccountRoleProvider : RoleProvider
  10. {
  11. public const string ProviderName = "AccountRoleProvider";
  12. private IAccountRepository _repository;
  13. private IAccountRepository AccountRepository
  14. {
  15. get { return _repository ?? ProvidersHelper.GetAccountRepository(); }
  16. set { _repository = value; }
  17. }
  18. public AccountRoleProvider() { }
  19. public AccountRoleProvider(IAccountRepository repository)
  20. {
  21. AccountRepository = repository;
  22. }
  23. public override void Initialize(string name, NameValueCollection config)
  24. {
  25. if (config == null)
  26. {
  27. throw new ArgumentNullException("config");
  28. }
  29. if (string.IsNullOrEmpty(name))
  30. {
  31. name = ProviderName;
  32. }
  33. if (string.IsNullOrEmpty(config["description"]))
  34. {
  35. config.Remove("description");
  36. config.Add("description", "Account Role Provider");
  37. }
  38. base.Initialize(name, config);
  39. ApplicationName = config["applicationName"];
  40. }
  41. public override bool IsUserInRole(string username, string roleName)
  42. {
  43. if (string.IsNullOrEmpty(username))
  44. {
  45. throw CreateArgumentNullOrEmptyException("username");
  46. }
  47. if (string.IsNullOrEmpty(roleName))
  48. {
  49. throw CreateArgumentNullOrEmptyException("roleName");
  50. }
  51. return AccountRepository.GetAll(roleName).Any(x => x.Name == username);
  52. }
  53. public override string[] GetRolesForUser(string username)
  54. {
  55. if (string.IsNullOrEmpty(username))
  56. {
  57. throw CreateArgumentNullOrEmptyException("username");
  58. }
  59. var user = AccountRepository.GetUserByName(username);
  60. return user == null ? new string[0] : new[] { user.Role.ToString() };
  61. }
  62. public override bool RoleExists(string roleName)
  63. {
  64. if (string.IsNullOrEmpty(roleName))
  65. {
  66. throw CreateArgumentNullOrEmptyException("roleName");
  67. }
  68. UserRole role;
  69. return Enum.TryParse(roleName, out role);
  70. }
  71. public override string[] GetUsersInRole(string roleName)
  72. {
  73. if (string.IsNullOrEmpty(roleName))
  74. {
  75. throw CreateArgumentNullOrEmptyException("roleName");
  76. }
  77. return AccountRepository.GetAll(roleName).Select(x => x.Name).ToArray();
  78. }
  79. public override string[] GetAllRoles()
  80. {
  81. return Enum.GetNames(typeof(UserRole));
  82. }
  83. public override string[] FindUsersInRole(string roleName, string usernameToMatch)
  84. {
  85. if (string.IsNullOrEmpty(roleName))
  86. {
  87. throw CreateArgumentNullOrEmptyException("roleName");
  88. }
  89. if (string.IsNullOrEmpty(usernameToMatch))
  90. {
  91. throw CreateArgumentNullOrEmptyException("usernameToMatch");
  92. }
  93. return AccountRepository
  94. .GetAll(roleName).Where(x => x.Name.Contains(usernameToMatch))
  95. .Select(x => x.Name).ToArray();
  96. }
  97. public override string ApplicationName { get; set; }
  98. private static ArgumentException CreateArgumentNullOrEmptyException(string paramName)
  99. {
  100. var errorString = string.Format("Argument cannot be null or empty: {0}", paramName);
  101. return new ArgumentException(errorString);
  102. }
  103. #region Not implemented
  104. public override void AddUsersToRoles(string[] usernames, string[] roleNames)
  105. {
  106. throw new NotImplementedException();
  107. }
  108. public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
  109. {
  110. throw new NotImplementedException();
  111. }
  112. public override void CreateRole(string roleName)
  113. {
  114. throw new NotImplementedException();
  115. }
  116. public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. #endregion
  121. }
  122. }