示例#1
0
        private bool RemoveRoleFromUser(YearApPermissionLevels role, ApplicationUser user)
        {
            var roleName = role.ToString();

            if (!_userManager.IsInRoleAsync(user, roleName).Result)
            {
                _logger.LogWarning("User not in role {}", roleName);
                return(false);
            }
            if (role == YearApPermissionLevels.Admin)
            {
                if (_userManager.GetUsersInRoleAsync(role.ToString()).Result.Count() == 1)
                {
                    throw new InvalidOperationException("There must be at least one admin user!");
                }
            }
            return(_userManager.RemoveFromRoleAsync(user, roleName).Result.Succeeded);
        }
示例#2
0
        private bool AddAspNetRoleToUser(YearApPermissionLevels role, ApplicationUser user)
        {
            var roleName = role.ToString();

            if (_userManager.IsInRoleAsync(user, roleName).Result)
            {
                _logger.LogWarning("User already in role {}", roleName);
                return(false);
            }
            return(_userManager.AddToRoleAsync(user, roleName).Result.Succeeded);
        }
示例#3
0
        private bool CreateRole(YearApPermissionLevels role)
        {
            var roleName = role.ToString();

            if (_roleManager.RoleExistsAsync(roleName).Result)
            {
                _logger.LogWarning("Role {} already exists", roleName);
                return(false);
            }

            return(_roleManager.CreateAsync(new IdentityRole(roleName)).Result.Succeeded);
        }