summaryrefslogtreecommitdiff
path: root/html/app/controllers/Auth.php
blob: c75f9794f21e7cea10e4d80c00576a1a8f2c41e0 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php

namespace app\controllers;

use Registry;
use UserManager;
use User;
use app\models\user\User_model;


/** class Auth
 * 
 * handles user's Authentication and Authorizarion
 * 
 */
class Auth {

    /** login
     * 
     * checks visitor's credentials;
     * if valid, authenticates user
     * 
     */
    public static function login()
    {
        $req = Registry::get('REQUEST');

        // get the record of the target user
        $record = User_model::checkUser('email', $req->POST['email']);
       
        // if no user exists, return false
        if ($record === false)     return false;
       
        // create a user object
        $user = (new User())
            ->setUserName($record['email'])
            ->setPassword($record['password'])
            ->setRoles(json_decode($record['roles']))
            ->setEnabled($record['active']);

        // let user manager to validate user credentials
        $userManager = new UserManager();
        
        if ($userManager->isPasswordValid($user, $req->POST['password'])) {
    
            // login OK, set Token in session
            $userManager->createUserToken($user);
    
        } else {
            return false;
        }
    }


    /** register
     * 
     * registers new user
     * 
     */
    public static function register()
    {
        $userManager = new UserManager();
        $req = Registry::get('REQUEST');

        // create a salted password hash
        $password = $userManager->cryptPassword($req->POST['password']);
        
        $user = (new User())
            ->setUserName($req->POST['username'])
            ->setPassword($password)
            ->setRoles(['ROLE_USER']);

        // TODO:
        // store user to database

        $userManager->createUserToken($user);
    }



    public static function logout()
    {
        $userManager = new UserManager();
        $userManager->logout();
    }



    /** isGranted( ROLE )
     * 
     * checks if the user is granted (some of) the specified role(s)
     * to access the source
     * 
     * NOTE:
     * if no roles are specified then user is granted
     * (because every user is granted the 'no-role')
     * 
     * @param $roles (array): array of roles to check (if any is granted)
     * 
     */
    public static function isGranted($roles = [])
    {
        // no role required ? user is granted access
        if ($roles == [])   return true;

        // else, UserManager knows if user isGranted
        $userManager = new UserManager();
        if ($userManager->isGranted($roles)) {
            return true;

        } else {
            return false;
        }
    }


    /** hasPermition( PERMIT )
     * 
     * checks if the user owns the specified permition
     * to access the source
     * 
     */
    public static function hasPermition($permit = [])
    {
        if ($permit == [])    return true;
    }


    /** isAuthenticated()
     * 
     * chechs if the user's roles and permitions
     * satisfy the specified requirements 
     * to access the source
     * 
     * @param $requirements (array of rules-array)
     * 
     * example:
     * [
     *    [
     *       role => ['editor','designer']
     *       permition => ['10', '12', '18']
     *    ],
     *    [
     *       role => ['admin' , 'developερ']
     *    ],
     *    [
     *       permition => [ 3 ]
     *    ]
     * ]
     * 
     * defines (and parses to) a requirements rule of:
     * [
     *    user should be editor or designer
     *    and have permition 10 or 12 or 18
     * ]
     * OR
     * [
     *    user should be an administratoe or developer
     * ]
     * OR
     * [
     *    user should have permition #3
     * ]
     *
     * 
     */
    public static function isAuthorized($requirements)
    {
        $authorized = false;
        foreach($requirements as $required) {
            if ( (self::isGranted($required['role'] ?? []))
            && (self::hasPermition($required['permition'] ?? [])) ) {
                $authorized = true;
            }
        }
        return $authorized;
    }




    public static function forgot_pass()
    {
    }



    public static function validate_otp()
    {
    }


    
}