User.php

<?php

namespace Tlf;

/**
 * The User object. It does... usertype stuff.
 * 
 * @export(Class.User)
 */
class User {

    protected $model;
    public $name;
    public $email;
    public $id;

    protected $backend;

    public function __construct($dbModel, $backend){
        $this->model = $dbModel;
        $this->backend = $backend;
        
        if ($dbModel==null){
            $this->name = 'guest';
        } else {
            $this->name = $this->model->first_name ?? 'anonymous';
        }
        $this->email = $this->model->email ?? null;
        $this->id = $this->model->id ?? null;
    }
    
    public function slug(){
        return 'user-'.$this->id;
    }

    public function logout(){
        return $this->backend->logoutUser();
    }
    public function logoutEverywhere(){
        return $this->backend->logoutEverywhere($this->model);
    }

    public function isLoggedIn(){
        if ($this->model==null)return false;
        if (!$this->isActive())return false;
        if ($this->model==$this->backend->getLoggedInUser())return true;
        return false;
    }
    public function isActive(){
        if ($this->model==null)return false;
        if ($this->backend->hasCompletedActivation($this->model))return true;
        return false;
    }
    public function isGuest(){
        //guests simply don't have a user account. (but we still sling a user object)
        if ($this->model==null
            ||$this->is('guest'))return true;
        return false;
    }
    public function isAdmin(){
        return $this->is('admin');
    }


    public function is($role){
        return $this->backend->isRole($this,$role);
    }

    public function can($action, $withId=null){
        return $this->backend->can($this, $action, $withId);
    }

    public function permit($action, $withId=null){
        return $this->backend->permit($this, $action, $withId);
    }
    public function revoke($action, $withId=null){
        return $this->backend->revoke($this, $action, $withId);
    }
    public function forbid($action, $withId=null){
        return $this->backend->forbid($this, $action, $withId);
    }
    public function permissions(){
        return $this->backend->getPermissions($this);
    }
    public function roles(){
        return $this->backend->getRoles($this);
    }
    public function addRole($role){
        return $this->backend->addRole($this, $role);
    }




    public function model(){
        return $this->model;
    }
    /**
     * check if this user is the given user
     * 
     * @param \Tlf\User $user
     * @return boolean true if user ids match, false otherwise
     */
    public function isUser(\Tlf\User $user){
        if ($user->id!=$this->id)return false;
        return true;
    }
    


    
    static public function newPackage($liaison, $configs=[]){
        $package = new \Lia\Package($liaison, dirname(__DIR__),$configs);
        return $package;
    }

}