Completed
Push — master ( 41ea8c...3bb2fe )
by Afshin
03:08
created

AuthService::check()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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
23
    public function check()
24
    {
25
        return isset($_SESSION['user']['user_id']);
26
    }
27
28
    public function attempt(string $loginField,string $password)
29
    {
30
        $user = UserDataAccess::getUserLoginField($loginField);
31
        if (!$user) {
32
            return [
33
                'type'=>'error',
34
                'message'=> 'User Not Exists',
35
            ];
36
        }
37
        $setting = Config::get('settings.auth');
38
        if($setting['2step']){
39
            $this->twoStepAuth();
40
        }else{
41
            if ($this->checkPass($password,$user->password)) {
0 ignored issues
show
Bug introduced by
The property password does not seem to exist on App\Model\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
42
                $_SESSION['user']['user_id'] = $user->id;
43
                $_SESSION['user']['mobile'] = $user->mobile;
0 ignored issues
show
Bug introduced by
The property mobile does not seem to exist on App\Model\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
44
                return [
45
                    'type'=>'success',
46
                    'message'=> 'Logined',
47
                ];
48
            }else{
49
                return [
50
                    'type'=>'error',
51
                    'message'=> 'password mismatch',
52
                ];
53
            }
54
        }
55
        return [
56
            'type'=>'error',
57
            'message'=> 'problem!',
58
        ];
59
    }
60
61
62
63
    public function checkPass($password,$database_pass)
64
    {
65
        if($database_pass == $password){
66
            return true;
67
        }
68
        return false;
69
70
    }
71
72
73
    public function twoStepAuth()
74
    {
75
        return true;
76
    }
77
78
    public function logout()
79
    {
80
        unset($_SESSION['user']);
81
    }
82
83
}