roles
This commit is contained in:
parent
83f705ae7c
commit
e4703a65db
|
@ -1,8 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using api.Data;
|
||||
using api.Dtos;
|
||||
using api.Models;
|
||||
using AutoMapper;
|
||||
|
@ -25,11 +27,16 @@ namespace api.Controllers
|
|||
private readonly IConfiguration config;
|
||||
public UserManager<User> _userManager { get; }
|
||||
public SignInManager<User> _signInManager { get; }
|
||||
private readonly IMapper _mapper;
|
||||
public IUsersRepository _repo { get; }
|
||||
|
||||
public AuthController(IConfiguration config, UserManager<User> UserManager, SignInManager<User> SignInManager, IMapper mapper)
|
||||
private readonly IMapper _mapper;
|
||||
private readonly RoleManager<Role> _roleManager;
|
||||
|
||||
public AuthController(IConfiguration config, UserManager<User> UserManager, SignInManager<User> SignInManager, IMapper mapper, RoleManager<Role> roleManager, IUsersRepository repo)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_roleManager = roleManager;
|
||||
_repo = repo;
|
||||
this.config = config;
|
||||
_userManager = UserManager;
|
||||
_signInManager = SignInManager;
|
||||
|
@ -76,14 +83,20 @@ namespace api.Controllers
|
|||
|
||||
}
|
||||
|
||||
private string GenerateJwtToken(User user)
|
||||
private async Task<string> GenerateJwtToken(User user)
|
||||
{
|
||||
var claims = new[]
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
|
||||
new Claim(ClaimTypes.Name, user.UserName)
|
||||
};
|
||||
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
|
||||
foreach(var role in roles) {
|
||||
claims.Add(new Claim(ClaimTypes.Role,role));
|
||||
}
|
||||
|
||||
//obtem a key na app settings
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config.GetSection("AppSettings:Token").Value));
|
||||
|
||||
|
@ -111,5 +124,31 @@ namespace api.Controllers
|
|||
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
//adiciona uma nova role à base de dados
|
||||
[HttpPost("addRole")]
|
||||
public async Task<IActionResult> addRole( Role role) {
|
||||
|
||||
|
||||
_roleManager.CreateAsync(role).Wait();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//adiciona role a um utilizador
|
||||
[HttpPost("setRole")]
|
||||
public async Task<IActionResult> setRole(setRoleDto roleDto) {
|
||||
|
||||
User user = await _repo.GetUser(roleDto.Id);
|
||||
|
||||
_userManager.AddToRoleAsync(user, roleDto.RoleName).Wait();
|
||||
|
||||
return Ok();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ using api.Dtos;
|
|||
using api.Models;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
|
@ -18,9 +19,14 @@ namespace api.Controllers
|
|||
{
|
||||
private readonly IUsersRepository _repo;
|
||||
private readonly IMapper _mapper;
|
||||
public UsersController(IUsersRepository repo, IMapper mapper)
|
||||
private readonly RoleManager<Role> _roleManager;
|
||||
private readonly UserManager<User> _userManager;
|
||||
|
||||
public UsersController(IUsersRepository repo, IMapper mapper,RoleManager<Role> roleManager,UserManager<User> UserManager)
|
||||
{
|
||||
_mapper = mapper;
|
||||
_roleManager = roleManager;
|
||||
_userManager = UserManager;
|
||||
_repo = repo;
|
||||
|
||||
}
|
||||
|
@ -59,6 +65,32 @@ namespace api.Controllers
|
|||
|
||||
throw new System.Exception($"updating user {id} failed on save");
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
//adiciona uma nova role à base de dados
|
||||
[HttpPost("addRole")]
|
||||
public async Task<IActionResult> addRole( Role role) {
|
||||
|
||||
|
||||
_roleManager.CreateAsync(role).Wait();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//adiciona role a um utilizador
|
||||
[HttpPost("setRole")]
|
||||
public async Task<IActionResult> setRole(setRoleDto roleDto) {
|
||||
|
||||
User user = await _repo.GetUser(roleDto.Id);
|
||||
|
||||
_userManager.AddToRoleAsync(user, roleDto.RoleName).Wait();
|
||||
|
||||
return Ok();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -43,5 +43,6 @@ namespace api.Data
|
|||
return await _context.SaveChangesAsync()>0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace api.Dtos
|
||||
{
|
||||
public class setRoleDto
|
||||
{
|
||||
public int Id{get;set;}
|
||||
public string RoleName{get;set;}
|
||||
|
||||
}
|
||||
}
|
BIN
api/api.db
BIN
api/api.db
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1 +1 @@
|
|||
869d82264c4b3d49130c20e3bded0690a264c567
|
||||
fed17238e19cd1c3e9c95685e1391573562ae5d0
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue