Completed
Push — master ( 5dab40...ca534d )
by Maxime
120:18 queued 114:35
created

UserUtils   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 86.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 24
c 2
b 0
f 0
lcom 0
cbo 0
dl 0
loc 118
ccs 39
cts 45
cp 0.8667
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A setArea() 0 4 1
A getArea() 0 4 1
A forgotArea() 0 4 1
A hasAccess() 0 8 3
A forgotDisplayAllStatus() 0 4 1
A isBackendRole() 0 8 3
A frontendInitialRole() 0 6 1
A isFrontendRole() 0 8 2
A getEmail() 0 4 1
A getDisplayName() 0 4 1
A isNotSuperAdmin() 0 4 1
A hasDisplayAllStatus() 0 4 1
A setDisplayAllStatus() 0 4 1
A setIsLoggedIn() 0 10 2
A forgotIsLoggedIn() 0 10 2
A securityCheckLockEnabled() 0 4 1
1
<?php namespace Distilleries\Expendable\Helpers;
2
3
use \Auth;
4
use \Session;
5
6
class UserUtils
7
{
8
9
10 300
    public static function isBackendRole()
11
    {
12 300
        $user = Auth::user();
13
14 300
        if (empty($user)) return false;
15
16 236
        return Auth::user()->role->initials == '@sa' || Auth::user()->role->initials == '@a';
17
    }
18
19
    public static function frontendInitialRole()
20
    {
21
        return [
22
            '@g', //guest
23
        ];
24
    }
25
26
    public static function isFrontendRole()
27
    {
28
        $user = Auth::user();
29
30
        if (empty($user)) return false;
31
32
        return in_array(Auth::user()->role->initials, self::frontendInitialRole());
33
    }
34
35
36
37 4
    public static function get()
38
    {
39 4
        return Auth::user();
40
    }
41
42 76
    public static function getEmail()
43
    {
44 76
        return Auth::user()->getEmailForPasswordReset();
45
    }
46
47 76
    public static function getDisplayName()
48
    {
49 76
        return Auth::user()->getEmailForPasswordReset();
50
    }
51
52 12
    public static function isNotSuperAdmin()
53
    {
54 12
        return Auth::user()->role->initials != '@sa';
55
    }
56
57
58 24
    public static function setArea($area)
59
    {
60 24
        Session::put('permissions', $area);
61
    }
62
63 12
    public static function getArea()
64
    {
65 12
        return Session::get('permissions');
66
    }
67
68 16
    public static function forgotArea()
69
    {
70 16
        Session::forget('permissions');
71
    }
72
73 8
    public static function hasAccess($key)
74
    {
75 8
        $key  = ltrim($key, "\\");
76 8
        $area = self::getArea();
77 8
        $area = (is_array($area) && !empty($area)) ? $area : [];
78
79 8
        return in_array($key, $area);
80
    }
81
82 300
    public static function hasDisplayAllStatus()
83
    {
84 300
        return Session::get('display_all_status', false);
85
    }
86
87 108
    public static function forgotDisplayAllStatus()
88
    {
89 108
        Session::forget('display_all_status');
90
    }
91
92 220
    public static function setDisplayAllStatus()
93
    {
94 220
        Session::put('display_all_status', true);
95
    }
96
97 16
    public static function setIsLoggedIn()
0 ignored issues
show
Coding Style introduced by
setIsLoggedIn uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
98
    {
99 16
        if (session_id() == '')
100
        {
101 4
            @session_start();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
102
103
        }
104
105 16
        $_SESSION['isLoggedIn'] = true;
106
    }
107
108 16
    public static function forgotIsLoggedIn()
0 ignored issues
show
Coding Style introduced by
forgotIsLoggedIn uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
109
    {
110 16
        $session_id = session_id();
111
112 16
        if (!empty($session_id))
113
        {
114 16
            unset($_SESSION['isLoggedIn']);
115
        }
116
117
    }
118
119 8
    public static function securityCheckLockEnabled()
120
    {
121 8
        return config('expendable.auth.security_enabled') === true;
122
    }
123
}