std/os/user
std/os/src/user.trb
User - the account this process runs as.
User.current is a match OperatingSystem.current whose arm hands the question to the directory of that system;
only the arm of the build's target is compiled.
type User
type User with Show, Equals
The account this process runs as: who it is for the system, what people call it, and where its files are.
- Linux, macOS and FreeBSD answer the account entry of the user id the process runs as (
getpwuid_r): the login name, the GECOS field up to its first comma as the full name, the home directory, and the user id as the identifier. The process is elevated when its effective user id is0. - Windows answers the account name without its domain,
GetUserNameExW(NameDisplay)as the full name, the security identifier of the process token as the identifier,FOLDERID_Profileas the home directory, and whether the token is elevated. - A browser has no user:
OsError.Unsupported.
Examples
const user = User.current().await()?
print "{user.name} lives in {user.home}"
if user.isElevated {
print "running with administrator rights"
}
Pitfalls
identifieris text because the two kinds have nothing else in common:1000on POSIX,S-1-5-21-…on Windows. Compare it with another identifier of the same system, never with a number.homeis the account's own, as the system's account database or Known Folders say it.Directories.homeanswers theHOMEvariable first where it is set, so the two differ when the environment overrides it.isElevatedon Windows is the elevation of the token: a member of the administrators group whose program was not started as administrator is not elevated.
Related
Directories- the folders below the home directory.
field name
name: String
The login name: ada, and on Windows the account name without its domain.
field fullName
fullName: String?
The name for people: the GECOS field, GetUserNameExW(NameDisplay). None where the account has none.
field identifier
identifier: String
1000 on POSIX, the SID S-1-5-21-… on Windows: text, because the two have nothing else in common.
field home
home: Path
The account's home directory: /home/ada, /Users/ada, C:/Users/ada.
field isElevated
isElevated: Bool
Root on POSIX, an elevated token on Windows.
fn current
static fn current(): Task<Result<User, OsError>>
The account of the running process. It waits, because the account database may be a directory service on the network.
Errors
OsError.Missingwhere the process runs as a user id with no account entry, or an entry without a home.OsError.Deniedwhere the system refuses to tell.OsError.Malformedwhere the entry is not in the form the system writes: a home that is no absolute path, a security identifier that is noS-1-….OsError.Unsupportedin a browser, which has no user.
Examples
const user = User.current().await()?
print "{user.name} ({user.identifier})"