diff --git a/Newsbot.Collector.Api/Controllers/AccountController.cs b/Newsbot.Collector.Api/Controllers/AccountController.cs index c0b2b36..a0c7d85 100644 --- a/Newsbot.Collector.Api/Controllers/AccountController.cs +++ b/Newsbot.Collector.Api/Controllers/AccountController.cs @@ -1,11 +1,10 @@ -using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Newsbot.Collector.Api.Domain; using Newsbot.Collector.Api.Domain.Requests; using Newsbot.Collector.Api.Domain.Response; using Newsbot.Collector.Api.Domain.Results; using Newsbot.Collector.Api.Services; -using Newsbot.Collector.Domain.Dto; -using Newsbot.Collector.Domain.Entities; namespace Newsbot.Collector.Api.Controllers; @@ -71,6 +70,21 @@ public class AccountController : ControllerBase return CheckIfSuccessful(response); } + [HttpPost("addRole")] + [Authorize(Roles = AuthorizationRoles.Administrators)] + public ActionResult AddRole([FromBody] AddRoleRequest request) + { + try + { + _identityService.AddRole(request.RoleName ?? "", request.UserId ?? ""); + return new OkResult(); + } + catch (Exception ex) + { + return new BadRequestResult(); + } + } + private ActionResult CheckIfSuccessful(AuthenticationResult result) { if (!result.IsSuccessful) diff --git a/Newsbot.Collector.Api/Domain/AuthorizationRoles.cs b/Newsbot.Collector.Api/Domain/AuthorizationRoles.cs new file mode 100644 index 0000000..079d6b8 --- /dev/null +++ b/Newsbot.Collector.Api/Domain/AuthorizationRoles.cs @@ -0,0 +1,6 @@ +namespace Newsbot.Collector.Api.Domain; + +public class AuthorizationRoles +{ + public const string Administrators = "Administrators"; +} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Domain/Requests/NewRoleRequest.cs b/Newsbot.Collector.Api/Domain/Requests/NewRoleRequest.cs new file mode 100644 index 0000000..706ebef --- /dev/null +++ b/Newsbot.Collector.Api/Domain/Requests/NewRoleRequest.cs @@ -0,0 +1,7 @@ +namespace Newsbot.Collector.Api.Domain.Requests; + +public class AddRoleRequest +{ + public string? RoleName { get; set; } + public string? UserId { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Services/IdentityService.cs b/Newsbot.Collector.Api/Services/IdentityService.cs index 4a48d0e..bd84b33 100644 --- a/Newsbot.Collector.Api/Services/IdentityService.cs +++ b/Newsbot.Collector.Api/Services/IdentityService.cs @@ -16,6 +16,7 @@ public interface IIdentityService AuthenticationResult Register(string email, string password); AuthenticationResult Login(string email, string password); AuthenticationResult RefreshToken(string token, string refreshToken); + void AddRole(string roleName, string userId); } public class IdentityService : IIdentityService @@ -178,6 +179,19 @@ public class IdentityService : IIdentityService return GenerateJwtToken(user.Result); } + public void AddRole(string roleName, string userId) + { + var user = _userManager.FindByIdAsync(userId); + user.Wait(); + + if (user.Result is null) + { + throw new Exception("User was not found"); + } + + _userManager.AddToRoleAsync(user.Result, roleName); + } + private ClaimsPrincipal? CheckTokenSigner(string token) { var tokenHandler = new JwtSecurityTokenHandler();