1 | using HeuristicLab.Clients.Access.Administration;
|
---|
2 | using HeuristicLab.Clients.Hive.WebJobManager.ViewModels;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 |
|
---|
8 | namespace 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 | public AccessAdministrationClient getAccessAdminClient(Guid id)
|
---|
127 | {
|
---|
128 | return accessclients.Find(x => x.userId == id);
|
---|
129 | }
|
---|
130 | public HiveAdminClientWeb getAdminClient(Guid id)
|
---|
131 | {
|
---|
132 | return adminclients.Find(x => x.userId == id);
|
---|
133 | }
|
---|
134 | /// <summary>
|
---|
135 | /// Removes all traces from the logged in user.
|
---|
136 | /// </summary>
|
---|
137 | /// <param name="id">User id</param>
|
---|
138 | public void logout(Guid id)
|
---|
139 | {
|
---|
140 |
|
---|
141 | webclients.RemoveAll(x => x.UserId == id);
|
---|
142 | locators.RemoveAll(x => x.UserId == id);
|
---|
143 | fileopeners.RemoveAll(x => x.UserId == id);
|
---|
144 | accessclients.RemoveAll(x => x.userId == id);
|
---|
145 | adminclients.RemoveAll(x => x.userId == id);
|
---|
146 | loggedIn.RemoveAll(x => x.userId == id);
|
---|
147 |
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|