AppController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 114
dl 0
loc 208
ccs 56
cts 70
cp 0.8
rs 10
c 1
b 0
f 0
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A checkReadonlyAccess() 0 35 5
A initialize() 0 6 1
B beforeFilter() 0 48 6
A checkAccess() 0 27 4
1
<?php
2
3
/**
4
 * Application level Controller.
5
 *
6
 * This file is application-wide controller file. You can put all
7
 * application-wide controller-related methods here.
8
 *
9
 * phpMyAdmin Error reporting server
10
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
11
 *
12
 * Licensed under The MIT License
13
 * For full copyright and license information, please see the LICENSE.txt
14
 * Redistributions of files must retain the above copyright notice.
15
 *
16
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
17
 * @license   https://opensource.org/licenses/mit-license.php MIT License
18
 *
19
 * @see      https://www.phpmyadmin.net/
20
 */
21
22
namespace App\Controller;
23
24
use App\Model\Table\DevelopersTable;
25
use App\Model\Table\NotificationsTable;
26
use Cake\Controller\Controller;
27
use Cake\Event\EventInterface;
28
use Cake\Http\Response;
29
use Cake\ORM\TableRegistry;
30
use Cake\Routing\Router;
31
32
use function in_array;
33
34
/**
35
 * Application Controller.
36
 *
37
 * Add your application-wide methods in the class below, your controllers
38
 * will inherit them.
39
 *
40
 * @see    http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
41
 *
42
 * @property NotificationsTable $Notifications
43
 * @property DevelopersTable $Developers
44
 */
