hanze/iwa-panda1

classes/Login_handler.php in tak
Repositories | Summary | Log | Files

Login_handler.php (1639B) download


 1<?php
 2class Login_handler
 3{
 4    function login(string $email, string $pwd) : bool
 5    //this function return true when user is autheticated uses set_globals to set $_SESSION variables
 6    {
 7        //create a SQLDatabase class
 8        $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2");
 9        //create a Database object class, with the table User
10        $u = $db->get(User::class);
11        
12        //check if the email exists in db
13        if(!$u->where('email', $email)){
14            //email does not exist
15            return false;
16        }else{
17            if(password_verify($pwd, $u->password)){
18                //authenticated -> set $_SESSION variables
19                $this->set_globals($u, $db);             
20                return true;
21            } else {
22                //password did not match
23                return false;
24            }
25        }
26    }
27     
28    private function set_globals(Lollipop\DatabaseObject $u, Lollipop\SQLDatabase $db)
29    //this function sets Session variables which incluse
30    //email, first_name, last_name and array user_permissions
31    {
32        //start session and set
33        session_start();
34        $_SESSION['email'] = $u->email;
35        $_SESSION['first_name'] = $u->first_name;
36        $_SESSION['last_name'] = $u->last_name;
37
38        //get permissions form db and set sessions_permissions
39        $p = $db->all_where(Permission_user::class, array('user_id' => $u->user_id));
40        foreach($p as $permission){
41            $user_permissions[] = $permission->permission_id;
42        }
43        $_SESSION['user_permissions'] = $user_permissions;
44    }
45}
46?>