1 | using HeuristicLab.Clients.Access;
|
---|
2 | using HeuristicLab.Clients.Common;
|
---|
3 | using HeuristicLab.Clients.Common.Properties;
|
---|
4 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
5 | using Microsoft.AspNet.Mvc;
|
---|
6 | using System.ServiceModel.Security;
|
---|
7 |
|
---|
8 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
9 | {
|
---|
10 | /// <summary>
|
---|
11 | /// Controller for initial landing page
|
---|
12 | /// </summary>
|
---|
13 | public class HomeController : Controller
|
---|
14 | {
|
---|
15 | private LoginViewModelService loginViewModelService;
|
---|
16 | private HiveServiceClient client;
|
---|
17 | public HomeController(ILoginViewModelService loginViewModelService)
|
---|
18 | {
|
---|
19 | this.loginViewModelService = (LoginViewModelService)loginViewModelService;
|
---|
20 | }
|
---|
21 | #region Login
|
---|
22 | /// <summary>
|
---|
23 | /// Opens initial home page
|
---|
24 | /// </summary>
|
---|
25 | /// <returns>View from home page</returns>
|
---|
26 | public IActionResult Index()
|
---|
27 | {
|
---|
28 | ViewBag.Title = "Login";
|
---|
29 | loginViewModelService.clear();
|
---|
30 | return View(loginViewModelService.GetLoginViewModel());
|
---|
31 | }
|
---|
32 | /// <summary>
|
---|
33 | /// Checks login
|
---|
34 | /// </summary>
|
---|
35 | /// <param name="loginName">Login name</param>
|
---|
36 | /// <param name="password">Login password</param>
|
---|
37 | /// <returns>Logged in view if correct or shows error</returns>
|
---|
38 | public IActionResult Login(string loginName, string password)//Checks login
|
---|
39 | {
|
---|
40 | if (!string.IsNullOrEmpty(loginName) && !string.IsNullOrEmpty(password))
|
---|
41 | {
|
---|
42 | var model = loginViewModelService.GetLoginViewModel();
|
---|
43 | HiveServiceLocatorWeb hiveServiceLocator = (HiveServiceLocatorWeb)HiveServiceLocatorWeb.Instance;
|
---|
44 | HeuristicLab.Clients.Common.Properties.Settings.Default.UserName = loginName;
|
---|
45 | HeuristicLab.Clients.Common.Properties.Settings.Default.Password = Common.CryptoService.EncryptString(password);
|
---|
46 | HeuristicLab.Clients.Common.Properties.Settings.Default.Save();
|
---|
47 | hiveServiceLocator.Username = loginName;
|
---|
48 | hiveServiceLocator.Password = password;
|
---|
49 |
|
---|
50 | client = hiveServiceLocator.getHiveServiceClient();
|
---|
51 | try {
|
---|
52 | var test = client.GetJobs();//Throws messageSecurityException if login failss
|
---|
53 | ViewBag.Title = "Login succesful";
|
---|
54 |
|
---|
55 | return RedirectToAction("Index","Job");
|
---|
56 | }
|
---|
57 | catch(MessageSecurityException e)
|
---|
58 | {
|
---|
59 | ViewBag.Title = "Login";
|
---|
60 | model.errorMessage = "Wrong login, try again";
|
---|
61 | return View("Index", model);
|
---|
62 | }
|
---|
63 |
|
---|
64 | }
|
---|
65 | else
|
---|
66 | {
|
---|
67 | ViewBag.Title = "Login";
|
---|
68 | var model = loginViewModelService.GetLoginViewModel();
|
---|
69 | model.errorMessage = "You should fill in both fields";
|
---|
70 | return View("Index", model);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | public IActionResult Logout()
|
---|
74 | {
|
---|
75 | return RedirectToAction("Index","Home");
|
---|
76 | }
|
---|
77 | #endregion
|
---|
78 | }
|
---|
79 | }
|
---|