-
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Admin.php
executable file
·210 lines (151 loc) · 5.4 KB
/
Admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* This file is part of the Cockpit project.
*
* (c) Artur Heinze - 🅰🅶🅴🅽🆃🅴🅹🅾, http://agentejo.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Cockpit\Helper;
use ArrayObject;
/**
* Admin Helper class.
*/
class Admin extends \Lime\Helper {
public $data;
public $options;
public $user;
public function initialize(){
$this->data = new \ContainerArray();
$this->options = [];
$this->user = $this->app->module('cockpit')->getUser();
$this->user['data'] = new \ContainerArray(isset($this->user['data']) && is_array($this->user['data']) ? $this->user['data']:[]);
}
public function init() {
// extend lexy parser
$this->app->renderer->extend(function($content){
return preg_replace('/(\s*)@hasaccess\?\((.+?)\)/', '$1<?php if ($app->module("cockpit")->hasaccess($2)) { ?>', $content);
});
$languages = [];
$langDefaultLabel = 'Default';
foreach ($this->app->retrieve('config/languages', []) as $key => $val) {
if (is_numeric($key)) $key = $val;
if ($key == 'default') {
$langDefaultLabel = $val;
} else {
$languages[] = ['code'=>$key, 'label'=>$val];
}
}
$this->data->extend([
'cockpit' => json_decode($this->app->helper('fs')->read('#root:package.json'), true),
/**
* Admin assets
*/
'assets' => new ArrayObject(array_merge($this->app['app.assets.base'], [
// uikit components
'assets:lib/uikit/js/components/autocomplete.min.js',
'assets:lib/uikit/js/components/tooltip.min.js',
// app related
'assets:app/js/bootstrap.js'
], $this->app->retrieve('config/app.assets.backend', []))),
/**
* web components
*/
'components' => new ArrayObject([]),
/**
* admin menus
*/
'menu.modules' => new ArrayObject([]),
/**
* extract to App.$data
*/
'extract' => [
'user' => $this->user,
'locale' => $this->app->helper('i18n')->locale,
'site_url' => $this->app->pathToUrl('site:'),
'languages' => $languages,
'languageDefaultLabel' => $langDefaultLabel,
'groups' => $this->app->helper('acl')->getGroups(),
'acl' => [
'finder' => $this->app->module('cockpit')->hasaccess('cockpit', 'finder')
]
]
]);
}
public function addMenuItem($menu, $data) {
$this->data["menu.{$menu}"]->append(array_merge([
'label' => '',
'icon' => 'cube',
'route' => '/',
'active' => false
], $data));
return $this;
}
public function addAssets($assets) {
foreach ((array)$assets as $asset) {
if (preg_match('/\.(js|css)$/i', $asset)) {
$this->data['assets']->append($asset);
} elseif(preg_match('/\.tag$/i', $asset)) {
$this->data['components']->append($asset);
}
}
return $this;
}
public function extractVar($key, $value) {
$this->data["extract/{$key}"] = $value;
}
public function getOption($key, $default = null) {
if (!isset($this->options[$key])) {
$this->options[$key] = $this->app->storage->getKey("cockpit/options", $key, $default);
}
return $this->options[$key];
}
public function setOption($key, $value) {
$this->options[$key] = $value;
$this->app->storage->setKey("cockpit/options", $key, $value);
return $value;
}
public function getUserOption($key, $default = null) {
return $this->user['data']->get($key, $default);
}
public function setUserOption($key, $value) {
$this->user['data']->set($key, $value);
return $this->app->module('cockpit')->updateUserOption($key, $value);
}
public function isResourceLocked($resourceId, $ttl = 300) {
$key = "locked:{$resourceId}";
$meta = $this->app->memory->get($key, false);
if ($meta && ($meta['time'] + $ttl) < time()) {
$this->app->memory->del($key);
$meta = false;
}
if ($meta) {
return $meta;
}
return false;
}
public function lockResourceId($resourceId, $user = null) {
$key = "locked:{$resourceId}";
$user = $user ?? $this->app->module('cockpit')->getUser();
$meta = [
'user' => ['_id' => $user['_id'], 'name' => $user['name'], 'user' => $user['user']],
'time' => time()
];
$this->app->memory->set($key, $meta);
return true;
}
public function unlockResourceId($resourceId) {
$key = "locked:{$resourceId}";
$this->app->memory->del($key);
return true;
}
public function denyRequest() {
if ($this->app->module('cockpit')->getUser()) {
$this->app->response->status = 401;
} else {
$this->app->response->status = 404;
}
return '';
}
}