Passed
Push — Admin-section ( ed5398...dfce56 )
by Stone
02:29
created

Auth   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isAdmin() 0 7 2
A getUserLevel() 0 19 4
A getUser() 0 4 1
A isUser() 0 7 2
1
<?php
2
3
namespace Core\Modules;
4
5
use \Core\Container;
6
7
/**
8
 * Authentication class taking care of access rights
9
 * Class Auth
10
 * @package Core\Modules
11
 */
12
class Auth extends Module
13
{
14
15
    //TODO session_level isn't explicit enough. Have to change things up a bit. Probably use user_role_name and user_role_level
16
17
    /**
18
     * get the user type
19
     * @return mixed
20
     */
21
    public function getUser()
22
    {
23
        $session = $this->container->getSession();
24
        return $session->get('session_level');
25
    }
26
27
    /**
28
     * Gets the user level defined in the session (this is set on login and also stored in the DB).
29
     * Returns an int for easier user control.
30
     * @return int
31
     */
32
    public function getUserLevel()
33
    {
34
        $session = $this->container->getSession();
35
        //For testing, setting the user level
36
        //$session->set('session_level', 'Admin');
37
38
        //get session level from the actual $_SESSION
39
        $sessionLevel = $session->get('session_level');
40
        //we could use a binary system for the rights but not much granular levels to take care of
41
        if ($sessionLevel) {
42
            if ($sessionLevel === 'Admin') {
43
                return 2;
44
            }
45
            if ($sessionLevel === 'User') {
46
                return 1;
47
            }
48
        }
49
50
        return 0;
51
    }
52
53
    /**
54
     * is the connected user an Admin
55
     * @return bool
56
     */
57
    public function isAdmin()
58
    {
59
        $userLevel = $this->getUserLevel();
60
        if ($userLevel > 1) {
61
            return true;
62
        }
63
        return false;
64
    }
65
66
    /**
67
     * is the user connected ?
68
     * @return bool
69
     */
70
    public function isUser()
71
    {
72
        $userLevel = $this->getUserLevel();
73
        if ($userLevel > 0) {
74
            return true;
75
        }
76
        return false;
77
    }
78
}