Esempio n. 1
0
        public Task <IList <ApplicationUser> > GetUsersInRoleAsync(string roleName, CancellationToken cToken)
        {
            string prefix = nameof(GetUsersInRoleAsync) + Constants.FNSUFFIX;

            IList <ApplicationUser> users = new List <ApplicationUser>();

            try
            {
                // Get the roleID from the roleName
                ApplicationRole appRole;
                using (var rolesDAL = new AspNetRolesDAL(_connStr)) { appRole = rolesDAL.SelectByRoleName(roleName.ToUpper()); }

                if (appRole != null)
                {
                    // Get the userIDs that have this roleID
                    IEnumerable <string> userIDsWithRoleID;
                    using (var userRolesDAL = new AspNetUserRolesDAL(_connStr)) { userIDsWithRoleID = userRolesDAL.SelectUsersInRole(appRole.RoleId); }

                    if (userIDsWithRoleID.Any())
                    {
                        // Get the users with these userIDs
                        using (var usersDAL = new AspNetUsersDAL(_connStr)) { users = usersDAL.SelectByUserIDs(userIDsWithRoleID).ToList(); }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
            }

            return(Task.FromResult(users));
        }
Esempio n. 2
0
        public Task <bool> IsInRoleAsync(ApplicationUser user, string roleName, CancellationToken cToken)
        {
            string prefix = nameof(IsInRoleAsync) + Constants.FNSUFFIX;

            bool result = false;

            try
            {
                // Get the role to get the roleID
                ApplicationRole appRole;
                using (var rolesDAL = new AspNetRolesDAL(_connStr)) { appRole = rolesDAL.SelectByRoleName(roleName.ToUpper()); }

                if (appRole != null)
                {
                    // Get the roleIDs for the user
                    IEnumerable <string> roleIDsForUser;
                    using (var userRolesDAL = new AspNetUserRolesDAL(_connStr)) { roleIDsForUser = userRolesDAL.SelectRolesForUser(user.UserId); }

                    foreach (string roleID in roleIDsForUser)
                    {
                        if (string.Equals(roleID, appRole.RoleId, StringComparison.OrdinalIgnoreCase))
                        {
                            result = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
            }

            return(Task.FromResult(result));
        }
Esempio n. 3
0
        public Task RemoveFromRoleAsync(ApplicationUser user, string roleName, CancellationToken cToken)
        {
            string prefix = nameof(RemoveFromRoleAsync) + Constants.FNSUFFIX;

            ApplicationRole appRole = null;

            try
            {
                using (var rolesDAL = new AspNetRolesDAL(_connStr)) { appRole = rolesDAL.SelectByRoleName(roleName.ToUpper()); }

                if (appRole != null)
                {
                    using (var userRolesDAL = new AspNetUserRolesDAL(_connStr)) { userRolesDAL.Delete(user.UserId, appRole.RoleId); }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
            }

            return(Task.CompletedTask);
        }
Esempio n. 4
0
        public Task <IdentityResult> UpdateAsync(ApplicationRole role, CancellationToken cancellationToken)
        {
            try
            {
                using (var rolesDAL = new AspNetRolesDAL(_connStr))
                {
                    rolesDAL.Update(role);
                }
            }
            catch (Exception ex)
            {
                List <IdentityError> idErrors = new List <IdentityError>();
                IdentityError        idError  = new IdentityError {
                    Description = ex.Message
                };
                idErrors.Add(idError);

                return(Task.FromResult(IdentityResult.Failed(idErrors.ToArray())));
            }

            return(Task.FromResult(IdentityResult.Success));
        }
Esempio n. 5
0
        public Task <IList <string> > GetRolesAsync(ApplicationUser user, CancellationToken cToken)
        {
            string prefix = nameof(GetRolesAsync) + Constants.FNSUFFIX;

            IList <string> roles = new List <string>();

            try
            {
                IEnumerable <string> roleIDs = null;
                using (var userRolesDAL = new AspNetUserRolesDAL(_connStr)) { roleIDs = userRolesDAL.SelectRolesForUser(user.UserId); }

                if (roleIDs.Any())
                {
                    using (var rolesDAL = new AspNetRolesDAL(_connStr)) { roles = rolesDAL.SelectRoleNamesByRoleId(roleIDs).ToList(); }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
            }

            return(Task.FromResult(roles));
        }
Esempio n. 6
0
        public Task <ApplicationRole> FindByNameAsync(string normalizedRoleName, CancellationToken cancellationToken)
        {
            string prefix = nameof(FindByNameAsync) + Constants.FNSUFFIX;

            ApplicationRole appRole = null;

            if (!string.IsNullOrWhiteSpace(normalizedRoleName))
            {
                try
                {
                    using (var rolesDAL = new AspNetRolesDAL(_connStr))
                    {
                        appRole = rolesDAL.SelectByRoleName(normalizedRoleName);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(prefix + $"Exception:[{ex.ToString()}]");
                }
            }

            return(Task.FromResult(appRole));
        }