#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using Microsoft.AspNetCore.Mvc;
using System.ServiceModel.Security;
using System;
using Microsoft.AspNetCore.Http;
using HeuristicLab.Clients.Hive.WebJobManager.Services;
using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
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)
{
var q = new QueryWebClient(model, password);
if (q.CheckLogin())
{
ViewBag.Title = "Login succesful";
weblog.newLoginOKBOnly(model,q, password);
HttpContext.Session.SetString("UserId", model.userId.ToString());
return RedirectToAction("Index", "Query");
}
else
{
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);
}
}
///
/// Redirect user to home sceen for a full logout
///
///
public IActionResult Logout()
{
return RedirectToAction("Index", "Home");
}
#endregion
}
}