Managing Current User

since Codoforum V2.2

The user can be accessed using the class User

Get the user object

$user = User::get();

If you do not pass any id, it returns the current user . But you can get a object of any other user by passing their userid.

$someUser = User::get(2);
//Above method returns an object of the user with userid 2.

You can also get the user object using following methods

$user = User::getByMail($mail);
//returns user object if any of the below exists in user table
$user = User::getByMailOrUsername($mail, $username);
$user = User::getByIdOrUsername($userid, $username);

Now, you have access to the following properties:

$user->id; //get userid
$user->username; //get username
$user->name; //get name
$user->mail; //get user's email address
$user->rid; //get user's role id
$user->created; //created time [in seconds]
$user->last_access; //last login time
$user->user_status; //boolean indicating whether the user is confirmed
$user->no_posts; //Number of posts made by the user
$user->profile_views; //Number of profile views

Note: If the user is not logged in and if you have not passed an id while calling User:get(), you will get a default user object for guest .

After , you get the $user object, you can use the following methods:

Find if the user is logged in

if($user->loggedIn()) {

  //the user is logged in
}

Check permission

Sometimes, you may have to check if the user has permissions to perform an action .

You can check that by using the $user->can() method

$user = User::get();
if($user->can('delete all topics')) {

   //Let him delete the topic
}

You can also check multiple permissions at once by passing an array of permissions using the following methods:

$user->canAny(array $permissions)

Returns true if any of the permissions are satisfied

$user->canAll(array $permissions)

Returns true only if all of the permissions are satisfied

Example.

if($user->canAll(array('delete my post', 'create topic'))) {

   //let him delete his own post
   //let him create a new topic
}

You can look at the table codo_role_permissions for a list of all permissions

Log out the user

If you want to log the user out of codoforum

User::logout();

Increment post count

Post count is the number of posts made by user in the forum .

$user->incPostCount();

Decrement post count

$user->decPostCount();

Increment profile views

Increments profile views of the user by 1

$user->incProfileViews();

Get user information

If you want the user information i.e username, mail, etc as a array , you can use the following method

$info = $user->getInfo();
echo 'Hi ' . $info['username'] . ', you have made ' . $info['no_posts'] . ' posts';

Update user

To update user information in the database you can use the method:

$user->set(array $values);

$values is an associative array in the form

$values = array('username' => 'Jackson');

Example.

$user->set(array(
   "name" => "Jason X",
   "last_access" => time()
));
//Above method will update the user's name and set his last login time

Check password

Check if a password matches the user's account password .

 if($user->checkPassword('1234')) {

    //your password is correct
 }

Update password

Updates the password of the user . This method hashes the password passed and then uses the set() to update the password in the database

$user->updatePassword('MyNewPassword');

remember Me

Sets a remember me cookie , so that codoforum logs the user in automatically when he visits the website next time .

$user->rememberMe();



The User class provides the following static methods :


Log the user in

To programatically login the user, you can use .

User::login($userid);
User::loginByMail($email);

Example

Login the user , with userid 2

User::login(2);

Login the user, with e-mail address user@domain.com

if(User::loginByMail('user@domain.com')) {

    $user = User::get();
    echo 'Hello ' . $user->username;
}

Returns true on success else false

Note: When this function is called, it also sets the user's last login time



Some helper methods


Get role name

Get the role name by role id

$rid = $user->rid;
$role = User::getRoleName($rid);

Get login url

echo '<a href=' . User::getLoginUrl() . '>' . _t("Login") . '</a>';

Note: _t() is used for translation

Get logout url

echo '<a href=' . User::getLogoutUrl() . '>' . _t("Logout") . '</a>';

Get register url

echo '<a href=' . User::getRegisterUrl() . '>' . _t("Register") . '</a>';

Get profile url

echo '<a href=' . User::getProfileUrl() . '>' . _t("My profile") . '</a>';

Check username exists

Checks if the given username exists in the users table

User::usernameExists($username);

Check mail exists

Checks if the given mail exists in the users table

User::mailExists($mail);
© CODOFORUM 2018. All Rights Reserved.