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

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

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 = array();
        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 static function generatePassword($password) {
        return password_hash($password, PASSWORD_DEFAULT);
    }

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

}

?>