Skip to content

CtxUsername()

ts
function CtxUsername(): string;

Returns the username of the user who logged in and started the session that triggered this script. Returns an empty string "" for events that fire before authentication has completed (e.g. OnNewConnection, BeforeSendSoftwareID, and the authentication events themselves).

Why this function exists, and why it matters

IMPORTANT

Always use CtxUsername() when your script needs to know which user the triggering event belongs to.

Syncplify Server! runs event-handler scripts asynchronously and concurrently. CtxUsername() works exactly like the other Ctx* functions: the username is captured at the moment the event fires and frozen into the script's execution context. It never changes while the script runs, regardless of what the client does in the background, and it is always safe to read, with no null-checks and no navigation through the session object.

Reaching for the user through Session.GetUser() is still perfectly valid when you need the full User object (its home directory, its permissions, its custom data), but that call can return null when no user is authenticated yet, so it always requires a guard. When the username alone is enough, CtxUsername() is the simpler and safer choice.

Return value

ScenarioReturn value
Event fired after a user has authenticatedThe username, e.g. jsmith
Event fired before authentication (connection, authentication, ...)"" (empty string)

Example

ts
{
  var user = CtxUsername();
  if (user === "") {
    Log("This event occurred before any user authenticated.");
    Exit();
  }
  Log("Event triggered by user: " + user);

  if (user === "backup" && CtxVFSName() === "Archive") {
    Log("The backup account is working on the archive VFS.");
  }
}

WARNING

CtxUsername() is not available prior to v8.0.5. Please upgrade to v8.0.5 or newer to use this function.

See also

  • EventCtx(): returns the whole event context (path, target path, VFS name, username) as a single object
  • CtxVFSName(): the name of the VFS the event refers to
  • CtxRelPath(): the path of the file that triggered the event
  • CtxRelTargetPath(): the target path of a rename or move operation
  • EventHandler(): returns the name of the event that triggered this script
  • Session.GetUser(): the full User object, when you need more than the username