Exemplo 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));
        }
Exemplo 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));
        }
Exemplo 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);
        }
Exemplo n.º 4
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));
        }