Exemplo n.º 1
0
        /// <summary>
        /// Add user to new role.
        /// </summary>
        /// <param name="user">Target user.</param>
        /// <param name="roleName">Target role.</param>
        /// <returns></returns>
        public override async Task AddToRoleAsync(TUser user, string roleName)
        {
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException("'user' parameter null");
            }

            if (String.IsNullOrWhiteSpace(roleName))
            {
                throw new ArgumentException("'roleName' parameter cannot be null or empty");
            }

            TRole role = _roleRepo.FindByName(roleName);

            if (role == null)
            {
                throw new InvalidOperationException("Given role name not found");
            }

            TUserRole userRole = new TUserRole();

            userRole.UserId = user.Id;
            userRole.RoleId = role.Id;

            _userRoleRepo.Add(userRole);

            await Task.FromResult(0);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Find the role by it's name.
        /// </summary>
        /// <param name="roleName">Target role's name.</param>
        /// <returns>Returns the role if found; otherwise, returns null.</returns>
        public override async Task <TRole> FindByNameAsync(string roleName)
        {
            ThrowIfDisposed();

            TRole role = null;

            if (!String.IsNullOrWhiteSpace(roleName))
            {
                role = _repo.FindByName(roleName);
            }

            return(await Task.FromResult(role));
        }