Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Controllers/HomeController.cs @ 13782

Last change on this file since 13782 was 13741, checked in by jlodewyc, 9 years ago

#2582 Users: Load, show info, update and add

File size: 4.0 KB
RevLine 
[13656]1using HeuristicLab.Clients.Access;
2using HeuristicLab.Clients.Common;
3using HeuristicLab.Clients.Common.Properties;
4using HeuristicLab.Clients.Hive.WebJobManager.Services;
5using Microsoft.AspNet.Mvc;
6using System.ServiceModel.Security;
[13739]7using Microsoft.AspNet.Http;
8using System;
9using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
[13656]10
11namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
12{
[13733]13    /// <summary>
14    /// Controller for initial landing page
15    /// </summary>
[13656]16    public class HomeController : Controller
17    {
[13739]18        private WebLoginService weblog;
[13656]19        private HiveServiceClient client;
[13739]20        public HomeController()
[13656]21        {
[13739]22            this.weblog = WebLoginService.Instance;
[13656]23        }
[13733]24        #region Login
25        /// <summary>
26        /// Opens initial home page
27        /// </summary>
28        /// <returns>View from home page</returns>
[13656]29        public IActionResult Index()
30        {
31            ViewBag.Title = "Login";
[13739]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
[13656]41        }
[13733]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
[13656]49        {
50            if (!string.IsNullOrEmpty(loginName) && !string.IsNullOrEmpty(password))
51            {
[13739]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();
[13656]58                hiveServiceLocator.Username = loginName;
[13741]59                hiveServiceLocator.Password = password;//Not encrypted for login to service
[13739]60                hiveServiceLocator.UserId = model.userId;
[13656]61
62                client = hiveServiceLocator.getHiveServiceClient();
63                try {
64                    var test = client.GetJobs();//Throws messageSecurityException if login failss
65                    ViewBag.Title = "Login succesful";
[13739]66                    weblog.newLogin(model, hiveServiceLocator);
67                    HttpContext.Session.SetString("UserId", model.userId.ToString());
[13733]68                    return RedirectToAction("Index","Job");
[13656]69                }
70                catch(MessageSecurityException e)
71                {
72                    ViewBag.Title = "Login";
[13739]73                    model = new LoginViewModel();
[13656]74                    model.errorMessage = "Wrong login, try again";
75                    return View("Index", model);
76                }
[13741]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                }
[13656]85
86            }
87            else
88            {
89                ViewBag.Title = "Login";
[13739]90                var model = new LoginViewModel();
[13656]91                model.errorMessage = "You should fill in both fields";
92                return View("Index", model);
93            }
94        }
[13733]95        public IActionResult Logout()
96        {
97            return RedirectToAction("Index","Home");
98        }
99        #endregion
[13656]100    }
101}
Note: See TracBrowser for help on using the repository browser.