123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System.ComponentModel.DataAnnotations;
- using Luticate2.Auth.Attributes;
- using Luticate2.Auth.Business;
- using Luticate2.Auth.Dbo.Basic;
- using Luticate2.Auth.Dbo.Permissions;
- using Luticate2.Auth.Dbo.Users;
- using Luticate2.Auth.Interfaces.Users;
- using Luticate2.Utils.Controllers;
- using Luticate2.Utils.Dbo.Basic;
- using Luticate2.Utils.Dbo.PaginatedRequest;
- using Luticate2.Utils.Utils;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
-
- namespace Luticate2.Auth.Controllers
- {
- [Route(LuAuthExtensions.RoutePrefix + "/[controller]")]
- public class LuUsersController : LuController
- {
- private const string EntityType = LuEntityTypes.LuUsers;
-
- private const string ReadPermission = LuPermissions.LuGroupsRead;
-
- private const string WritePermission = LuPermissions.LuGroupsWrite;
-
- private readonly ILuUsersBusiness _busines;
- private readonly LuAuthOptionsDbo _luAuthOptions;
-
- public LuUsersController(ILuUsersBusiness busines, IOptions<LuUtilsOptionsDbo> luUtilsOptionsDbo,
- IOptions<LuAuthOptionsDbo> luAuthOptions) : base(luUtilsOptionsDbo)
- {
- _busines = busines;
- _luAuthOptions = luAuthOptions.Value;
- }
-
- [HttpGet("{id}")]
- [LuPermission(ReadPermission, EntityType)]
- public LuApiWrapperDbo<LuUsersDbo> GetSingleById([LuPermissionArg][Required]string id)
- {
- return Handle(_busines.GetSingleById(id).ToLite());
- }
-
- [HttpGet]
- [LuPermission(ReadPermission, EntityType)]
- public LuApiWrapperDbo<LuPaginatedDbo<LuUsersDbo>> GetMultiple([Required]LuPaginatedRequestDbo request)
- {
- return Handle(_busines.GetMultiple(request).ToLite());
- }
-
- [HttpPost]
- [LuPermission(WritePermission, EntityType)]
- public LuApiWrapperDbo<LuUsersDbo> Register([Required]LuUsersAddDbo data)
- {
- return Handle(_busines.Register(data).ToLite());
- }
-
- [HttpPost("{id}")]
- [LuPermission(WritePermission, EntityType)]
- public LuApiWrapperDbo<LuUsersDbo> Edit([LuPermissionArg][Required]string id, [Required]LuUsersEditDbo data)
- {
- return Handle(_busines.Edit(id, data).ToLite());
- }
-
- [HttpDelete("{id}")]
- [LuPermission(WritePermission, EntityType)]
- public LuApiWrapperDbo<LuUsersDbo> Delete([LuPermissionArg][Required]string id)
- {
- return Handle(_busines.DeleteSingleByIdDbo(id).ToLite());
- }
-
- [HttpPost("login")]
- public LuApiWrapperDbo<LuUsersLoginResultDbo> Login([Required]string username, [Required]string password)
- {
- var loginRes = _busines.Login(username, password);
- if (loginRes)
- {
- Response.Cookies.Append(LuAuthExtensions.TokenCookieName, loginRes.Data.Token, new CookieOptions
- {
- HttpOnly = true,
- Secure = _luAuthOptions.SecureCookies
- });
- }
- return Handle(loginRes);
- }
-
- [HttpPost("logout")]
- public LuApiWrapperDbo<bool> Logout(/*string token*/)
- {
- // var t = token;
- // if (t == null)
- // {
- // t = HttpContext.GetLuUserToken();
- // }
- var t = HttpContext.GetLuUserToken();
- var logoutRes = _busines.Logout(t);
- if (logoutRes/* && (token == null || t == token)*/)
- {
- Response.Cookies.Delete(LuAuthExtensions.TokenCookieName);
- }
- return Handle(logoutRes);
- }
-
- [HttpGet("me")]
- public LuApiWrapperDbo<LuUsersDbo> Me()
- {
- return Handle(_busines.Me().ToLite());
- }
-
- [HttpGet("sessions")]
- public LuApiWrapperDbo<LuPaginatedDbo<LuUsersTokenDbo>> Sessions(LuPaginatedRequestDbo paginatedRequestDbo)
- {
- return Handle(_busines.Sessions(paginatedRequestDbo));
- }
- }
- }
|