Package io.levysworks.beans
Class DatabaseManager
java.lang.Object
io.levysworks.beans.DatabaseManager
Database manager bean, used for connecting, and interacting with the database.
This class initializes a connection after all dependencies are injected
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddActiveKey(String user_uuid, String server, String public_key) Inserts a new row into thessh_keystable with the provided parametersvoidInserts a new row into theaudit_logtable with the provided parametersvoidaddPendingRequest(String user_uuid, String server, String public_key) Inserts a new row into therequeststable with the provided parametersvoidInserts a new user into theuserstable with the specified parametersbooleancheckKeyExists(String uid) Checks whether an SSH Key exists in thessh_keystable with the specified UIDintQueries the number of ssh keys present in thessh_keystable.Queries a set of user and SSH key information by joining thessh_keysanduserstables.getLogs(int limit) Queries theaudit_logtable for the latest logs ordered descending by thetimestampcolumn, limited to the specified amount.getPendingRequestById(int requestId) Queries a request by its id from therequeststableintQueries the number of requests present in therequeststable.Queries a set of user and request information by joining therequestsanduserstables.getUserByUUID(String uuid) Queries theuserstable for the user with the specified UUID.intQueries the number of users present in theuserstable.getUserKeysByUUID(String uuid) Queries thessh_keystable for all SSH keys associated with the specified user UUID.Retrieves user data along with SSH key metadata by joiningusersandssh_keys.voidremoveKeyByUID(String server, String uid) Removes the key entry from thessh_keystable whereserveranduidboth matchvoidremovePendingRequest(int id) Removes a request from therequeststable by its IDvoidremoveUser(String uuid) Removes a user from theuserstable by their UUIDvoidupdateUser(String uuid, String first_name, String last_name, String email, String department, String notes) Updates the user with matchinguuidin theuserstable
-
Constructor Details
-
DatabaseManager
public DatabaseManager()
-
-
Method Details
-
getUserCount
Queries the number of users present in theuserstable.- Returns:
- the number of users in the database
- Throws:
SQLException- if theuserstable doesn't exist or the connection is closed
-
getKeyCount
Queries the number of ssh keys present in thessh_keystable.- Returns:
- the number of ssh keys in the database
- Throws:
SQLException- if thessh_keystable doesn't exist or the connection is closed
-
getPendingRequestCount
Queries the number of requests present in therequeststable.- Returns:
- the number of requests in the database
- Throws:
SQLException- if therequeststable doesn't exist or the connection is closed
-
getPendingRequestById
Queries a request by its id from therequeststable- Parameters:
requestId- The id of the request in therequeststable- Returns:
- a
Requestcontaining the queried data - Throws:
SQLException- if therequeststable doesn't exist or the connection is closed
-
getRequestsForTemplate
Queries a set of user and request information by joining therequestsanduserstables.The result contains combined user-request data including
name,email,request ID,server,key type, andtimestamp.- Returns:
- a
ListofCompositeUserDataobjects representing the joined data. - Throws:
SQLException- if a database access error occurs or the query fails.
-
addPendingRequest
public void addPendingRequest(String user_uuid, String server, String public_key) throws SQLException Inserts a new row into therequeststable with the provided parameters- Parameters:
user_uuid- The UUID of the user requestserver- The server the user requested a key topublic_key- The public key generated by the server- Throws:
SQLException- if a database access error occurs or the query fails.
-
addActiveKey
public void addActiveKey(String user_uuid, String server, String public_key) throws SQLException, NoSuchAlgorithmException Inserts a new row into thessh_keystable with the provided parametersComputes a fingerprint for the public key for the sql statement
- Parameters:
user_uuid- UUID of the owner of the keyserver- The server, the key will be onpublic_key- The public key to add- Throws:
SQLException- if a database access error occurs or the query fails.NoSuchAlgorithmException- if theKeyGenerator.KeyHasher.generateFingerprint(java.lang.String)fails to compute the fingerprint of the public key
-
getKeysForTemplate
Queries a set of user and SSH key information by joining thessh_keysanduserstables.The result contains combined user–SSH key data including
name,email,fingerprint,key type,issued date,valid until,UID, andserver.- Returns:
- a
ListofCompositeUserDataobjects representing the joined user and key data. - Throws:
SQLException- if a database access error occurs or the query fails.
-
getUsersForTemplate
Retrieves user data along with SSH key metadata by joiningusersandssh_keys.Includes name, email, UUID, department, key count, and associated servers.
- Returns:
- a
ListofCompositeUserDatawith user and key summary. - Throws:
SQLException- if a database access error occurs.
-
getUserByUUID
Queries theuserstable for the user with the specified UUID.- Parameters:
uuid- The UUID of the queried user- Returns:
- a
CompositeUserDataobject containing thefirst name,last name,email,departmentand adminnotesabout the user. - Throws:
SQLException- if a database access error occurs.
-
getUserKeysByUUID
Queries thessh_keystable for all SSH keys associated with the specified user UUID.- Parameters:
uuid- The UUID of the user- Returns:
- a
ListofCompositeUserDataobjects, each containing a separate SSH key's data - Throws:
SQLException- if a database access error occurs.
-
removeKeyByUID
Removes the key entry from thessh_keystable whereserveranduidboth match- Parameters:
server- The name of the serveruid- The UID of the SSH Key- Throws:
SQLException- if a database access error occurs.
-
updateUser
public void updateUser(String uuid, String first_name, String last_name, String email, String department, String notes) throws SQLException Updates the user with matchinguuidin theuserstable- Parameters:
uuid- The updatable user's UUIDfirst_name- The firs name to update tolast_name- The last name to update toemail- The email to update todepartment- The department to update tonotes- The notes to update to- Throws:
SQLException- if a database access error occurs.
-
addNewLog
Inserts a new row into theaudit_logtable with the provided parameters- Parameters:
title- Title of the logmessage- Message of the logtimestamp- Timestamp of the log- Throws:
SQLException- if a database access error occurs.
-
getLogs
Queries theaudit_logtable for the latest logs ordered descending by thetimestampcolumn, limited to the specified amount.- Parameters:
limit- The amount of logs to return- Returns:
- a
ListofCompositeUserDataobjects representing the log entries - Throws:
SQLException- if a database access error occurs
-
checkKeyExists
Checks whether an SSH Key exists in thessh_keystable with the specified UID- Parameters:
uid- The UID to check- Returns:
- a
booleanrepresenting whether the key exists or not - Throws:
SQLException- if a database access error occurs
-
removePendingRequest
Removes a request from therequeststable by its ID- Parameters:
id- The ID of the removable request- Throws:
SQLException- if a database access error occurs
-
addUser
public void addUser(String first_name, String last_name, String email, String department, String notes) throws SQLException Inserts a new user into theuserstable with the specified parameters- Parameters:
first_name- The first name of the userlast_name- The last name of the useremail- The email of the userdepartment- The department the user is innotes- The admin notes about this user- Throws:
SQLException- if a database access error occurs
-
removeUser
Removes a user from theuserstable by their UUID- Parameters:
uuid- The UUID of the user- Throws:
SQLException- if a database access error occurs
-