Completed
Push — dev-master ( 9bf5e2...86aae1 )
by Vijay
05:35
created

Base::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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