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 | using Microsoft.AspNet.Http;
|
---|
8 | using System;
|
---|
9 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
10 |
|
---|
11 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
12 | {
|
---|
13 | /// <summary>
|
---|
14 | /// Controller for initial landing page
|
---|
15 | /// </summary>
|
---|
16 | public class HomeController : Controller
|
---|
17 | {
|
---|
18 | private WebLoginService weblog;
|
---|
19 | private HiveServiceClient client;
|
---|
20 | public HomeController()
|
---|
21 | {
|
---|
22 | this.weblog = WebLoginService.Instance;
|
---|
23 | }
|
---|
24 | #region Login
|
---|
25 | /// <summary>
|
---|
26 | /// Opens initial home page
|
---|
27 | /// </summary>
|
---|
28 | /// <returns>View from home page</returns>
|
---|
29 | public IActionResult Index()
|
---|
30 | {
|
---|
31 | ViewBag.Title = "Login";
|
---|
32 | var user = HttpContext.Session.GetString("UserId");
|
---|
33 | if(user != null && user != "")
|
---|
34 | {
|
---|
35 | Guid t = Guid.Parse(user);
|
---|
36 | weblog.logout(t);
|
---|
37 | HttpContext.Session.Clear();
|
---|
38 | }
|
---|
39 | return View(new LoginViewModel());
|
---|
40 |
|
---|
41 | }
|
---|
42 | /// <summary>
|
---|
43 | /// Checks login
|
---|
44 | /// </summary>
|
---|
45 | /// <param name="loginName">Login name</param>
|
---|
46 | /// <param name="password">Login password</param>
|
---|
47 | /// <returns>Logged in view if correct or shows error</returns>
|
---|
48 | public IActionResult Login(string loginName, string password)//Checks login
|
---|
49 | {
|
---|
50 | if (!string.IsNullOrEmpty(loginName) && !string.IsNullOrEmpty(password))
|
---|
51 | {
|
---|
52 | var passE = Common.CryptoService.EncryptString(password);
|
---|
53 | var model = new LoginViewModel(loginName, passE);
|
---|
54 | HiveServiceLocatorWeb hiveServiceLocator = new HiveServiceLocatorWeb();
|
---|
55 | Common.Properties.Settings.Default.UserName = loginName;
|
---|
56 | Common.Properties.Settings.Default.Password = passE;
|
---|
57 | Common.Properties.Settings.Default.Save();
|
---|
58 | hiveServiceLocator.Username = loginName;
|
---|
59 | hiveServiceLocator.Password = password;//Not encrypted for login to service
|
---|
60 | hiveServiceLocator.UserId = model.userId;
|
---|
61 |
|
---|
62 | client = hiveServiceLocator.getHiveServiceClient();
|
---|
63 | try {
|
---|
64 | var test = client.GetJobs();//Throws messageSecurityException if login failss
|
---|
65 | ViewBag.Title = "Login succesful";
|
---|
66 | weblog.newLogin(model, hiveServiceLocator);
|
---|
67 | HttpContext.Session.SetString("UserId", model.userId.ToString());
|
---|
68 | return RedirectToAction("Index","Job");
|
---|
69 | }
|
---|
70 | catch(MessageSecurityException e)
|
---|
71 | {
|
---|
72 | ViewBag.Title = "Login";
|
---|
73 | model = new LoginViewModel();
|
---|
74 | model.errorMessage = "Wrong login, try again";
|
---|
75 | return View("Index", model);
|
---|
76 | }
|
---|
77 | catch(SecurityAccessDeniedException e)
|
---|
78 | {
|
---|
79 | ViewBag.Title = "Access denied - Login";
|
---|
80 | model = new LoginViewModel();
|
---|
81 | model.errorMessage = "Access denied, you have no permission to use this application." +
|
---|
82 | " Contact a HeuristicLab Hive admin to gain access.";
|
---|
83 | return View("Index", model);
|
---|
84 | }
|
---|
85 |
|
---|
86 | }
|
---|
87 | else
|
---|
88 | {
|
---|
89 | ViewBag.Title = "Login";
|
---|
90 | var model = new LoginViewModel();
|
---|
91 | model.errorMessage = "You should fill in both fields";
|
---|
92 | return View("Index", model);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | /// <summary>
|
---|
96 | /// Redirect user to home sceen for a full logout
|
---|
97 | /// </summary>
|
---|
98 | /// <returns></returns>
|
---|
99 | public IActionResult Logout()
|
---|
100 | {
|
---|
101 | return RedirectToAction("Index","Home");
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 | }
|
---|
105 | }
|
---|