pub struct UserRegistry { /* private fields */ }Expand description
Daemon-level user registry with persistent storage.
Manages user lifecycle, token auth, room membership, and global status.
All mutations auto-save to {data_dir}/users.json.
Implementations§
Source§impl UserRegistry
impl UserRegistry
Sourcepub fn new(data_dir: PathBuf) -> Self
pub fn new(data_dir: PathBuf) -> Self
Create a new empty registry backed by the given directory.
Does not load from disk — use UserRegistry::load for that.
Sourcepub fn load(data_dir: PathBuf) -> Result<Self, String>
pub fn load(data_dir: PathBuf) -> Result<Self, String>
Load an existing registry from {data_dir}/users.json.
Returns a fresh empty registry if the file does not exist. Returns an error only if the file exists but cannot be parsed.
Sourcepub fn register_user(&mut self, username: &str) -> Result<&User, String>
pub fn register_user(&mut self, username: &str) -> Result<&User, String>
Register a new user. Fails if the username is already taken.
Sourcepub fn remove_user(&mut self, username: &str) -> Result<bool, String>
pub fn remove_user(&mut self, username: &str) -> Result<bool, String>
Remove a user and all their tokens.
Returns true if the user existed.
Sourcepub fn list_users(&self) -> Vec<&User>
pub fn list_users(&self) -> Vec<&User>
List all registered users.
Sourcepub fn issue_token(&mut self, username: &str) -> Result<String, String>
pub fn issue_token(&mut self, username: &str) -> Result<String, String>
Issue a new token for a registered user.
The user must already be registered via [register_user].
Sourcepub fn validate_token(&self, token: &str) -> Option<&str>
pub fn validate_token(&self, token: &str) -> Option<&str>
Validate a token, returning the associated username.
Sourcepub fn revoke_token(&mut self, token: &str) -> Result<bool, String>
pub fn revoke_token(&mut self, token: &str) -> Result<bool, String>
Revoke a specific token. Returns true if it existed.
Sourcepub fn revoke_user_tokens(&mut self, username: &str) -> Result<usize, String>
pub fn revoke_user_tokens(&mut self, username: &str) -> Result<usize, String>
Revoke all tokens for a user. Returns the number revoked.
Sourcepub fn join_room(&mut self, username: &str, room_id: &str) -> Result<(), String>
pub fn join_room(&mut self, username: &str, room_id: &str) -> Result<(), String>
Record that a user has joined a room.
Sourcepub fn leave_room(
&mut self,
username: &str,
room_id: &str,
) -> Result<bool, String>
pub fn leave_room( &mut self, username: &str, room_id: &str, ) -> Result<bool, String>
Record that a user has left a room.
Sourcepub fn set_status(
&mut self,
username: &str,
status: Option<String>,
) -> Result<(), String>
pub fn set_status( &mut self, username: &str, status: Option<String>, ) -> Result<(), String>
Set or clear a user’s global status.
Pass None to clear. Status applies across all rooms the user is in.
Sourcepub fn has_token_for_user(&self, username: &str) -> bool
pub fn has_token_for_user(&self, username: &str) -> bool
Return true if any token is currently associated with username.
Used by daemon auth to detect username collisions without scanning the entire token map externally.
Sourcepub fn register_user_idempotent(&mut self, username: &str) -> Result<(), String>
pub fn register_user_idempotent(&mut self, username: &str) -> Result<(), String>
Register a user if not already registered; no-op if already present.
Unlike [register_user], this is idempotent — calling it for an
existing user does not return an error. Used by daemon auth so that
users from a previous session (loaded from users.json) can rejoin
without triggering a registration error.
Sourcepub fn token_snapshot(&self) -> HashMap<String, String>
pub fn token_snapshot(&self) -> HashMap<String, String>
Return a snapshot of all current token → username mappings.
Used at daemon startup to seed the in-memory TokenMap from persisted
registry data so existing tokens remain valid without a fresh join.
Sourcepub fn import_token(
&mut self,
username: &str,
token: &str,
) -> Result<(), String>
pub fn import_token( &mut self, username: &str, token: &str, ) -> Result<(), String>
Insert a pre-existing token UUID for a registered user.
Unlike [issue_token], which generates a fresh UUID, this method
preserves the caller-supplied token string. It is intended for
migration paths that read legacy token files (e.g. /tmp/room-*-*.token)
and want existing clients to remain valid without a forced re-join.
Returns Ok(()) immediately if the token is already present in the
registry (idempotent). Returns an error if username is not registered.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for UserRegistry
impl RefUnwindSafe for UserRegistry
impl Send for UserRegistry
impl Sync for UserRegistry
impl Unpin for UserRegistry
impl UnsafeUnpin for UserRegistry
impl UnwindSafe for UserRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more