ControllerRolesManager

mail@pastecode.io avatar
unknown
csharp
2 years ago
1.8 kB
2
Indexable
Never
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PWEB_AulasP_2223.Models;
using PWEB_AulasP_2223.ViewModels;

namespace PWEB_AulasP_2223.Controllers
{
    public class UserRolesManagerController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private readonly RoleManager<IdentityRole> _roleManager;
        public UserRolesManagerController(UserManager<ApplicationUser> userManager,RoleManager<IdentityRole> roleManager)
        {
            _userManager = userManager;
            _roleManager = roleManager;
        }
        public async Task<IActionResult> Index()
        {
            var users = await _userManager.Users.ToListAsync();
            var userRoles = new List<UserRolesViewModel>();
            foreach (var u in users)
            {
                var user = new UserRolesViewModel();
                user.UserId = u.Id;
                user.UserName = u.UserName;
                user.PrimeiroNome = u.PrimeiroNome;
                user.UltimoNome = u.UltimoNome;
                user.Roles = await GetUserRoles(u);
                userRoles.Add(user);
            }
            return View(userRoles);
        }
        private async Task<List<string>> GetUserRoles(ApplicationUser user)
        {
            return new List<string>(await _userManager.GetRolesAsync(user));
        }
        public async Task<IActionResult> Details(string userId)
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> Details(List<ManageUserRolesViewModel> model,
       string userId)
        {
            /* código a criar */
            return RedirectToAction("Index");
        }
    }
}