93 lines
3.1 KiB
C#
93 lines
3.1 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;
|
|
using System;
|
|
|
|
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([FromBody] AuthenticateRequest model)
|
|
{
|
|
var response = await _userService.LoginAD(model);
|
|
return Ok(response);
|
|
}
|
|
[AllowAnonymous]
|
|
[HttpPost("[action]")]
|
|
public async Task<IActionResult> LoginApiAD([FromBody] 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);
|
|
}
|
|
/* download the excel file */
|
|
[HttpGet("[action]")]
|
|
public async Task<IActionResult> GetReportFile()
|
|
{
|
|
// this.Response.Headers.Add("Access-Control-Expose-Headers", "Content-Disposition");
|
|
var result = await _userService.GetAllUserReport(); // .GetReport(fromDate, toDate);
|
|
string fileContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //Map(result.Data.FileName);
|
|
return File(result.Data.Content, fileContentType, result.Data.FileName);
|
|
}
|
|
}
|
|
}
|