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

Prado::using('System.Security.TDbUserManager');
Prado::using('Application.model.User');
Prado::using('Application.model.Calendar');
Prado::using('Application.dto.TimezoneDTO');

class DbUser extends TDbUser {

    private $_record;

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

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

    public function createUser($username) {
        $dbUser = User::finder()->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 = User::finder()->findByLogin($login);
        $dbPassword = $user ? $user->Password : '';
        if (self::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');
    }

    public function getCalendarPreference() {
        if ($this->IsGuest) {
            return Calendar::finder()->findAllByIsVisible(1);
        } else {
            return $this->DbRecord->Calendars;
        }
    }

    public function getTimezonePreference() {
        if (!$this->IsGuest) {
            try {
                return new TimezoneDTO($this->DbRecord->Timezone);
            } catch(Exception $e) {}
        }
        return new TimezoneDTO(date_default_timezone_get());
    }

    public function changePassword($pass) {
        if (!$this->IsGuest) {
            $this->DbRecord->Password = self::generatePassword($pass);
            $this->DbRecord->save();
        }
    }

    public function getPassword() {
        if (!$this->IsGuest) {
            return $this->DbRecord->Password;
        }
    }

    public static function generatePassword($password) {
        return password_hash($password, PASSWORD_DEFAULT);
    }

    public static function verifyPassword($password, $dbPassword) {
        return password_verify($password, $dbPassword);
    }

}

?>