Passed
Push — Auth ( 4ce6d9...5fd30d )
by Stone
02:20
created

Auth::getLevelConst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Core\Modules;
4
5
use Core\Constant;
6
use \Core\Container;
7
8
/**
9
 * Authentication class taking care of access rights
10
 * Class Auth
11
 * @package Core\Modules
12
 */
13
class Auth extends Module
14
{
15
16
    /**
17
     * get the user type
18
     * @return mixed
19
     */
20
    public function getUser()
21
    {
22
        $session = $this->container->getSession();
23
        return $session->get('user_role_name');
24
    }
25
26
    /**
27
     * Gets the user level defined in the session (this is set on login and also stored in the DB).
28
     * Returns an int for easier user control.
29
     * @return int
30
     */
31
    public function getUserLevel(): int
32
    {
33
        $session = $this->container->getSession();
34
        return $session->get('user_role_level') ?? 0;
35
    }
36
37
    /**
38
     * Gets the user role name
39
     * @return string
40
     */
41
    public function getUserRole():string
42
    {
43
        $session = $this->container->getSession();
44
        return $session->get('user_role_name') ?? '';
45
    }
46
47
    /**
48
     * gets the configured levels defined in the constant file
49
     * @return \stdClass
50
     */
51
    public function getLevelConst():\stdClass
52
    {
53
        $levels = new \stdClass();
54
        $levels->userLevel = Constant::USER_LEVEL;
55
        $levels->adminLevel = Constant::ADMIN_LEVEL;
56
57
        return $levels;
58
    }
59
60
    /**
61
     * is the connected user an Admin
62
     * @return bool
63
     */
64
    public function isAdmin():bool
65
    {
66
        $userLevel = $this->getUserLevel();
67
        if ($userLevel >= Constant::ADMIN_LEVEL) {
68
            return true;
69
        }
70
        return false;
71
    }
72
73
    /**
74
     * is the user connected ?
75
     * @return bool
76
     */
77
    public function isUser():bool
78
    {
79
        $userLevel = $this->getUserLevel();
80
        if ($userLevel >= Constant::USER_LEVEL) {
81
            return true;
82
        }
83
        return false;
84
    }
85
}