45
class AppController extends Controller
46
{
47
    /** @var array */
48
    public $whitelist = [
49
        'Developers',
50
        'Pages',
51
        'Incidents' => ['create'],
52
        'Events',
53
    ];
54
55
    /** @var array */
56
    public $readonly_whitelist = [
57
        'Developers',
58
        'Pages',
59
        'Reports' => [
60
            'index',
61
            'view',
62
            'data_tables',
63
        ],
64
        'Incidents' => ['view'],
65
    ];
66
67
    /** @var string[] */
68
    public $css_files = [
69
        'jquery.dataTables',
70
        'jquery.dataTables_themeroller',
71
        'bootstrap.min',
72
        'bootstrap-responsive.min',
73
        'shCore',
74
        'shThemeDefault',
75
        'custom',
76
    ];
77
78
    /** @var string[] */
79
    public $js_files = [
80
        'jquery',
81
        'jquery.dataTables.min',
82
        'bootstrap',
83
        'shCore',
84
        'shBrushXml',
85
        'shBrushJScript',
86
        'shBrushPhp',
87
        'raphael-min',
88
        'g.raphael-min',
89
        'g.pie-min',
90
        'g.line-min',
91
        'g.bar-min',
92
        'g.dot-min',
93
        'jquery.jqplot.min',
94
        'jqplot.barRenderer.min',
95
        'jqplot.highlighter.min',
96
        'jqplot.dateAxisRenderer.min',
97
        'jqplot.categoryAxisRenderer.min',
98
        'jqplot.pointLabels.min',
99
        'jqplot.canvasTextRenderer.min',
100
        'jqplot.canvasAxisTickRenderer.min',
101
        'jqplot.cursor.min',
102
        'pie',
103
        'custom',
104
    ];
105
106
    /**
107
     * Initialization hook method.
108
     *
109
     * Use this method to add common initialization code like loading components.
110
     *
111
     * @return void Nothing
112
     */
113 154
    public function initialize(): void
114
    {
115 154
        parent::initialize();
116 154
        $this->loadComponent('Flash');
117 154
        $this->loadModel('Notifications');
118 154
        $this->loadModel('Developers');
119
        /*  $this->loadComponent(
120
                'Auth', [
121
                    'loginAction' => [
122
                        'controller' => 'Developer',
123
                        'action' => 'login'
124
                    ],
125
                    'authError' => 'Did you really think you are allowed to see that?',
126
                    'authenticate' => [
127
                        'Form' => [
128
                            'fields' => ['username' => 'email']
129
                        ]
130
                    ]
131
                ]
132
            );
133
        */
134 154
    }
135
136
    /**
137
     * @return Response|void Returns a Response if a redirect is needed
138
     */
139 147
    public function beforeFilter(EventInterface $event)
140
    {
141 147
        $controllerName = $this->request->getParam('controller');
142 147
        $this->set('current_controller', $controllerName);
143 147
        $notif_count = 0;
144
145 147
        $devId = $this->request->getSession()->read('Developer.id');
146 147
        if ($devId) {
147 133
            $response = $this->checkReadonlyAccess();
148 133
            if ($response !== null) {
149
                // This is a security check
150
                // The response can be printed if you remove this line
151 7
                return $response;
152
            }
153
154 126
            $current_developer = TableRegistry::getTableLocator()->get('Developers')->
155 126
                    findById($devId)->all()->first();
156
157 126
            $notif_count = TableRegistry::getTableLocator()->get('Notifications')->find(
158 126
                'all',
159
                [
160 126
                    'conditions' => ['developer_id' => (int) isset($current_developer) ? $current_developer['id'] : null],
161
                ]
162 126
            )->count();
163 126
            $this->set('current_developer', $current_developer);
164 126
            $this->set('developer_signed_in', true);
165
166 126
            $read_only = false;
167 126
            if ($this->request->getSession()->read('read_only')) {
168
                $read_only = true;
169
            }
170
171 126
            $this->set('read_only', $read_only);
172
        } else {
173 14
            $this->set('developer_signed_in', false);
174 14
            $this->set('read_only', true);
175 14
            $response = $this->checkAccess();
176 14
            if ($response !== null) {
177
                // This is a security check
178
                // The response can be printed if you remove this line
179
                return $response;
180
            }
181
        }
182
183 140
        $this->set('notif_count', $notif_count);
184 140
        $this->set('js_files', $this->js_files);
185 140
        $this->set('css_files', $this->css_files);
186 140
        $this->set('baseURL', Router::url('/', true));
187 140
    }
188
189 14
    protected function checkAccess(): ?Response
190
    {
191 14
        $controllerName = $this->request->getParam('controller');
192 14
        $action = $this->request->getParam('action');
193
194 14
        if (in_array($controllerName, $this->whitelist)) {
195 14
            return null;
196
        }
197
198
        if (
199
            isset($this->whitelist[$controllerName])
200
            && in_array($action, $this->whitelist[$controllerName])
201
        ) {
202
            return null;
203
        }
204
205
        $flash_class = 'alert';
206
        $this->Flash->set(
207
            'You need to be signed in to do this',
208
            ['params' => ['class' => $flash_class]]
209
        );
210
211
        // save the return url
212
        $ret_url = Router::url($this->request->getRequestTarget(), true);
213
        $this->request->getSession()->write('last_page', $ret_url);
214
215
        return $this->redirect('/');
216
    }
217
218 133
    protected function checkReadonlyAccess(): ?Response
219
    {
220 133
        $controllerName = $this->request->getParam('controller');
221 133
        $action = $this->request->getParam('action');
222 133
        $read_only = $this->request->getSession()->read('read_only');
223
224
        // If developer has commit access on phpmyadmin/phpmyadmin
225 133
        if (! $read_only) {
226 126
            return null;
227
        }
228
229 7
        if (in_array($controllerName, $this->readonly_whitelist)) {
230
            return null;
231
        }
232
233
        if (
234 7
            isset($this->readonly_whitelist[$controllerName])
235 7
            && in_array($action, $this->readonly_whitelist[$controllerName])
236
        ) {
237
            return null;
238
        }
239
240 7
        $this->request->getSession()->destroy();
241 7
        $this->request->getSession()->write('last_page', '');
242
243 7
        $flash_class = 'alert';
244 7
        $this->Flash->set(
245
            'You need to have commit access on phpmyadmin/phpmyadmin '
246 7
            . 'repository on Github.com to do this',
247
            [
248 7
                'params' => ['class' => $flash_class],
249
            ]
250
        );
251
252 7
        return $this->redirect('/');
253
    }
254
}
255