Class UserRepository

java.lang.Object
com.inesm.KeyManager.repositories.UserRepository

@Repository public class UserRepository extends Object
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 Details

    • UserRepository

      public UserRepository()
  • Method Details

    • setDataSource

      @Autowired public void setDataSource(DataSource dataSource)
      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

      public User getUserByID(long id) throws ObjectNotFoundException
      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

      public User getUserByUsermame(String username) throws ObjectNotFoundException
      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

      public List<User> 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 username
      org.springframework.dao.DataIntegrityViolationException - if violation of an integrity constraint occurs
    • createUser

      public long createUser(User user) throws org.springframework.dao.DataIntegrityViolationException
      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

      public void deleteUser(String username) throws ObjectNotFoundException
      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 userID
      org.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 userID
      org.springframework.dao.DataIntegrityViolationException - if violation of an integrity constraint occurs
    • getRoleOperations

      public List<Operation> getRoleOperations(long id)
      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

      public List<Operation> getOperations()
      Gets all operations from the database.
      Returns:
      a list containing all the operations inside the station, not null, maybe empty
    • getOperationByID

      public Operation getOperationByID(int id) throws ObjectNotFoundException
      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

      public List<Role> getRoles()
      Gets all roles from the database.
      Returns:
      a list containing all the roles present in the database, not null, maybe empty
    • getRoleByID

      public Role getRoleByID(long id) throws ObjectNotFoundException
      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

      public Role getRoleByName(String name) throws ObjectNotFoundException
      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

      public long createRole(Role role) throws org.springframework.dao.DataIntegrityViolationException
      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 database
      org.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 roleID
      org.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 role
      operationID - 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 role
      operationID - 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 fails
      org.springframework.dao.DataIntegrityViolationException - if violation of an integrity constraint occurs
    • getRequests

      public List<Request> getRequests()
      Gets all the requests from the database.
      Returns:
      a list containing all the requests inside the database, not null, maybe empty
    • getRequestByID

      public Request getRequestByID(long id) throws ObjectNotFoundException
      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 requestID
      org.springframework.dao.DataIntegrityViolationException - if violation of an integrity constraint occurs
    • deleteRequest

      public void deleteRequest(long requestID) throws ObjectNotFoundException
      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