summaryrefslogtreecommitdiff
path: root/app/frontend/user/DbUser.php
blob: d636e8b532cfc0f1cd404cf097fe467d2fcae83e (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
<?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;
    }

    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);
        $dbPassword = $user ? $user->Password : '';
        if (UserFacade::getInstance()->verifyPassword($password, $dbPassword)
            && $user) {
            $user->LastLogin = date('Y-m-d H:i:s');
            $user->save();
            return TRUE;
        } else {
            return FALSE;
        }
    }

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

}

?>