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.
|The user's login name.|
|The 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.|
|The 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.
|Is this the Guest user? Returns true or false.|
|Is this the Superuser? Returns true or false.|
|Is this user logged in? Returns true or false.|
|Save user to the database.|
|Get a property value from the user. Same as $user->[property].|
|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).
|Does the user have the given role? Returns true or false.|
|Add the given Role to this user.|
|Remove a given Role from this user.|
|Does the user have the given permission? Returns true or false.|
|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
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();
No comments yet. Be the first to post!
Your e-mail is kept confidential and not included with your comment. Website is optional.