Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2582 Security access implemented, directory renaming and choosing, adding more partials

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