Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 Bugfixing, email setup password and code commenting

File size: 6.0 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        private List<HiveAdminClientWeb> adminclients;
24        /// <summary>
25        /// Service instance that keeps all the information per user seperated. Data is in  different lists so
26        /// only the necessary things can be called.
27        /// </summary>
28        public static WebLoginService Instance
29        {
30            get
31            {
32                if (instance == null)
33                {
34                    instance = new WebLoginService();
35                }
36                return instance;
37            }
38        }
39        /// <summary>
40        /// Initialize the LoginService
41        /// </summary>
42        public WebLoginService()
43        {
44
45            loggedIn = new List<LoginViewModel>();
46            locators = new List<HiveServiceLocatorWeb>();
47            webclients = new List<HiveClientWeb>();
48            fileopeners = new List<FileOpeningService>();
49            accessclients = new List<AccessAdministrationClient>();
50            adminclients = new List<HiveAdminClientWeb>();
51        }
52        /// <summary>
53        /// Creates a new entry for a user. Services will be kept running here untill the user logs out or times out.
54        /// </summary>
55        /// <param name="log">LoginViewModel from new user</param>
56        /// <param name="loc">HiveServiceLocatorWeb created during login</param>
57        public void newLogin(LoginViewModel log, HiveServiceLocatorWeb loc)
58        {
59            destroyPossiblePreviousSessions(log.loginName);
60            var ind = loggedIn.Find(x => x.loginName == log.loginName);
61            if (ind != null)
62            {
63                logout(ind.userId);
64            }
65            loggedIn.Add(log);
66            locators.Add(loc);
67            var temp = new HiveClientWeb(loc, log.userId);
68            webclients.Add(temp);
69            var temp2 = new FileOpeningService(log.userId);
70            fileopeners.Add(temp2);
71            var temp3 = new AccessAdministrationClient(log.userId);
72            accessclients.Add(temp3);
73            var temp4 = new HiveAdminClientWeb(log.userId);
74            adminclients.Add(temp4);
75        }
76        /// <summary>
77        /// Checks if the client has a previous session stored. Destroys all info of that session
78        /// </summary>
79        /// <param name="client">Client login name</param>
80        private void destroyPossiblePreviousSessions(string client)
81        {
82            var ind = loggedIn.FindAll(x => x.loginName == client);
83            foreach (var i in ind)
84            {
85                logout(i.userId);
86            }
87
88        }
89        /// <summary>
90        /// Returns the service locator for a user
91        /// </summary>
92        /// <param name="id">User id</param>
93        /// <returns></returns>
94        public HiveServiceLocatorWeb getServiceLocator(Guid id)
95        {
96            return locators.Find(x => x.UserId == id);
97        }
98        /// <summary>
99        /// Returns HiveClientWeb for a user
100        /// </summary>
101        /// <param name="id">User id</param>
102        /// <returns></returns>
103        public HiveClientWeb getClientWeb(Guid id)
104        {
105            return webclients.Find(x => x.UserId == id);
106        }
107        /// <summary>
108        /// Returns the LoginViewModel for a user
109        /// </summary>
110        /// <param name="id">User id</param>
111        /// <returns></returns>
112        public LoginViewModel getLoginViewModel(Guid id)
113        {
114            return loggedIn.Find(x => x.userId == id);
115        }
116        /// <summary>
117        /// Returns the FileOpenerService for the user
118        /// </summary>
119        /// <param name="id">User id</param>
120        /// <returns></returns>
121        public FileOpeningService getFileOpener(Guid id)
122        {
123            return fileopeners.Find(x => x.UserId == id);
124        }
125
126        /// <summary>
127        /// Returns the AccessAdministrationClient used for User/Group/Role management
128        /// </summary>
129        /// <param name="id"></param>
130        /// <returns></returns>
131        public AccessAdministrationClient getAccessAdminClient(Guid id)
132        {
133            return accessclients.Find(x => x.userId == id);
134        }
135        /// <summary>
136        /// Returns HiveAdminClient used for Resource management
137        /// </summary>
138        /// <param name="id"></param>
139        /// <returns></returns>
140        public HiveAdminClientWeb getAdminClient(Guid id)
141        {
142            return adminclients.Find(x => x.userId == id);
143        }
144        /// <summary>
145        /// Removes all traces from the logged in user.
146        /// </summary>
147        /// <param name="id">User id</param>
148        public void logout(Guid id)
149        {
150           
151            webclients.RemoveAll(x => x.UserId == id);
152            locators.RemoveAll(x => x.UserId == id);
153            fileopeners.RemoveAll(x => x.UserId == id);
154            accessclients.RemoveAll(x => x.userId == id);
155            adminclients.RemoveAll(x => x.userId == id);
156            loggedIn.RemoveAll(x => x.userId == id);
157
158        }
159    }
160}
Note: See TracBrowser for help on using the repository browser.