ORE Studio 0.0.4
Loading...
Searching...
No Matches
Public Types | Public Member Functions | List of all members
account_service Class Reference

Service for managing user accounts including creation, listing, and deletion. More...

#include <account_service.hpp>

Collaboration diagram for account_service:
Collaboration graph

Public Types

using context = ores::database::context
 

Public Member Functions

 account_service (database::context ctx)
 Constructs an account_service with required repositories and security components.
 
domain::account create_account (const std::string &username, const std::string &email, const std::string &password, const std::string &recorded_by, const std::string &change_commentary="Account created")
 Creates a new account with the provided details.
 
std::optional< domain::accountget_account (const boost::uuids::uuid &account_id)
 Gets a single account by its ID.
 
std::vector< domain::accountlist_accounts ()
 Lists all accounts in the system.
 
std::vector< domain::accountlist_accounts (std::uint32_t offset, std::uint32_t limit)
 Lists accounts with pagination support.
 
std::uint32_t get_total_account_count ()
 Gets the total count of active accounts.
 
std::vector< domain::login_infolist_login_info ()
 Lists all login info records in the system.
 
void delete_account (const boost::uuids::uuid &account_id)
 Deletes an account by its ID.
 
domain::account login (const std::string &username, const std::string &password, const boost::asio::ip::address &ip_address)
 Authenticates a user and updates login tracking information.
 
bool lock_account (const boost::uuids::uuid &account_id)
 Locks an account, preventing login.
 
bool unlock_account (const boost::uuids::uuid &account_id)
 Unlocks an account that has been locked due to failed login attempts or manual locking.
 
void logout (const boost::uuids::uuid &account_id)
 Logs out a user by setting their online status to false.
 
bool update_account (const boost::uuids::uuid &account_id, const std::string &email, const std::string &recorded_by, const std::string &change_reason_code, const std::string &change_commentary)
 Updates an existing account's email address.
 
std::vector< domain::accountget_account_history (const std::string &username)
 Retrieves all historical versions of an account by username.
 
bool set_password_reset_required (const boost::uuids::uuid &account_id)
 Sets the password_reset_required flag on an account.
 
std::string change_password (const boost::uuids::uuid &account_id, const std::string &new_password)
 Changes the password for an account.
 
domain::login_info get_login_info (const boost::uuids::uuid &account_id)
 Retrieves the login_info for a specific account.
 
std::string update_my_email (const boost::uuids::uuid &account_id, const std::string &new_email)
 Updates the email address for a user's own account.
 

Detailed Description

Service for managing user accounts including creation, listing, and deletion.

Constructor & Destructor Documentation

◆ account_service()

Constructs an account_service with required repositories and security components.

Parameters
account_repoThe repository for managing account data.
login_info_repoThe repository for managing login tracking data.

Member Function Documentation

◆ create_account()

domain::account create_account ( const std::string &  username,
const std::string &  email,
const std::string &  password,
const std::string &  recorded_by,
const std::string &  change_commentary = "Account created" 
)

Creates a new account with the provided details.

This method receives non-computed fields of the account entity such as email, password, username, etc. It uses the password manager to compute the salt and hash, uses the account repository to create the account, and adds a new login tracking entry.

Note: Administrative privileges are now managed through RBAC roles. Use authorization_service::assign_role() to grant roles after creation.

Parameters
usernameThe unique username for the account
emailThe email address for the account
passwordThe plaintext password (will be hashed)
recorded_byThe username of the person creating the account
change_commentaryOptional commentary explaining account creation
Returns
The created account with computed fields
Here is the caller graph for this function:

◆ get_account()

std::optional< domain::account > get_account ( const boost::uuids::uuid &  account_id)

Gets a single account by its ID.

Parameters
account_idThe ID of the account to retrieve
Returns
The account if found, std::nullopt otherwise

◆ list_accounts() [1/2]

std::vector< domain::account > list_accounts ( )

Lists all accounts in the system.

Returns
Vector of all accounts

◆ list_accounts() [2/2]

std::vector< domain::account > list_accounts ( std::uint32_t  offset,
std::uint32_t  limit 
)

Lists accounts with pagination support.

Parameters
offsetNumber of records to skip
limitMaximum number of records to return
Returns
Vector of accounts for the requested page

◆ get_total_account_count()

