Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17709 was 13862, checked in by jlodewyc, 9 years ago

#2582 Start angular OKB manager, data loaded

File size: 5.5 KB
Line 
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
22
23using Microsoft.AspNetCore.Mvc;
24using System.ServiceModel.Security;
25using System;
26using Microsoft.AspNetCore.Http;
27using HeuristicLab.Clients.Hive.WebJobManager.Services;
28using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
29using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
30
31namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
32{
33    /// <summary>
34    /// Controller for initial landing page
35    /// </summary>
36    public class HomeController : Controller
37    {
38        private WebLoginService weblog;
39        private HiveServiceClient client;
40        public HomeController()
41        {
42            this.weblog = WebLoginService.Instance;
43        }
44        #region Login
45        /// <summary>
46        /// Opens initial home page
47        /// </summary>
48        /// <returns>View from home page</returns>
49        public IActionResult Index()
50        {
51            ViewBag.Title = "Login";
52            var user = HttpContext.Session.GetString("UserId");
53            if (user != null && user != "")
54            {
55                Guid t = Guid.Parse(user);
56                weblog.logout(t);
57                HttpContext.Session.Clear();
58            }
59            return View(new LoginViewModel());
60
61        }
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
69        {
70            if (!string.IsNullOrEmpty(loginName) && !string.IsNullOrEmpty(password))
71            {
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();
78                hiveServiceLocator.Username = loginName;
79                hiveServiceLocator.Password = password;//Not encrypted for login to service
80                hiveServiceLocator.UserId = model.userId;
81
82                client = hiveServiceLocator.getHiveServiceClient();
83                try
84                {
85                    var test = client.GetJobs();//Throws messageSecurityException if login failss
86                    ViewBag.Title = "Login succesful";
87                    weblog.newLogin(model, hiveServiceLocator);
88                    HttpContext.Session.SetString("UserId", model.userId.ToString());
89                    return RedirectToAction("Index", "Job");
90                }
91                catch (MessageSecurityException e)
92                {
93                    ViewBag.Title = "Login";
94                    model = new LoginViewModel();
95                    model.errorMessage = "Wrong login, try again";
96                    return View("Index", model);
97                }
98                catch (SecurityAccessDeniedException e)
99                {
100                    var q = new QueryWebClient(model, password);
101                    if (q.CheckLogin())
102                    {
103                        ViewBag.Title = "Login succesful";
104                        weblog.newLoginOKBOnly(model,q, password);
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
117                }
118
119            }
120            else
121            {
122                ViewBag.Title = "Login";
123                var model = new LoginViewModel();
124                model.errorMessage = "You should fill in both fields";
125                return View("Index", model);
126            }
127        }
128        /// <summary>
129        /// Redirect user to home sceen for a full logout
130        /// </summary>
131        /// <returns></returns>
132        public IActionResult Logout()
133        {
134            return RedirectToAction("Index", "Home");
135        }
136        #endregion
137    }
138}
Note: See TracBrowser for help on using the repository browser.