Completed
Push — master ( 714c30...9cdd17 )
by Maxime
04:22
created

UserUtils   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

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 45
cts 45
cp 1
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A isBackendRole() 0 8 3
A frontendInitialRole() 0 6 1
A isFrontendRole() 0 8 2
A get() 0 4 1
A getEmail() 0 4 1
A getDisplayName() 0 4 1
A isNotSuperAdmin() 0 4 1
A setArea() 0 4 1
A getArea() 0 4 1
A forgotArea() 0 4 1
A hasAccess() 0 8 3
A hasDisplayAllStatus() 0 4 1
A forgotDisplayAllStatus() 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 4
10
    public static function isBackendRole()
11 4
    {
12
        $user = Auth::user();
13
14 88
        if (empty($user)) return false;
15
16 88
        return Auth::user()->role->initials == '@sa' || Auth::user()->role->initials == '@a';
17
    }
18
19 88
    public static function frontendInitialRole()
20
    {
21 88
        return [
22
            '@g', //guest
23
        ];
24 12
    }
25
26 12
    public static function isFrontendRole()
27
    {
28
        $user = Auth::user();
29
30 24
        if (empty($user)) return false;
31
32 24
        return in_array(Auth::user()->role->initials, self::frontendInitialRole());
33 18
    }
34
35 12
36
37 12
    public static function get()
38
    {
39
        return Auth::user();
40 16
    }
41
42 16
    public static function getEmail()
43 12
    {
44
        return Auth::user()->getEmailForPasswordReset();
45 8
    }
46
47 8
    public static function getDisplayName()
48 8
    {
49 8
        return Auth::user()->getEmailForPasswordReset();
50
    }
51 8
52
    public static function isNotSuperAdmin()
53
    {
54 344
        return Auth::user()->role->initials != '@sa';
55
    }
56 344
57
58
    public static function setArea($area)
59 16
    {
60
        Session::put('permissions', $area);
61 16
    }
62 12
63
    public static function getArea()
64 16
    {
65
        return Session::get('permissions');
66 16
    }
67 12
68
    public static function forgotArea()
69 16
    {
70
        Session::forget('permissions');
71 16
    }
72 12
73 4
    public static function hasAccess($key)
74
    {
75 3
        $key  = ltrim($key, "\\");
76
        $area = self::getArea();
77 16
        $area = (is_array($area) && !empty($area)) ? $area : [];
78 12
79
        return in_array($key, $area);
80 16
    }
81
82 16
    public static function hasDisplayAllStatus()
83
    {
84 16
        return Session::get('display_all_status', false);
85 12
    }
86 16
87 12
    public static function forgotDisplayAllStatus()
88
    {
89 12
        Session::forget('display_all_status');
90
    }
91 8
92
    public static function setDisplayAllStatus()
93 8
    {
94
        Session::put('display_all_status', true);
95
    }
96
97
    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
        if (session_id() == '')
100
        {
101
            @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
        $_SESSION['isLoggedIn'] = true;
106
    }
107
108
    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
        $session_id = session_id();
111
112
        if (!empty($session_id))
113
        {
114
            unset($_SESSION['isLoggedIn']);
115
        }
116
117
    }
118
119
    public static function securityCheckLockEnabled()
120
    {
121
        return config('expendable.auth.security_enabled') === true;
122
    }
123
}