2020-04-15 15:13:38 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2020-04-08 13:07:15 +02:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using zero.Core.Api;
|
2020-04-15 15:13:38 +02:00
|
|
|
using zero.Core.Entities;
|
2020-04-16 00:56:22 +02:00
|
|
|
using zero.Core.Identity;
|
2020-04-15 15:13:38 +02:00
|
|
|
using zero.Web.Models;
|
2020-04-08 13:07:15 +02:00
|
|
|
|
|
|
|
|
namespace zero.Web.Controllers
|
|
|
|
|
{
|
2020-04-15 15:13:38 +02:00
|
|
|
[ZeroAuthorize(false)]
|
2020-04-08 13:07:15 +02:00
|
|
|
public class AuthenticationController : BackofficeController
|
|
|
|
|
{
|
2020-05-11 15:34:47 +02:00
|
|
|
IAuthenticationApi Api;
|
2020-05-21 13:41:09 +02:00
|
|
|
IApplicationContext AppContext;
|
2020-04-08 13:07:15 +02:00
|
|
|
|
2020-05-21 13:41:09 +02:00
|
|
|
|
|
|
|
|
public AuthenticationController(IAuthenticationApi api, IApplicationContext appContext)
|
2020-04-08 13:07:15 +02:00
|
|
|
{
|
|
|
|
|
Api = api;
|
2020-05-21 13:41:09 +02:00
|
|
|
AppContext = appContext;
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-05-11 11:30:56 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Returns the current user
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task<IActionResult> GetUser()
|
|
|
|
|
{
|
|
|
|
|
return await As<User, UserEditModel>(await Api.GetUser());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-04-08 13:07:15 +02:00
|
|
|
/// <summary>
|
2020-04-15 15:13:38 +02:00
|
|
|
/// If a user is logged in
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IActionResult IsLoggedIn()
|
2020-04-08 13:07:15 +02:00
|
|
|
{
|
2020-04-15 15:13:38 +02:00
|
|
|
return Json(EntityResult.Maybe(Api.IsLoggedIn()));
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Tries a login for a user with username/password
|
|
|
|
|
/// </summary>
|
2020-04-15 15:13:38 +02:00
|
|
|
[HttpPost]
|
|
|
|
|
public async Task<IActionResult> LoginUser([FromBody] LoginModel model)
|
2020-04-08 13:07:15 +02:00
|
|
|
{
|
2020-04-15 15:13:38 +02:00
|
|
|
EntityResult result = await Api.Login(model.Email, model.Password, model.IsPersistent);
|
|
|
|
|
return Json(result);
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logout for the current user
|
|
|
|
|
/// </summary>
|
2020-04-15 15:13:38 +02:00
|
|
|
[HttpPost, ZeroAuthorize]
|
2020-04-08 13:07:15 +02:00
|
|
|
public async Task<IActionResult> LogoutUser()
|
|
|
|
|
{
|
2020-04-15 15:13:38 +02:00
|
|
|
await Api.Logout();
|
|
|
|
|
return Json(EntityResult.Success());
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
2020-05-21 13:41:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Try to switch selected application for user
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpPost, ZeroAuthorize]
|
|
|
|
|
public async Task<IActionResult> SwitchApp(string appId)
|
|
|
|
|
{
|
|
|
|
|
User user = await Api.GetUser();
|
|
|
|
|
bool isSuccess = await AppContext.TrySwitchForUser(user, appId);
|
|
|
|
|
|
|
|
|
|
return Json(new EntityResult()
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = isSuccess
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-04-08 13:07:15 +02:00
|
|
|
}
|
|
|
|
|
}
|