using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using GreenTree.Strohrmann.ERP.Core.Helper;
using GreenTree.Strohrmann.ERP.Domain.Model;
using GreenTree.Strohrmann.ERP.Services.Authentication;
using GreenTree.Strohrmann.ERP.Services.Authorization;
using GreenTree.Strohrmann.ERP.Web.Models.Account;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
namespace GreenTree.Strohrmann.ERP.Web.Controllers
{
public class AccountController : Controller
{
#region DI fields
// The global DbContext
private readonly ERPDbContext _eRPDbContext;
// The global authentication service
private readonly IAuthenticationService _authenticationService;
// The global user helper
private readonly IUserHelper _userHelper;
// The global administation options
private readonly AdministrationOptions _administrationOptions;
#endregion
#region Ctor
///
/// Initializes a new instance of the AccountController class
///
/// Global DbContext.
/// Global authentication service.
/// Global user helper.
/// Global administration options.
public AccountController(
ERPDbContext eRPDbContext,
IAuthenticationService authenticationService,
IUserHelper userHelper,
AdministrationOptions administrationOptions)
{
_eRPDbContext = eRPDbContext;
_authenticationService = authenticationService;
_userHelper = userHelper;
_administrationOptions = administrationOptions;
}
#endregion
#region Actions
// Index View / redirect to Login
public IActionResult Index()
{
return RedirectToAction(nameof(Login));
}
// Login View
public IActionResult Login()
{
return View("~/Views/Account/Login.cshtml");
}
// POST: Account/Login
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Login(LoginModel login)
{
if (!ModelState.IsValid)
{
login.Password = String.Empty;
return View("~/Views/Account/Login.cshtml", login);
}
if (login.Username == _administrationOptions.Administrator)
{
_authenticationService.SignInAdmin(login.StayLoggedIn);
return RedirectToAction("Index", "Home");
}
else
{
_authenticationService.SignIn(login.Username, login.StayLoggedIn);
return RedirectToAction("Index", "Home");
}
}
// Logout View
public IActionResult Logout()
{
_authenticationService.SignOut();
return RedirectToAction(nameof(Login));
}
#endregion
}
}