134 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			134 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require_once "AutoStoring.php";
 | 
						|
 | 
						|
class User extends AutoStoring
 | 
						|
{
 | 
						|
    private int $id; // User's ID
 | 
						|
    private string $nickname; // User's nickname
 | 
						|
    private string $password; // User's password in it's encoded form or left empty
 | 
						|
    private string $realname; // User's real name displayed in their profile
 | 
						|
//    private array $groups; // User's assigned groups
 | 
						|
    private string $privilege; // User's privilege
 | 
						|
    private UserMgr $userMgr; // UserManager object governing this object.
 | 
						|
 | 
						|
    // -------------------------------------------
 | 
						|
 | 
						|
    // Store modifications to the database.
 | 
						|
    public function storeMods(): void
 | 
						|
    {
 | 
						|
        $this->userMgr->updateUser($this);
 | 
						|
    }
 | 
						|
 | 
						|
    // -------------------------------------------
 | 
						|
 | 
						|
    function __construct(UserMgr &$usrmgr, int $id, string $nickname = null, string $password = null, string $realname = null, string $privilege = null)
 | 
						|
    {
 | 
						|
        parent::__construct();
 | 
						|
 | 
						|
        $this->id = $id;
 | 
						|
        $this->nickname = $nickname;
 | 
						|
        $this->password = $password;
 | 
						|
        $this->realname = $realname;
 | 
						|
//        $this->groups = $groups;
 | 
						|
        $this->privilege = $privilege;
 | 
						|
 | 
						|
        // save reference to user manager
 | 
						|
        $this->userMgr = &$usrmgr;
 | 
						|
    }
 | 
						|
 | 
						|
    // Create user from an array
 | 
						|
    static function fromArray(UserMgr &$usrmgr, array $a): User
 | 
						|
    {
 | 
						|
        $id = $a["_id"] ?? -1;
 | 
						|
        return new User($usrmgr, $id, $a["nickname"], $a["password"], $a["realname"], $a["privilege"]);
 | 
						|
    }
 | 
						|
 | 
						|
    // Convert user to array
 | 
						|
    function toArray(array $omit = []): array
 | 
						|
    {
 | 
						|
        $a = [
 | 
						|
            "_id" => $this->id,
 | 
						|
            "nickname" => $this->nickname,
 | 
						|
            "password" => $this->password,
 | 
						|
            "realname" => $this->realname,
 | 
						|
//            "groups" => $this->groups,
 | 
						|
            "privilege" => $this->privilege
 | 
						|
        ];
 | 
						|
 | 
						|
        // omit specific fields
 | 
						|
        foreach ($omit as $field) {
 | 
						|
            unset($a[$field]);
 | 
						|
        }
 | 
						|
 | 
						|
        return $a;
 | 
						|
    }
 | 
						|
 | 
						|
    // Change user password. If $safe, then $old is checked.
 | 
						|
    function changePassword(string $new, string $old, bool $safe = true): bool
 | 
						|
    {
 | 
						|
        if (!$safe || password_verify($old, $this->password)) {
 | 
						|
            $this->password = password_hash($new, PASSWORD_DEFAULT);
 | 
						|
            $this->storeMods(); // store modifications
 | 
						|
            return true;
 | 
						|
        } else {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
//    // Change user groups
 | 
						|
//    function changeGroups(array $add, array $remove): void
 | 
						|
//    {
 | 
						|
//        alter_array_contents($this->groups, $add, $remove);
 | 
						|
//        $this->storeMods(); // store modifications
 | 
						|
//    }
 | 
						|
 | 
						|
//    // Get user's groups
 | 
						|
//    function getGroups(): array
 | 
						|
//    {
 | 
						|
//        return $this->groups;
 | 
						|
//    }
 | 
						|
 | 
						|
    // Set user privilege level
 | 
						|
    function setPrivilege(string $privilege): void
 | 
						|
    {
 | 
						|
        $this->privilege = ($this->nickname === QUIZMASTER_NICKNAME) ? PRIVILEGE_QUIZMASTER : $privilege; // quizmaster's privilege mustn't be tampered with
 | 
						|
        $this->storeMods(); // store modifications
 | 
						|
    }
 | 
						|
 | 
						|
    // Get user privilege level
 | 
						|
    function getPrivilege(): string
 | 
						|
    {
 | 
						|
        return $this->privilege;
 | 
						|
    }
 | 
						|
 | 
						|
    // Get user's nickname.
 | 
						|
    function getNickname(): string
 | 
						|
    {
 | 
						|
        return $this->nickname;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set user's real name.
 | 
						|
    function setRealname(string $realname): void
 | 
						|
    {
 | 
						|
        $this->realname = $realname;
 | 
						|
    }
 | 
						|
 | 
						|
    // Get user's real name.
 | 
						|
    function getRealname(): string
 | 
						|
    {
 | 
						|
        return $this->realname;
 | 
						|
    }
 | 
						|
 | 
						|
    // Check against user credentials.
 | 
						|
    function checkPassword(string $password): bool
 | 
						|
    {
 | 
						|
        return password_verify($password, $this->password);
 | 
						|
    }
 | 
						|
 | 
						|
    // Has the user quizmaster privileges?
 | 
						|
    function hasQuizmasterPrivilege(): bool
 | 
						|
    {
 | 
						|
        return $this->privilege == PRIVILEGE_QUIZMASTER;
 | 
						|
    }
 | 
						|
} |