Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Clients.Hive.WebJobManager/Services/WebLoginService.cs @ 13740

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

#2582 Job Manager done. Start user management

File size: 5.2 KB
Line 
1using HeuristicLab.Clients.Access.Administration;
2using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading.Tasks;
7
8namespace HeuristicLab.Clients.Hive.WebJobManager.Services
9{
10    /// <summary>
11    /// A service that will handle all the logins and will serve as a singleton that controls
12    /// all the necessary Services used by Hive. (HiveServiceLocatorWeb and HiveClientWeb instances per user).
13    /// Users are identified with a seperate Guid made by the web version to ensure login security.
14    /// </summary>
15    public class WebLoginService
16    {
17        private static WebLoginService instance;
18        private List<LoginViewModel> loggedIn;
19        private List<HiveServiceLocatorWeb> locators;
20        private List<HiveClientWeb> webclients;
21        private List<FileOpeningService> fileopeners;
22        private List<AccessAdministrationClient> accessclients;
23        /// <summary>
24        /// Service instance that keeps all the information per user seperated. Data is in  different lists so
25        /// only the necessary things can be called.
26        /// </summary>
27        public static WebLoginService Instance
28        {
29            get
30            {
31                if (instance == null)
32                {
33                    instance = new WebLoginService();
34                }
35                return instance;
36            }
37        }
38        /// <summary>
39        /// Initialize the LoginService
40        /// </summary>
41        public WebLoginService()
42        {
43
44            loggedIn = new List<LoginViewModel>();
45            locators = new List<HiveServiceLocatorWeb>();
46            webclients = new List<HiveClientWeb>();
47            fileopeners = new List<FileOpeningService>();
48            accessclients = new List<AccessAdministrationClient>();
49        }
50        /// <summary>
51        /// Creates a new entry for a user. Services will be kept running here untill the user logs out or times out.
52        /// </summary>
53        /// <param name="log">LoginViewModel from new user</param>
54        /// <param name="loc">HiveServiceLocatorWeb created during login</param>
55        public void newLogin(LoginViewModel log, HiveServiceLocatorWeb loc)
56        {
57            destroyPossiblePreviousSessions(log.loginName);
58            var ind = loggedIn.Find(x => x.loginName == log.loginName);
59            if (ind != null)
60            {
61                logout(ind.userId);
62            }
63            loggedIn.Add(log);
64            locators.Add(loc);
65            var temp = new HiveClientWeb(loc, log.userId);
66            webclients.Add(temp);
67            var temp2 = new FileOpeningService(log.userId);
68            fileopeners.Add(temp2);
69            var temp3 = new AccessAdministrationClient(log.userId);
70            accessclients.Add(temp3);
71        }
72        /// <summary>
73        /// Checks if the client has a previous session stored. Destroys all info of that session
74        /// </summary>
75        /// <param name="client">Client login name</param>
76        private void destroyPossiblePreviousSessions(string client)
77        {
78            var ind = loggedIn.FindAll(x => x.loginName == client);
79            foreach (var i in ind)
80            {
81                logout(i.userId);
82            }
83
84        }
85        /// <summary>
86        /// Returns the service locator for a user
87        /// </summary>
88        /// <param name="id">User id</param>
89        /// <returns></returns>
90        public HiveServiceLocatorWeb getServiceLocator(Guid id)
91        {
92            return locators.Find(x => x.UserId == id);
93        }
94        /// <summary>
95        /// Returns HiveClientWeb for a user
96        /// </summary>
97        /// <param name="id">User id</param>
98        /// <returns></returns>
99        public HiveClientWeb getClientWeb(Guid id)
100        {
101            return webclients.Find(x => x.UserId == id);
102        }
103        /// <summary>
104        /// Returns the LoginViewModel for a user
105        /// </summary>
106        /// <param name="id">User id</param>
107        /// <returns></returns>
108        public LoginViewModel getLoginViewModel(Guid id)
109        {
110            return loggedIn.Find(x => x.userId == id);
111        }
112        /// <summary>
113        /// Returns the FileOpenerService for the user
114        /// </summary>
115        /// <param name="id">User id</param>
116        /// <returns></returns>
117        public FileOpeningService getFileOpener(Guid id)
118        {
119            return fileopeners.Find(x => x.UserId == id);
120        }
121
122        public AccessAdministrationClient getAccessAdminClient(Guid id)
123        {
124            return accessclients.Find(x => x.userId == id);
125        }
126        /// <summary>
127        /// Removes all traces from the logged in user.
128        /// </summary>
129        /// <param name="id">User id</param>
130        public void logout(Guid id)
131        {
132           
133            webclients.RemoveAll(x => x.UserId == id);
134            locators.RemoveAll(x => x.UserId == id);
135            fileopeners.RemoveAll(x => x.UserId == id);
136            accessclients.RemoveAll(x => x.userId == id);
137            loggedIn.RemoveAll(x => x.userId == id);
138        }
139    }
140}
Note: See TracBrowser for help on using the repository browser.