1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.controller.security.managers.impl; |
7 | |
|
8 | |
import java.util.HashMap; |
9 | |
import java.util.Map; |
10 | |
|
11 | |
import pt.digitalis.dif.controller.security.managers.IAuthenticationManager; |
12 | |
import pt.digitalis.dif.controller.security.managers.IIdentityManager; |
13 | |
import pt.digitalis.dif.controller.security.objects.IDIFUser; |
14 | |
import pt.digitalis.dif.exception.security.AuthenticationManagerException; |
15 | |
import pt.digitalis.dif.exception.security.IdentityManagerException; |
16 | |
import pt.digitalis.dif.utils.ObjectFormatter; |
17 | |
|
18 | |
import com.google.inject.Inject; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class AuthenticationManagerStaticImpl implements IAuthenticationManager { |
29 | |
|
30 | |
|
31 | 0 | private Map<String, String> loggedClients = new HashMap<String, String>(); |
32 | |
|
33 | |
|
34 | |
@Inject |
35 | |
private IIdentityManager theIDManager; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public void disconnectClient(String clientIdentifier) |
41 | |
{ |
42 | |
|
43 | 0 | logOut(clientIdentifier); |
44 | 0 | } |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
public IDIFUser getLoggedUser(String clientIdentifier) throws AuthenticationManagerException |
50 | |
{ |
51 | |
try |
52 | |
{ |
53 | 0 | String userID = loggedClients.get(clientIdentifier); |
54 | |
|
55 | 0 | if (userID == null) |
56 | 0 | return null; |
57 | |
else |
58 | 0 | return theIDManager.getUser(userID); |
59 | |
} |
60 | 0 | catch (IdentityManagerException identityManagerException) |
61 | |
{ |
62 | 0 | throw new AuthenticationManagerException("Could not access the identity manager for user authentication!", |
63 | 0 | identityManagerException); |
64 | |
} |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public boolean isClientLogged(String clientIdentifier) |
71 | |
{ |
72 | 0 | return loggedClients.containsKey(clientIdentifier); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public IDIFUser logIn(String clientSpecificID, String userID, String password) |
80 | |
throws AuthenticationManagerException |
81 | |
{ |
82 | |
try |
83 | |
{ |
84 | 0 | if (loggedClients.containsKey(clientSpecificID)) |
85 | |
{ |
86 | |
|
87 | |
|
88 | 0 | String presentUserID = loggedClients.get(clientSpecificID); |
89 | |
|
90 | 0 | if (presentUserID.equals(userID)) |
91 | 0 | return theIDManager.getUser(userID); |
92 | |
} |
93 | |
|
94 | |
|
95 | 0 | if (theIDManager.isIdentityValid(userID, password)) |
96 | |
{ |
97 | |
|
98 | |
|
99 | 0 | loggedClients.put(clientSpecificID, userID); |
100 | |
|
101 | 0 | return theIDManager.getUser(userID); |
102 | |
} |
103 | |
else |
104 | |
|
105 | 0 | return null; |
106 | |
} |
107 | 0 | catch (IdentityManagerException identityManagerException) |
108 | |
{ |
109 | 0 | throw new AuthenticationManagerException("Could not access the identity manager for user authentication!", |
110 | 0 | identityManagerException); |
111 | |
} |
112 | |
} |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
public void logOut(String clientIdentifier) |
118 | |
{ |
119 | 0 | loggedClients.remove(clientIdentifier); |
120 | 0 | } |
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
@Override |
126 | |
public String toString() |
127 | |
{ |
128 | 0 | ObjectFormatter formatter = new ObjectFormatter(); |
129 | 0 | formatter.addItem("Logged Clients", loggedClients); |
130 | |
|
131 | 0 | return formatter.getFormatedObject(); |
132 | |
} |
133 | |
} |