Class UserRepository
java.lang.Object
com.inesm.KeyManager.repositories.UserRepository
This class provides all the interactions with the users management database.
It provides functions to interact with the database's users, roles, permissions, operations and requests tables.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcreatePermission(long roleID, int operationID) Creates a new permission in the database.longcreateRole(Role role) Adds a new role to the database.longcreateUser(User user) Adds a new user to the database.voiddeleteRequest(long requestID) Deletes a request from the database.voiddeleteRole(long roleID) Deletes a role from the database.voiddeleteUser(String username) Deletes a user from the database.getOperationByID(int id) Gets an operation from the database.Gets all operations from the database.getRequestByID(long id) Gets a requests from the database by requestID.Gets all the requests from the database.getRoleByID(long id) Gets a role from the database by roleID.getRoleByName(String name) Gets a role from the database by name.getRoleOperations(long id) Gets all operations assigned to a given role.getRoles()Gets all roles from the database.getUserByID(long id) Gets a user from the database by userID.getUserByUsermame(String username) Gets a user from the database by username.getUsers()Gets all users from the database.longinsertRequest(Request request) Inserts a request in the database.voidremoveAllPermissions(long roleID) Removes all permissions associated with a role.voidremovePermission(long roleID, int operationID) Removes a permission from the database.voidsetDataSource(DataSource dataSource) Binds the jdbcTemplate to the specified data source.voidupdateRequestStatus(Request request) Updates the status of a request in the database.voidupdateRole(Role role) Updates a role in the database.voidupdateUser(User user) Updates a user in the database.voidupdateUserLoginStatus(User user) Updates the user login status.voidupdateUserPassword(User user) Updates a user password in the database.
-
Constructor Details
-
UserRepository
public UserRepository()
-
-
Method Details
-
setDataSource
Binds the jdbcTemplate to the specified data source.- Parameters:
dataSource- connection object for the database. Its properties are defined in the application.properties file
-
getUserByID
Gets a user from the database by userID.- Parameters:
id- the userID of the user- Returns:
- the user object retrieved from the database
- Throws:
ObjectNotFoundException- if no user was found with the given userID
-
getUserByUsermame
Gets a user from the database by username.- Parameters:
username- the username of the user- Returns:
- the user retrieved from the database
- Throws:
ObjectNotFoundException- if no user was found with the given username
-
getUsers
Gets all users from the database.- Returns:
- the list containing all the users, not null, maybe empty
-
updateUser
public void updateUser(User user) throws ObjectNotFoundException, org.springframework.dao.DataIntegrityViolationException Updates a user in the database. Will update the following fields:- username
- citizenCard
- firstName
- lastName
- active
- roleID
- tempPassword
- Parameters:
user- the user object to be updated, not null, must have username set- Throws:
ObjectNotFoundException- if no user was found with the set usernameorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
createUser
Adds a new user to the database. Will insert the following fields:- username
- citizenCard
- pwHash
- firstName
- lastName
- isActive
- roleID
- tempPassword
- Parameters:
user- the user object to be added to the database, not null- Returns:
- the userID of the created user
- Throws:
org.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
deleteUser
Deletes a user from the database.- Parameters:
username- the username of the user to be deleted- Throws:
ObjectNotFoundException- if no user was found with the given username
-
updateUserLoginStatus
public void updateUserLoginStatus(User user) throws ObjectNotFoundException, org.springframework.dao.DataIntegrityViolationException Updates the user login status. Will update the following fields:- failedLogins
- lastLoginAttempt
- Parameters:
user- not null, must have userID set- Throws:
ObjectNotFoundException- if no user was found with the set userIDorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
updateUserPassword
public void updateUserPassword(User user) throws ObjectNotFoundException, org.springframework.dao.DataIntegrityViolationException Updates a user password in the database. Will update the following fields:- pwHash
- tempPassword
- userID
- Parameters:
user- the user object to be updated, not null, must have userID set- Throws:
ObjectNotFoundException- if no user was found with the set userIDorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
getRoleOperations
Gets all operations assigned to a given role.- Parameters:
id- the roleID of the role- Returns:
- a list containing all the operations assigned to received role, not null, maybe empty
-
getOperations
Gets all operations from the database.- Returns:
- a list containing all the operations inside the station, not null, maybe empty
-
getOperationByID
Gets an operation from the database.- Parameters:
id- the operationID of the operation- Returns:
- the operation object retrieved from the database
- Throws:
ObjectNotFoundException- if no operation was found with the given operationID
-
getRoles
Gets all roles from the database.- Returns:
- a list containing all the roles present in the database, not null, maybe empty
-
getRoleByID
Gets a role from the database by roleID.- Parameters:
id- the roleID of the role- Returns:
- the role object retrieved from the database
- Throws:
ObjectNotFoundException- if no role was found with the given roleID
-
getRoleByName
Gets a role from the database by name.- Parameters:
name- the name of the role- Returns:
- the role object retrieved
- Throws:
ObjectNotFoundException- if no role with the given name was found in the database
-
createRole
Adds a new role to the database. Will update the following fields:- name
- description
- color
- Parameters:
role- the role to be added, not null- Returns:
- the roleID of the created role
- Throws:
org.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
updateRole
public void updateRole(Role role) throws ObjectNotFoundException, org.springframework.dao.DataIntegrityViolationException Updates a role in the database.- Parameters:
role- the role object to be updated, not null, must have roleID set- Throws:
ObjectNotFoundException- if the role with the set roleID was not found in the databaseorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
deleteRole
public void deleteRole(long roleID) throws ObjectNotFoundException, org.springframework.dao.DataIntegrityViolationException Deletes a role from the database.- Parameters:
roleID- the roleID of the role to be deleted- Throws:
ObjectNotFoundException- if no role was found with the given roleIDorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
createPermission
public void createPermission(long roleID, int operationID) throws org.springframework.dao.DataIntegrityViolationException Creates a new permission in the database.- Parameters:
roleID- the roleID of the roleoperationID- the operationID of the operation- Throws:
org.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
removePermission
public void removePermission(long roleID, int operationID) Removes a permission from the database.- Parameters:
roleID- the roleID of the roleoperationID- the operationID of the operation
-
removeAllPermissions
public void removeAllPermissions(long roleID) Removes all permissions associated with a role.- Parameters:
roleID- the roleID of the role
-
insertRequest
public long insertRequest(Request request) throws IOException, org.springframework.dao.DataIntegrityViolationException Inserts a request in the database. Will insert the following fields:- operationID
- userFullname
- userCitizenCard
- verifierFullname
- time
- status
- reason
- scope
- data
- Parameters:
request- the request to be inserted in the database- Returns:
- the requestID of the created request
- Throws:
IOException- if serialization of the data field of the request failsorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
getRequests
Gets all the requests from the database.- Returns:
- a list containing all the requests inside the database, not null, maybe empty
-
getRequestByID
Gets a requests from the database by requestID.- Parameters:
id- the requestID of the request- Returns:
- the retrieved request object
- Throws:
ObjectNotFoundException- if no request was found with the given requestID
-
updateRequestStatus
public void updateRequestStatus(Request request) throws ObjectNotFoundException, org.springframework.dao.DataIntegrityViolationException Updates the status of a request in the database. Will update the following fields:- status
- verifierFullname
- verifierCitizenCard
- reason
- Parameters:
request- the request object to be added, must have requestID set- Throws:
ObjectNotFoundException- if no request was found with the set requestIDorg.springframework.dao.DataIntegrityViolationException- if violation of an integrity constraint occurs
-
deleteRequest
Deletes a request from the database.- Parameters:
requestID- the requestID of the request to be deleted- Throws:
ObjectNotFoundException- if no request with the given requestID was found in the database
-