auth
This commit is contained in:
parent
e4703a65db
commit
f160985f88
|
@ -0,0 +1,20 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace api.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AdminController : ControllerBase
|
||||
{
|
||||
[Authorize(Policy= "RequireAdminRole")]
|
||||
[HttpGet("userWithRoles")]
|
||||
public IActionResult GetUsersWithRole() {
|
||||
|
||||
return Ok("Só admins");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -124,30 +124,6 @@ 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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -77,8 +77,6 @@ namespace api.Controllers
|
|||
return Ok();
|
||||
}
|
||||
|
||||
|
||||
|
||||
//adiciona role a um utilizador
|
||||
[HttpPost("setRole")]
|
||||
public async Task<IActionResult> setRole(setRoleDto roleDto) {
|
||||
|
|
|
@ -79,6 +79,10 @@ namespace api
|
|||
};
|
||||
});
|
||||
|
||||
services.AddAuthorization(options => {
|
||||
options.AddPolicy("RequireAdminRole",policy => policy.RequireRole("Admin"));
|
||||
//adicionar mais roles aqui
|
||||
});
|
||||
|
||||
services.AddMvc(Options=>
|
||||
{
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -1 +1 @@
|
|||
fed17238e19cd1c3e9c95685e1391573562ae5d0
|
||||
5c42e60f9517460e0b20965eea4adfe8a31af8a1
|
||||
|
|
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue