Untitled
unknown
plain_text
7 months ago
2.0 kB
2
Indexable
Never
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using IntegracjaSystemow8.Model; using IntegracjaSystemow8.Entities; using IntegracjaSystemow8.Sevices.Users; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Server.HttpSys; namespace IntegracjaSystemow8.Controllers { [Route("api/[controller]")] [ApiController] public class UsersController : ControllerBase { private IUserService userService; public UsersController(IUserService userService) { this.userService = userService; } [HttpGet("authorization")] [Authorize(Roles = "admin", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public ActionResult<IEnumerable<User>> GetAllUsers() { var response = userService.GetAllUsers(); if (!User.IsInRole("admin")) return BadRequest(new { message = "Cos sie zepsulo, nie masz uprawnien Admina" }); return Ok(response); } [HttpGet("count")] [Authorize(Roles = "user", AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] public ActionResult<int> GetUsersCount() { var response = userService.GetUsersCount(); if (!User.IsInRole("user")) return BadRequest(new { message = "Cos sie zepsulo, nie masz uprawnien Usera" }); return Ok(response); } [HttpPost("authenticate")] public ActionResult Authenticate(AuthenticationRequest request) { var response = userService.Authenticate(request); if (response == null) return BadRequest(new{ message = "Username or password is incorrect" }); return Ok(response); } } }