summaryrefslogtreecommitdiff
path: root/app/frontend/facades/UserFacade.php
blob: 5c8b6c0aa759d9db0b024f2b291cd1c49bd10b78 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php

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

class UserFacade extends Facade {

    public function findByLogin(string $login) {
        return User::finder()->findByLogin($login);
    }

    public function checkForUsername(string $login) {
        return !User::finder()->count('login = ?', $login);
    }

    public function registerUser(string $login, string $password, bool $admin) {
        $transaction = $this->beginTransaction();
        try {
            $newUser = new User();
            $newUser->Login = $login;
            $newUser->Password = $this->generatePassword($password);
            $newUser->IsAdmin = $admin;
            $newUser->save();
            $this->raiseEvent('UserRegistered', $newUser);
            $transaction->commit();
            return $newUser;
        } catch (Exception $e) {
            $transaction->rollback();
            throw $e;
        }
    }

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

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

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

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

    public function setTimezonePreference(DbUser $user, string $timezone) {
        if ($user->IsGuest) {
            throw new TInvalidDataException(
                Prado::localize(
                    '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 setLanguagePreference(DbUser $user, string $lang) {
        if ($user->IsGuest) {
            throw new TInvlaidDataException(
                Prado::localize(
                    'Language preference change impossible for guest user'
                )
            );
        }
        $user->DbRecord->Language = $lang;
        $user->DbRecord->save();
    }

    public function getLanguagePreference(DbUser $user) {
        if (!$user->IsGuest) {
            try {
                if ($user->DbRecord->Language) {
                    return new LanguageDTO($user->DbRecord->Language);
                }
            } catch(Exception $e) {}
        }
        return NULL;
    }

    public function setGroupedViewPreference(DbUser $user, bool $value) {
        if ($user->IsGuest) {
            throw new TInvlaidDataException(
                Prado::localize(
                    'Grouped view preference change impossible for guest user'
                )
            );
        }
        $user->DbRecord->GroupedView = $value;
        $user->DbRecord->save();
    }

    public function getGroupedViewPreference(DbUser $user) {
        if (!$user->IsGuest) {
            return TPropertyValue::ensureBoolean(
                $user->DbRecord->GroupedView
            );
        }
        return FALSE;
    }

}

?>