using HeuristicLab.Clients.Access;
using HeuristicLab.Clients.Common;
using HeuristicLab.Clients.Common.Properties;
using HeuristicLab.Clients.Hive.WebJobManager.Services;
using Microsoft.AspNet.Mvc;
using System.ServiceModel.Security;
using Microsoft.AspNet.Http;
using System;
using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
{
///
/// Controller for initial landing page
///
public class HomeController : Controller
{
private WebLoginService weblog;
private HiveServiceClient client;
public HomeController()
{
this.weblog = WebLoginService.Instance;
}
#region Login
///
/// Opens initial home page
///
/// View from home page
public IActionResult Index()
{
ViewBag.Title = "Login";
var user = HttpContext.Session.GetString("UserId");
if(user != null && user != "")
{
Guid t = Guid.Parse(user);
weblog.logout(t);
HttpContext.Session.Clear();
}
return View(new LoginViewModel());
}
///
/// Checks login
///
/// Login name
/// Login password
/// Logged in view if correct or shows error
public IActionResult Login(string loginName, string password)//Checks login
{
if (!string.IsNullOrEmpty(loginName) && !string.IsNullOrEmpty(password))
{
var passE = Common.CryptoService.EncryptString(password);
var model = new LoginViewModel(loginName, passE);
HiveServiceLocatorWeb hiveServiceLocator = new HiveServiceLocatorWeb();
Common.Properties.Settings.Default.UserName = loginName;
Common.Properties.Settings.Default.Password = passE;
Common.Properties.Settings.Default.Save();
hiveServiceLocator.Username = loginName;
hiveServiceLocator.Password = password;//Not encrypted for login to service
hiveServiceLocator.UserId = model.userId;
client = hiveServiceLocator.getHiveServiceClient();
try {
var test = client.GetJobs();//Throws messageSecurityException if login failss
ViewBag.Title = "Login succesful";
weblog.newLogin(model, hiveServiceLocator);
HttpContext.Session.SetString("UserId", model.userId.ToString());
return RedirectToAction("Index","Job");
}
catch(MessageSecurityException e)
{
ViewBag.Title = "Login";
model = new LoginViewModel();
model.errorMessage = "Wrong login, try again";
return View("Index", model);
}
catch(SecurityAccessDeniedException e)
{
ViewBag.Title = "Access denied - Login";
model = new LoginViewModel();
model.errorMessage = "Access denied, you have no permission to use this application." +
" Contact a HeuristicLab Hive admin to gain access.";
return View("Index", model);
}
}
else
{
ViewBag.Title = "Login";
var model = new LoginViewModel();
model.errorMessage = "You should fill in both fields";
return View("Index", model);
}
}
public IActionResult Logout()
{
return RedirectToAction("Index","Home");
}
#endregion
}
}