Completed
Push — dev-master ( 2b6108...c85948 )
by Vijay
05:50
created

Base::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace FFCMS\Controllers;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Models};
7
8
/**
9
 * Base Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright (c) Copyright 2016 Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
abstract class Base
16
{
17
    use Traits\UrlHelper,
18
        Traits\Notification,
19
        Traits\SecurityController,
20
        Traits\Validation;
21
22
    /**
23
     * Add default scripts for displaying templates - override in controller to add more
24
     *
25
     * @return void
26
     * @see app/config/default.ini
27
     */
28
    protected function addScripts()
29
    {
30
        // no scripts to add, override me and set css and js
31
        $this->setScripts();
32
    }
33
34
35
    /**
36
     * Set the scripts to load in the templates
37
     *
38
     * @param array $css list of css to load as defined in config.ini [css]
39
     * @param array $js list of js to load as defined in config.ini [js]
40
     * @return void
41
     * @see app/config/default.ini
42
     */
43
    protected function setScripts(array $css = [], array $js = [])
44
    {
45
        $f3 = \Base::instance();
46
        $env = ('production' == $f3->get('app.env')) ? 'production' : 'dev';
47
        $scripts = [];
48
        $scripts['css']['autoload'] = $css;
49
        $scripts['js']['autoload'] = $js;
50
        foreach (['js', 'css'] as $type) {
51
            $scripts[$type]['autoload'] = array_merge($scripts[$type]['autoload'], $f3->get($type . '.autoload'));
52
            $scripts[$type]['scripts'] = $f3->get($type . '.' . $env);
53
            $scripts[$type]['load'] = array_intersect_key($scripts[$type]['scripts'], array_flip($scripts[$type]['autoload']));
54
            $f3->set($type . '.load', $scripts[$type]['load']);
55
        }
56
    }
57
58
    /**
59
     * Logout if not admin
60
     *
61
     * @param \Base $f3
62
     * @param array $params
63
     * @return void
64
     */
65
     public function beforeRoute(\Base $f3, array $params)
66
    {
67
        // get logged in user info
68
        $usersModel = Models\Users::instance();
69
        $usersMapper = $usersModel->getMapper();
70
71
        // get the logged in user
72
        $uuid = $f3->get('uuid');
73
        if (empty($uuid)) {
74
            return;
75
        }
76
        $usersMapper->load(['uuid = ?', $uuid]);
77
78
        // invalid user, go to logout
79
        if (empty($usersMapper->uuid)) {
80
            $f3->clear('uuid');
81
            $f3->clear('SESSION');
82
            $f3->reroute('@logout');
83
            return;
84
        }
85
86
        // fetch the user scopes
87
        $user = $usersMapper->cast();
88
        $user['scopes'] = empty($user['scopes']) ? [] : preg_split("/[\s,]+/", $user['scopes']);
89
        $user['apiEnabled'] = (int) in_array('api', $user['scopes']);
90
        $f3->set('userScopes', $user['scopes']);
91
        $f3->set('isAdmin', in_array('admin', $user['scopes']));
92
        $f3->set('isRoot', in_array('root', $user['scopes']));
93
94
        // fetch addtional information for the user
95
        $usersData = $usersModel->getUserDetails($usersMapper->uuid, [
96
            'access_token',
97
            'refresh_token',
98
            'email_confirmed',
99
        ]);
100
101
        $f3->set('user', array_merge($user, $usersData));
102
        $f3->set('usersMapper', $usersMapper);
103
104
        // add default css and js
105
        $this->addScripts();
106
    }
107
108
}
109