Completed
Push — master ( 9c742a...1858b1 )
by Fèvre
14s
created

src/Controller/Component/MaintenanceComponent.php (2 issues)

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\Component;
3
4
use Cake\Controller\Component;
5
use Cake\Core\Configure;
6
use Cake\ORM\TableRegistry;
7
8
class MaintenanceComponent extends Component
9
{
10
    /**
11
     * Controller object
12
     *
13
     * @var \Cake\Controller\Controller
14
     */
15
    protected $_controller;
16
17
    /**
18
     * The allowed routes.
19
     *
20
     * @var array
21
     */
22
    protected $_allowedRoutes = [
23
        'Pages.maintenance',
24
        'Users.login',
25
        'Users.logout'
26
    ];
27
28
    /**
29
     * Initialize properties.
30
     *
31
     * @param array $config The config data.
32
     *
33
     * @return void
34
     */
35
    public function initialize(array $config)
36
    {
37
        $this->_controller = $this->_registry->getController();
38
    }
39
40
    /**
41
     * Handle the maintenance mode.
42
     *
43
     * @return false|void
44
     */
45
    public function handle()
46
    {
47
        if (Configure::read('Site.maintenance') !== true) {
48
            return false;
49
        }
50
51
        $controller = $this->request->params['controller'];
52
        $action = $this->request->params['action'];
53
54
        if ($this->_controller->Auth->user()) {
55
            $this->Users = TableRegistry::get('Users');
56
            $user = $this->Users
57
                ->find()
58
                ->contain([
59
                    'Groups' => function ($q) {
60
                        return $q->select(['id', 'is_staff']);
61
                    }
62
                ])
63
                ->where([
64
                    'Users.id' => $this->_controller->Auth->user('id')
65
                ])
66
                ->first();
67
68
            if (!is_null($user) && $user->group->is_staff == true) {
69
                //To prevent multiple flash messages.
70
                $this->_controller->Flash->config(['clear' => true]);
71
                $this->_controller->Flash->error(__("Hello {0}, The website is under maintenance, only you and the staff groups have the access !", h($user->full_name)));
72 View Code Duplication
            } else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
                if (!in_array($controller . '.' . $action, $this->_allowedRoutes)) {
74
                    $this->_controller->redirect([
75
                        'controller' => 'pages',
76
                        'action' => 'maintenance',
77
                        'prefix' => false
78
                    ]);
79
                }
80
            }
81 View Code Duplication
        } else {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
            if (!in_array($controller . '.' . $action, $this->_allowedRoutes)) {
83
                $this->_controller->redirect([
84
                    'controller' => 'pages',
85
                    'action' => 'maintenance',
86
                    'prefix' => false
87
                ]);
88
            }
89
        }
90
    }
91
}
92