Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17842 was 13862, checked in by jlodewyc, 8 years ago

#2582 Start angular OKB manager, data loaded

File size: 5.5 KB
RevLine 
[13860]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
[13862]22
[13860]23using Microsoft.AspNetCore.Mvc;
[13656]24using System.ServiceModel.Security;
[13739]25using System;
[13862]26using Microsoft.AspNetCore.Http;
27using HeuristicLab.Clients.Hive.WebJobManager.Services;
[13739]28using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
[13862]29using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
[13656]30
31namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
32{
[13733]33    /// <summary>
34    /// Controller for initial landing page
35    /// </summary>
[13656]36    public class HomeController : Controller
37    {
[13739]38        private WebLoginService weblog;
[13656]39        private HiveServiceClient client;
[13739]40        public HomeController()
[13656]41        {
[13739]42            this.weblog = WebLoginService.Instance;
[13656]43        }
[13733]44        #region Login
45        /// <summary>
46        /// Opens initial home page
47        /// </summary>
48        /// <returns>View from home page</returns>
[13656]49        public IActionResult Index()
50        {
51            ViewBag.Title = "Login";
[13739]52            var user = HttpContext.Session.GetString("UserId");
[13854]53            if (user != null && user != "")
[13739]54            {
55                Guid t = Guid.Parse(user);
56                weblog.logout(t);
57                HttpContext.Session.Clear();
58            }
59            return View(new LoginViewModel());
60
[13656]61        }
[13733]62        /// <summary>
63        /// Checks login
64        /// </summary>
65        /// <param name="loginName">Login name</param>
66        /// <param name="password">Login password</param>
67        /// <returns>Logged in view if correct or shows error</returns>
68        public IActionResult Login(string loginName, string password)//Checks login
[13656]69        {
70            if (!string.IsNullOrEmpty(loginName) && !string.IsNullOrEmpty(password))
71            {
[13739]72                var passE = Common.CryptoService.EncryptString(password);
73                var model = new LoginViewModel(loginName, passE);
74                HiveServiceLocatorWeb hiveServiceLocator = new HiveServiceLocatorWeb();
75                Common.Properties.Settings.Default.UserName = loginName;
76                Common.Properties.Settings.Default.Password = passE;
77                Common.Properties.Settings.Default.Save();
[13656]78                hiveServiceLocator.Username = loginName;
[13741]79                hiveServiceLocator.Password = password;//Not encrypted for login to service
[13739]80                hiveServiceLocator.UserId = model.userId;
[13656]81
82                client = hiveServiceLocator.getHiveServiceClient();
[13854]83                try
84                {
[13656]85                    var test = client.GetJobs();//Throws messageSecurityException if login failss
86                    ViewBag.Title = "Login succesful";
[13739]87                    weblog.newLogin(model, hiveServiceLocator);
88                    HttpContext.Session.SetString("UserId", model.userId.ToString());
[13854]89                    return RedirectToAction("Index", "Job");
[13656]90                }
[13854]91                catch (MessageSecurityException e)
[13656]92                {
93                    ViewBag.Title = "Login";
[13739]94                    model = new LoginViewModel();
[13656]95                    model.errorMessage = "Wrong login, try again";
96                    return View("Index", model);
97                }
[13854]98                catch (SecurityAccessDeniedException e)
[13741]99                {
[13854]100                    var q = new QueryWebClient(model, password);
101                    if (q.CheckLogin())
102                    {
103                        ViewBag.Title = "Login succesful";
[13862]104                        weblog.newLoginOKBOnly(model,q, password);
[13854]105                        HttpContext.Session.SetString("UserId", model.userId.ToString());
106                        return RedirectToAction("Index", "Query");
107                    }
108                    else
109                    {
110                        ViewBag.Title = "Access denied - Login";
111                        model = new LoginViewModel();
112                        model.errorMessage = "Access denied, you have no permission to use this application." +
113                            "              Contact a HeuristicLab Hive admin to gain access.";
114                        return View("Index", model);
115                    }
116
[13741]117                }
[13656]118
119            }
120            else
121            {
122                ViewBag.Title = "Login";
[13739]123                var model = new LoginViewModel();
[13656]124                model.errorMessage = "You should fill in both fields";
125                return View("Index", model);
126            }
127        }
[13805]128        /// <summary>
129        /// Redirect user to home sceen for a full logout
130        /// </summary>
131        /// <returns></returns>
[13733]132        public IActionResult Logout()
133        {
[13854]134            return RedirectToAction("Index", "Home");
[13733]135        }
136        #endregion
[13656]137    }
138}
Note: See TracBrowser for help on using the repository browser.