summaryrefslogtreecommitdiff
path: root/app/frontend/user/DbUser.php
blob: b224120c2caafca562cebafe2e1a7d602dcce62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

Prado::using('System.Security.TDbUserManager');
Prado::using('Application.model.User');
Prado::using('Application.facades.UserFacade');

class DbUser extends TDbUser {

    private $_record;

    public function setDbRecord(User $record) {
        $this->_record = $record;
    }

    public function getDbRecord() {
        if (!$this->_record) {
            $this->_record = UserFacade::getInstance()->findByLogin($this->Name);
        }
        return $this->_record;
    }

    private $_authKey;

    public function setAuthKey(string $key) {
        $this->_authKey = $key;
    }

    public function getAuthKey() {
        return $this->_authKey;
    }

    public function createUser($username) {
        $dbUser = UserFacade::getInstance()->findByLogin($username);
        if (!$dbUser) {
            return NULL;
        }
        $user = new DbUser($this->Manager);
        $user->setDbRecord($dbUser);
        $user->Name = $dbUser->Login;
        if ($dbUser->IsAdmin) {
            $user->Roles = 'Admin';
        }
        $user->IsGuest = FALSE;
        return $user;
    }

    public function validateUser($login, $password) {
        $user = UserFacade::getInstance()->findByLogin($login);
        if (!($user && $user->IsActive)) {
            return FALSE;
        }
        $dbPassword = $user ? $user->Password : '';
        if (UserFacade::getInstance()->verifyPassword($password, $dbPassword)
            && $user) {
            $user->LastLogin = (new DateTime('UTC'))->format('Y-m-d H:i:s');
            $user->save();
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function createUserFromCookie($cookie) {
        $userFacade = UserFacade::getInstance();
        try {
            $userData = $userFacade->getUserFromCookieData($cookie);
            if ($userData) {
                $user = $this->createUser($userData->User);
                $user->AuthKey = $userData->Key;
                return $user;
            }
            return NULL;
        } catch (TInvalidDataException $e) {
            return NULL;
        }
        return NULL;
    }

    public function saveUserToCookie($cookie) {
        $userFacade = UserFacade::getInstance();
        $authKey = $userFacade->createUserAuthKey($this);
        if ($authKey) {
            $cookieData = $userFacade->compileCookieData($authKey);
            $cookie->setValue($cookieData);
            $cookie->setExpire(4294967296); // 2**32
        }
    }

    public function __call($name, $args) {
        $match = [];
        if (preg_match('/^getIs(.+)$/', $name, $match)) {
            return $this->isInRole($match[1]);
        }
        throw new Exception('Unimplemented CustomDbUser method');
    }

}

?>