The user API variable is your connection to the current user viewing the page.

Unless a user is logged in, $user is always referring to the guest user account. If the user is logged in, then $user can be the superuser or any other user that you may have added to the system.


User properties

$user->nameThe user's login name.
$user->passThe user's hashed password. Note that this returns a hashed version of the password, so it is not typically useful to get this property. However, it is useful to set this property if you want to change the password. When you change a password, it is assumed to be the non-hashed/non-encrypted version. ProcessWire will hash it automatically when the user is saved.
$user->emailThe user's email adress.

You may get and set these user properties using either direct reference (i.e $user->property) or by get() and set() functions (i.e. $user->get("property")). In addition, you can make up your own property names and assign them to $user object in this same fashion. A User is just another type of Page, and you can add new fields to it like any other Page. To add new fields to User, go to Setup > Templates > Filters > Show System Templates > and select the "user" template.


User functions

$user->isGuest()Is this the Guest user? Returns true or false.
$user->isSuperuser()Is this the Superuser? Returns true or false.
$user->isLoggedin()Is this user logged in? Returns true or false.
$user->save()Save user to the database.
$user->get($property)Get a property value from the user. Same as $user->[property].
$user->set($property, $value)Set a property value. Same as $user->[property] = $value;

The following functions deal with roles and permissions. For functions dealing with roles, you may specify either the role's name (string) or the role's instance (object of type Role). Likewise, for permissions, you may specify the permission name or object (of type Permission), though it is more common to specify the permission name (string).

$user->hasRole($role)Does the user have the given role? Returns true or false.
$user->addRole($role)Add the given Role to this user.
$user->removeRole($role)Remove a given Role from this user.
$user->hasPermission($permission)Does the user have the given permission? Returns true or false.
$user->hasPermission($permission, $page)Does the user have the given permission name on $page? Returns true or false.

Note that a User is just another type of Page, and likewise inherits all the other methods of $page.


Examples

Depending on the context, you may have to call $user->of(false); to turn off output formatting before assigning values to a user.

Changing the user's password

<?php
$user->pass = "Bam!";
$user->save(); 

Adding a new user with the "superuser" role

<?php
$u = $users->add('gonzo');
$u->pass = "BamBam!";
$u->addRole("superuser");
$u->save();

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.