Completed
Push — master ( 7c757d...667317 )
by William
02:47
created

AppController::_checkAccess()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

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