std::uint32_t get_total_account_count ( )

Gets the total count of active accounts.

Returns
Total number of active accounts

◆ list_login_info()

std::vector< domain::login_info > list_login_info ( )

Lists all login info records in the system.

Returns
Vector of all login info records

◆ delete_account()

void delete_account ( const boost::uuids::uuid &  account_id)

Deletes an account by its ID.

Parameters
account_idThe ID of the account to delete

◆ login()

domain::account login ( const std::string &  username,
const std::string &  password,
const boost::asio::ip::address &  ip_address 
)

Authenticates a user and updates login tracking information.

This method validates the provided credentials against stored account data, and if successful, updates the login_info table with the current login information. It also handles failed login attempts by incrementing the failed_login_info counter and may lock the account after too many consecutive failures.

Parameters
usernameThe username for authentication
passwordThe plaintext password to verify
ip_addressThe IP address of the login attempt
Returns
The authenticated account if credentials are valid
Exceptions
std::invalid_argumentIf username or password is empty
std::runtime_errorIf account is locked or credentials are invalid

◆ lock_account()

bool lock_account ( const boost::uuids::uuid &  account_id)

Locks an account, preventing login.

This method sets the account's locked status to true, preventing the user from logging in until the account is unlocked.

Parameters
account_idThe ID of the account to lock
Returns
true if the account was locked successfully, false otherwise
Here is the caller graph for this function:

◆ unlock_account()

bool unlock_account ( const boost::uuids::uuid &  account_id)

Unlocks an account that has been locked due to failed login attempts or manual locking.

This method resets the account's locked status and clears the failed login counter, allowing the user to attempt to login again.

Parameters
account_idThe ID of the account to unlock
Returns
true if the account was unlocked successfully, false otherwise

◆ logout()

void logout ( const boost::uuids::uuid &  account_id)

Logs out a user by setting their online status to false.

This method updates the login_info table to mark the user as offline.

Parameters
account_idThe ID of the account to log out
Exceptions
std::invalid_argumentIf account does not exist

◆ update_account()

bool update_account ( const boost::uuids::uuid &  account_id,
const std::string &  email,
const std::string &  recorded_by,
const std::string &  change_reason_code,
const std::string &  change_commentary 
)

Updates an existing account's email address.

Username cannot be changed. This creates a new version of the account in the temporal history.

Note: Role assignments are managed via authorization_service::assign_role() and revoke_role().

Parameters
account_idThe ID of the account to update
emailThe new email address
recorded_byThe username making the change
change_reason_codeThe change reason code for audit trail
change_commentaryFree-text commentary explaining the change
Returns
true if the account was updated successfully, false otherwise
Exceptions
std::invalid_argumentIf account does not exist

◆ get_account_history()

std::vector< domain::account > get_account_history ( const std::string &  username)

Retrieves all historical versions of an account by username.

Returns all versions of the account from the temporal history, ordered from newest to oldest.

Parameters
usernameThe username of the account
Returns
Vector of all historical versions of the account

◆ set_password_reset_required()

bool set_password_reset_required ( const boost::uuids::uuid &  account_id)

Sets the password_reset_required flag on an account.

When this flag is set, the user will be forced to change their password on their next login.

Parameters
account_idThe ID of the account to flag for password reset
Returns
true if the flag was set successfully, false otherwise

◆ change_password()

std::string change_password ( const boost::uuids::uuid &  account_id,
const std::string &  new_password 
)

Changes the password for an account.

Validates password strength, hashes the new password, updates the account, and clears the password_reset_required flag.

Parameters
account_idThe ID of the account to update
new_passwordThe new plaintext password (will be hashed)
Returns
empty string on success, error message on failure

◆ get_login_info()

domain::login_info get_login_info ( const boost::uuids::uuid &  account_id)

Retrieves the login_info for a specific account.

Parameters
account_idThe ID of the account
Returns
The login_info for the account
Exceptions
std::runtime_errorIf login_info not found

◆ update_my_email()

std::string update_my_email ( const boost::uuids::uuid &  account_id,
const std::string &  new_email 
)

Updates the email address for a user's own account.

This is for self-service email updates. Validates email format.

Parameters
account_idThe ID of the account to update
new_emailThe new email address
Returns
empty string on success, error message on failure