![]() |
VR-Forces Developer's Guide
|
DtUserStore — process-wide singleton for loading and querying the user database.
Users are defined in a JSON file with the following format:
The "passwordHash" field is optional. An absent or empty value means the account requires no password. Hashes are produced by DtPasswordHasher::hash().
After initialize() returns the store may be mutated at runtime via addUser(), removeUser(), setPasswordHash(), and setPermissions(). Each mutation is persisted back to the JSON file immediately. All public methods are thread-safe.
If the file does not exist, a default file with an admin and a viewer user is created and a warning is logged.
Classes | |
| struct | DtUserEntry |
Public Member Functions | |
| void | initialize (const std::string &usersFilePath) |
| bool | userExists (const std::string &username) const |
| std::vector< std::string > | getUserPermissions (const std::string &username) const |
| std::string | getPasswordHash (const std::string &username) const |
| std::vector< std::string > | usernames () const |
| bool | addUser (const std::string &username, const std::vector< std::string > &permissions, const std::string &passwordHash) |
| bool | addUserWithPreset (const std::string &username, const std::string &preset, const std::vector< std::string > &extraPerms, const std::string &passwordHash) |
| std::string | getUserPreset (const std::string &username) const |
| std::map< std::string, std::vector< std::string > > | getPresets () const |
| bool | removeUser (const std::string &username) |
| bool | setPasswordHash (const std::string &username, const std::string &passwordHash) |
| bool | setPermissions (const std::string &username, const std::vector< std::string > &permissions) |
| bool | setPreset (const std::string &username, const std::string &preset) |
Static Public Member Functions | |
| static DtUserStore & | getInstance () |
Private Member Functions | |
| DtUserStore () | |
| bool | persistToFile () const |
Private Attributes | |
| std::map< std::string, DtUserEntry > | myUsers |
| std::map< std::string, std::vector< std::string > > | myPresets |
| std::string | myUsersFilePath |
| std::mutex | myMutex |
|
private |
|
static |
Returns the process-wide singleton instance.
| void DtUserStore::initialize | ( | const std::string & | usersFilePath | ) |
Load users from the given JSON file path. Creates a default file with admin/viewer users if the path does not exist. Must be called exactly once from main() before any background threads start.
| bool DtUserStore::userExists | ( | const std::string & | username | ) | const |
Returns true if a user with the given username exists in the store.
| std::vector<std::string> DtUserStore::getUserPermissions | ( | const std::string & | username | ) | const |
Returns the permission strings for the given user. The returned list is the union of the user's preset permissions (if any) and their individual extra permissions. Duplicates are removed. Returns an empty vector if the user does not exist.
| std::string DtUserStore::getPasswordHash | ( | const std::string & | username | ) | const |
Returns the stored password hash for the given user. Returns an empty string if the user does not exist or has no password set.
| std::vector<std::string> DtUserStore::usernames | ( | ) | const |
Returns a list of all known usernames.
| bool DtUserStore::addUser | ( | const std::string & | username, |
| const std::vector< std::string > & | permissions, | ||
| const std::string & | passwordHash | ||
| ) |
Adds a new user with the given permissions and optional password hash.
| passwordHash | Pass DtPasswordHasher::hash(password) or empty for no password. |
| bool DtUserStore::addUserWithPreset | ( | const std::string & | username, |
| const std::string & | preset, | ||
| const std::vector< std::string > & | extraPerms, | ||
| const std::string & | passwordHash | ||
| ) |
Adds a new user whose permissions are derived from a named preset, with an optional list of additional (extra) permissions on top.
| preset | Name of a preset defined in presets; empty string for none. |
| extraPerms | Additional permissions beyond the preset (may be empty). |
| passwordHash | Pass DtPasswordHasher::hash(password) or empty for no password. |
| std::string DtUserStore::getUserPreset | ( | const std::string & | username | ) | const |
Returns the preset name assigned to the given user. Returns an empty string if the user does not exist or has no preset.
| std::map<std::string, std::vector<std::string> > DtUserStore::getPresets | ( | ) | const |
Returns all defined presets as a map of preset-name → permission list.
| bool DtUserStore::removeUser | ( | const std::string & | username | ) |
Removes the user with the given username.
| bool DtUserStore::setPasswordHash | ( | const std::string & | username, |
| const std::string & | passwordHash | ||
| ) |
Replaces the password hash for an existing user. Pass an empty string to remove the password requirement.
| bool DtUserStore::setPermissions | ( | const std::string & | username, |
| const std::vector< std::string > & | permissions | ||
| ) |
Replaces the full permission list for an existing user. Replaces only the user's extra permissions; the preset field is preserved.
| bool DtUserStore::setPreset | ( | const std::string & | username, |
| const std::string & | preset | ||
| ) |
Sets the preset for an existing user. Pass an empty string to remove the preset assignment. The user's extra permissions are not modified.
|
private |
Serializes myUsers and myPresets back to myUsersFilePath. Must be called while myMutex is held.
|
private |
|
private |
|
private |
|
mutableprivate |