Completed
Push — master ( a505a0...5d1dca )
by Fèvre
20s
created

src/Controller/AppController.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Controller;
3
4
use App\Event\Badges;
5
use App\I18n\Language;
6
use Cake\Controller\Controller;
7
use Cake\Event\Event;
8
use Cake\I18n\Time;
9
10
class AppController extends Controller
11
{
12
13
    /**
14
     * Initialization hook method.
15
     *
16
     * @return void
17
     */
18
    public function initialize()
19
    {
20
        parent::initialize();
21
22
        //Components.
23
        $this->loadComponent('Flash');
24
        $this->loadComponent('Cookie');
25
        $this->loadComponent('Acl.Acl');
26
        $this->loadComponent('SessionsActivity');
27
        $this->loadComponent('Auth', [
28
            'className' => 'AclAuth',
29
            'allowedActionsForBanned' => [
30
                'Pages' => [
31
                    'home'
32
                ]
33
            ],
34
            'authenticate' => [
35
                'Form',
36
                'Xety/Cake3CookieAuth.Cookie'
37
            ],
38
            'flash' => [
39
                'element' => 'error',
40
                'key' => 'flash',
41
                'params' => [
42
                    'class' => 'error'
43
                ]
44
            ],
45
            'authorize' => [
46
                'Acl.Actions' => [
47
                    'actionPath' => 'app/'
48
                ]
49
            ],
50
            'loginAction' => [
51
                'controller' => 'users',
52
                'action' => 'login',
53
                'prefix' => false
54
            ],
55
            'unauthorizedRedirect' => [
56
                'controller' => 'pages',
57
                'action' => 'home',
58
                'prefix' => false
59
            ],
60
            'loginRedirect' => [
61
                'controller' => 'pages',
62
                'action' => 'home'
63
            ],
64
            'logoutRedirect' => [
65
                'controller' => 'pages',
66
                'action' => 'home'
67
            ]
68
        ]);
69
70
        if (env('HTTPS')) {
71
            $this->loadComponent('Csrf', [
72
                'secure' => true
73
            ]);
74
        } else {
75
            $this->loadComponent('Csrf');
76
        }
77
    }
78
79
    /**
80
     * beforeFilter handle.
81
     *
82
     * @param Event $event The beforeFilter event that was fired.
83
     *
84
     * @return void
85
     */
86
    public function beforeFilter(Event $event)
87
    {
88
        $this->loadModel('Settings');
89
        $this->Settings->setSettings();
90
91
        $this->Auth->config('authError', __('You need to be logged in or you are not authorized to access that location !'));
92
93
        //Define the language.
94
        $language = new Language($this);
95
        $language->setLanguage();
96
97
        //Set trustProxy or get the original visitor IP.
98
        $this->request->trustProxy = true;
99
100
        //Automatically Login.
101
        if (!$this->Auth->user() && $this->Cookie->read('CookieAuth')) {
102
            $this->loadModel('Users');
103
104
            $user = $this->Auth->identify();
105
            if ($user && $user['is_deleted'] == false) {
106
                $this->Auth->setUser($user);
0 ignored issues
show
It seems like $user defined by $this->Auth->identify() on line 104 can also be of type boolean; however, Cake\Controller\Component\AuthComponent::setUser() does only seem to accept array|object<ArrayAccess>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
108
                $user = $this->Users->newEntity($user, ['accessibleFields' => ['id' => true]]);
109
                $user->isNew(false);
110
111
                $user->last_login = new Time();
112
                $user->last_login_ip = $this->request->clientIp();
113
114
                $this->Users->save($user);
115
116
                //Event.
117
                $this->eventManager()->attach(new Badges($this));
118
119
                $user = new Event('Model.Users.register', $this, [
120
                    'user' => $user
121
                ]);
122
                $this->eventManager()->dispatch($user);
123
            } else {
124
                $this->Cookie->delete('CookieAuth');
125
            }
126
        }
127
128
        if (isset($this->request->params['prefix'])) {
129
            $prefix = explode('/', $this->request->params['prefix'])[0];
130
131
            switch ($prefix) {
132
                case 'admin':
133
                    $this->viewBuilder()->layout('admin');
134
                    break;
135
            }
136
        }
137
138
        $allowCookies = $this->Cookie->check('allowCookies');
139
        $this->set(compact('allowCookies'));
140
141
        //JavaScript Notifications.
142
        if ($this->request->session()->read('Notification') && !empty($this->request->session()->read('Notification'))) {
143
            $notification = $this->request->session()->read('Notification');
144
            $this->request->session()->delete('Notification');
145
146
            $this->set(compact('notification'));
147
        }
148
    }
149
}
150