-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthentication.java
More file actions
144 lines (132 loc) · 4.02 KB
/
Authentication.java
File metadata and controls
144 lines (132 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package yuconz23d;
import java.sql.SQLException;
/**
* This class is responsible for the Authentication
* of user Logins to the system. This includes checking
* logins, creating, retrieving and removing sessions
* as appropriate.
* @author John
*
*/
public class Authentication {
private String user;
private String staffNo;
private Role selectedRole;
private DatabaseHandler db;
/**
* Initialises the class, user+staffNo set to null until a login occurs and
* db stores a reference to the database handler
*/
public Authentication() {
user = null;
staffNo = null;
selectedRole = null;
db = new DatabaseHandler();
}
/**
* A basic getter method to retrieve the current userID
* @return String the current user's login name, null if no one is logged in
*/
public String getUser() {
return user;
}
/**
* A basic getter method to retrieve the current user's staffNo
* @return Current user's staffNo, null if no one is logged in
*/
public String getStaffNo() {
return staffNo;
}
/**
* Return the role that was selected upon login authentication
* @return Enumerated Roles value for the given authentication level, null if not logged in
*/
public Role getAccessRole() {
return this.selectedRole;
}
/**
* Checks whether there is a user currently authenticated
* @return true when a user is authenticated, false otherwise
*/
public boolean isAuthenticated() {
return user != null;
}
/**
* Retrieves all available roles for a given userID
* @param userID which user's available roles will be found and returned
* @return String array of all available roles, null if the userID is not found or an SQL error occurred
*/
public String[] getAvailableRoles(String userID) {
if(userID.isEmpty()) { return null; }
Integer highestRole = null;
try {
highestRole = db.getHighestRoleByLogin(userID);
} catch(SQLException e) {
System.err.println("SQL connection error occurred: " + e.getMessage());
} catch (IllegalArgumentException e1) {
return null;
}
if(highestRole == null) {
return null;
}
else if(highestRole == 0) {
String[] resultArray = {"Suspended"};
return resultArray;
}
else if(highestRole == 1) {
String[] resultArray = {"Employee"};
return resultArray;
}
else {
String highestRoleName = Role.nameOf(highestRole);
String[] resultArray = {highestRoleName, "Employee"};
return resultArray;
}
}
/**
* Checks the given login details and verifies if they are correct,
* when correct the user field of this class is updated to contain
* this new userID value
* @param userID The userID to attempt to login as
* @param password The password that the user submitted for this attempt
* @param authLevel the authentication level that was selected
* @return true when the login was a success, false when it failed
*/
public boolean checkLogin(String userID, String password, int authLevel) {
if(password.isEmpty()) { return false; }
String correctPassword = null;
try {
correctPassword = db.getPassword(userID);
} catch (SQLException e) {
System.err.println("SQL connection error occurred: " + e.getMessage());
e.printStackTrace();
}
if(correctPassword != null) {
if(password.equals(correctPassword)) {
try {
db.addSession(userID, authLevel);
this.user = userID;
this.selectedRole = Role.valueOf(authLevel);
this.staffNo = db.getEmployeeIdByLogin(userID);
return true;
} catch (SQLException e) {
System.err.println("SQL connection error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}
return false;
}
/**
* Logs the current user out of the system by destroying their session
* and forgetting their session information
*/
public void logout() {
try {
db.destroySession(user);
} catch (SQLException e) {
System.err.println("SQL connection error occurred: " + e.getMessage());
}
user = null;
}
}