Exemple #1
0
        public async Task Execute(ChangeRoleInput input)
        {
            if (input == null)
            {
                outputPort.WriteError(""); return;
            }

            var user = await userRepository.Get(input.UserId);

            if (user == null)
            {
                outputPort.NotFound(""); return;
            }

            bool success = await userRepository.ChangeRole(user.Id, input.RolesId);

            outputPort.Standart(new ChangeRoleOutput(success));
        }
        public async Task Execute(ChangeRoleInput input)
        {
            var currentUserId = currentUser.UserId;

            if (!await identityService.CheckRole(currentUserId, Roles.Admin))
            {
                outputPort.WriteError("You role is not Administrator");
                return;
            }

            var user = await identityService.FindByIdAsync(input.UserId);

            if (user == null)
            {
                outputPort.WriteError("User not found");
                return;
            }

            var _ = await identityService.GetUserRoles(input.UserId);

            var userRoles    = _.Select(x => x.Name);
            var desiredRoles = input.DesiredRoles;

            var toRemove = userRoles.Where(x => !desiredRoles.Contains(x));
            var toAdd    = desiredRoles.Where(x => !userRoles.Contains(x));

            foreach (var role in toRemove)
            {
                await identityService.RemoveRole(user.Id, role);
            }
            foreach (var role in toAdd)
            {
                await identityService.AddRole(user.Id, role);
            }

            var outputRoles = await identityService.GetUserRoles(input.UserId);

            outputPort.Standart(new ChangeRoleOutput(outputRoles));
        }