summaryrefslogtreecommitdiff
path: root/app/php/facades/UserFacade.php
blob: 696f77f86b3d8afdd9ca4862315ebc7c1e123fdd (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
<?php

Prado::using('Application.facades.Facade');
Prado::using('Application.user.DbUser');
Prado::using('Application.model.User');
Prado::using('Application.dto.TimezoneDTO');

class UserFacade extends Facade {

    public function setTimezonePreference(DbUser $user, $timezone) {
        if ($user->IsGuest) {
            throw new TInvalidDataException(
                'Timezone preference change impossible for guest user'
            );
        }
        $user->DbRecord->Timezone = $timezone;
        $user->DbRecord->save();
    }

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

    public function checkForUsername($login) {
        return !User::finder()->countByLogin($login);
    }

    public function registerUser($login, $password, $admin) {
        $newUser = new User();
        $newUser->Login = $login;
        $newUser->Password = $this->generatePassword($password);
        $newUser->IsAdmin = $admin;
        $newUser->save();
        return $newUser;
    }

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

    public function verifyUserPassword($password, DbUser $user) {
        $dbPassword = $user->IsGuest ? '' : $user->DbRecord->Password;
        return $this->verifyPassword($password, $dbPassword);
    }

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

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



}

?>