/Gradebook.Security/Providers/RoleProvider.cs
C# | 147 lines | 122 code | 25 blank | 0 comment | 13 complexity | 849184cd31e285405119774bf7409fb9 MD5 | raw file
- using Gradebook.Contracts.Repositories;
- using Gradebook.Model;
- using System;
- using System.Collections.Specialized;
- using System.Linq;
- using System.Web.Security;
-
- namespace Gradebook.Security.Providers
- {
- public class AccountRoleProvider : RoleProvider
- {
- public const string ProviderName = "AccountRoleProvider";
-
- private IAccountRepository _repository;
- private IAccountRepository AccountRepository
- {
- get { return _repository ?? ProvidersHelper.GetAccountRepository(); }
- set { _repository = value; }
- }
-
- public AccountRoleProvider() { }
- public AccountRoleProvider(IAccountRepository repository)
- {
- AccountRepository = repository;
- }
-
- public override void Initialize(string name, NameValueCollection config)
- {
- if (config == null)
- {
- throw new ArgumentNullException("config");
- }
- if (string.IsNullOrEmpty(name))
- {
- name = ProviderName;
- }
- if (string.IsNullOrEmpty(config["description"]))
- {
- config.Remove("description");
- config.Add("description", "Account Role Provider");
- }
-
- base.Initialize(name, config);
-
- ApplicationName = config["applicationName"];
- }
-
- public override bool IsUserInRole(string username, string roleName)
- {
- if (string.IsNullOrEmpty(username))
- {
- throw CreateArgumentNullOrEmptyException("username");
- }
- if (string.IsNullOrEmpty(roleName))
- {
- throw CreateArgumentNullOrEmptyException("roleName");
- }
-
- return AccountRepository.GetAll(roleName).Any(x => x.Name == username);
- }
-
- public override string[] GetRolesForUser(string username)
- {
- if (string.IsNullOrEmpty(username))
- {
- throw CreateArgumentNullOrEmptyException("username");
- }
-
- var user = AccountRepository.GetUserByName(username);
- return user == null ? new string[0] : new[] { user.Role.ToString() };
- }
-
- public override bool RoleExists(string roleName)
- {
- if (string.IsNullOrEmpty(roleName))
- {
- throw CreateArgumentNullOrEmptyException("roleName");
- }
-
- UserRole role;
- return Enum.TryParse(roleName, out role);
- }
-
- public override string[] GetUsersInRole(string roleName)
- {
- if (string.IsNullOrEmpty(roleName))
- {
- throw CreateArgumentNullOrEmptyException("roleName");
- }
-
- return AccountRepository.GetAll(roleName).Select(x => x.Name).ToArray();
- }
-
- public override string[] GetAllRoles()
- {
- return Enum.GetNames(typeof(UserRole));
- }
-
- public override string[] FindUsersInRole(string roleName, string usernameToMatch)
- {
- if (string.IsNullOrEmpty(roleName))
- {
- throw CreateArgumentNullOrEmptyException("roleName");
- }
- if (string.IsNullOrEmpty(usernameToMatch))
- {
- throw CreateArgumentNullOrEmptyException("usernameToMatch");
- }
-
- return AccountRepository
- .GetAll(roleName).Where(x => x.Name.Contains(usernameToMatch))
- .Select(x => x.Name).ToArray();
- }
-
- public override string ApplicationName { get; set; }
-
- private static ArgumentException CreateArgumentNullOrEmptyException(string paramName)
- {
- var errorString = string.Format("Argument cannot be null or empty: {0}", paramName);
- return new ArgumentException(errorString);
- }
-
- #region Not implemented
-
- public override void AddUsersToRoles(string[] usernames, string[] roleNames)
- {
- throw new NotImplementedException();
- }
-
- public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
- {
- throw new NotImplementedException();
- }
-
- public override void CreateRole(string roleName)
- {
- throw new NotImplementedException();
- }
-
- public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
- {
- throw new NotImplementedException();
- }
-
- #endregion
- }
- }