Ejemplo n.º 1
0
        public static async Task <IStatusGeneric <UserToRole> > AddRoleToUserAsync(
            string userId,
            string roleName,
            IAuthorizationRepository repository)
        {
            if (userId == null)
            {
                throw new ArgumentNullException(nameof(userId));
            }
            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }
            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            var        status     = new StatusGenericHandler <UserToRole>();
            UserToRole userToRole = await repository.GetUserToRoleAsync(userId, roleName);

            if (userToRole != null)
            {
                status.AddError($"The user already has the Role '{roleName}'.");
                return(status);
            }

            RoleToPermissions roleToAdd = await repository.GetRoleToPermissionAsync(roleName);

            if (roleToAdd == null)
            {
                status.AddError($"Could not find the Role '{roleName}'.");
                return(status);
            }

            return(status.SetResult(new UserToRole(userId, roleToAdd)));
        }