Completed
Push — master ( 5fc065...3e93a4 )
by Afshin
02:37
created

AuthService::hasRole()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: afshin
5
 * Date: 11/24/17
6
 * Time: 1:11 PM
7
 */
8
9
namespace Core\Services;
10
11
use App\DataAccess\User\UserDataAccess;
12
use Core\Config;
13
use Core\Interfaces\_Service;
14
15
class AuthService extends _Service
16
{
17
    public function user()
18
    {
19
        return UserDataAccess::getUserById(isset($_SESSION['user']['user_id']) ? $_SESSION['user']['user_id'] : 0);
20
    }
21
22
    public function hasRole($roleName)
23
    {
24
        if(!self::check()){
0 ignored issues
show
Bug Best Practice introduced by
The method Core\Services\AuthService::check() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        if(!self::/** @scrutinizer ignore-call */ check()){
Loading history...
25
            return false;
26
        }
27
        $userRoles =  UserDataAccess::getUserRoles(isset($_SESSION['user']['user_id']) ? $_SESSION['user']['user_id'] : 0);
28
        $hasAccess = false;
29
        foreach ($userRoles as $role){
30
            if($role->name == $roleName){
31
                $hasAccess = true;
32
                break;
33
            }
34
        }
35
        return $hasAccess;
36
    }
37
38
39
    public function check()
40
    {
41
        return isset($_SESSION['user']['user_id']);
42
    }
43
44
    public function attempt(string $loginField,string $password)
45
    {
46
        $user = UserDataAccess::getUserLoginField($loginField);
47
        if (!$user) {
48
            return [
49
                'type'=>'error',
50
                'message'=> 'User Not Exists',
51
            ];
52
        }
53
        $setting = Config::get('settings.auth');
54
        if($setting['2step']){
55
            $this->twoStepAuth();
56
        }else{
57
            if ($this->checkPass($password,$user->password)) {
58
                $_SESSION['user']['user_id'] = $user->id;
59
                $_SESSION['user']['mobile'] = $user->mobile;
60
                return [
61
                    'type'=>'success',
62
                    'message'=> 'Logined',
63
                ];
64
            }else{
65
                return [
66
                    'type'=>'error',
67
                    'message'=> 'password mismatch',
68
                ];
69
            }
70
        }
71
        return [
72
            'type'=>'error',
73
            'message'=> 'problem!',
74
        ];
75
    }
76
77
78
79
    public function checkPass($password,$database_pass)
80
    {
81
        if($database_pass == $password){
82
            return true;
83
        }
84
        return false;
85
86
    }
87
88
89
    public function twoStepAuth()
90
    {
91
        return true;
92
    }
93
94
    public function logout()
95
    {
96
97
        unset($_SESSION['user']);
98
    }
99
100
}