83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using CarParkValidationAPI.Authorization;
|
|
using CarParkValidationAPI.Entities;
|
|
using CarParkValidationAPI.Models.Users;
|
|
using CarParkValidationAPI.Interface;
|
|
using System.Threading.Tasks;
|
|
using CarParkValidationAPI.Models;
|
|
|
|
namespace CarParkValidationAPI.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
public class UsersController : ControllerBase
|
|
{
|
|
private IUserService _userService;
|
|
|
|
public UsersController(IUserService userService)
|
|
{
|
|
_userService = userService;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost("[action]")]
|
|
public IActionResult Authenticate(AuthenticateRequest model)
|
|
{
|
|
var response = _userService.Authenticate(model);
|
|
|
|
|
|
return Ok(response);
|
|
}
|
|
[AllowAnonymous]
|
|
[HttpPost("[action]")]
|
|
public async Task<IActionResult> LoginAD(AuthenticateRequest model)
|
|
{
|
|
var response = await _userService.LoginAD(model);
|
|
return Ok(response);
|
|
}
|
|
[AllowAnonymous]
|
|
[HttpPost("[action]")]
|
|
public async Task<IActionResult> LoginApiAD(AuthenticateRequest model)
|
|
{
|
|
var response = await _userService.LoginApiAD(model);
|
|
return Ok(response);
|
|
}
|
|
[AllowAnonymous]
|
|
[HttpGet("[action]")]
|
|
public async Task<IActionResult> SearchApiStaff(string stafflinkNo)
|
|
{
|
|
var user = await _userService.SearchApiStaff(stafflinkNo);
|
|
return Ok(user);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet("[action]")]
|
|
public async Task<ResultModel<User>> SearchADStaff(string stafflinkNo)
|
|
{
|
|
var user = await _userService.SearchADStaff(stafflinkNo);
|
|
return user;
|
|
}
|
|
|
|
[Authorize(AdminRole.Admin)]
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetAllUsers()
|
|
{
|
|
var users = await _userService.GetAllUsers();
|
|
return Ok(users);
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
public async Task<IActionResult> GetById(int id)
|
|
{
|
|
// only admins can access other user records
|
|
var currentUser = (User)HttpContext.Items["User"];
|
|
if (id != currentUser.Id && currentUser.Role != AdminRole.Admin)
|
|
return Unauthorized(new { message = "Unauthorized" });
|
|
|
|
var user = await _userService.GetById(id);
|
|
return Ok(user);
|
|
}
|
|
}
|
|
}
|