forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.php
More file actions
395 lines (355 loc) · 10.5 KB
/
Script.php
File metadata and controls
395 lines (355 loc) · 10.5 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
namespace ProcessMaker\Models;
use Illuminate\Validation\Rule;
use ProcessMaker\Contracts\ScriptInterface;
use ProcessMaker\Exception\ConfigurationException;
use ProcessMaker\Exception\ScriptLanguageNotSupported;
use ProcessMaker\Models\ScriptCategory;
use ProcessMaker\Models\User;
use ProcessMaker\ScriptRunners\ScriptRunner;
use ProcessMaker\Traits\Exportable;
use ProcessMaker\Traits\ExtendedPMQL;
use ProcessMaker\Traits\HasCategories;
use ProcessMaker\Traits\HasVersioning;
use ProcessMaker\Traits\HideSystemResources;
use ProcessMaker\Traits\ProjectAssetTrait;
use ProcessMaker\Traits\SerializeToIso8601;
use ProcessMaker\Validation\CategoryRule;
/**
* Represents an Eloquent model of a Script
*
*
* @property int id
* @property string key
* @property string title
* @property text description
* @property string language
* @property text code
* @property int timeout
*
* @OA\Schema(
* schema="scriptsEditable",
* @OA\Property(property="title", type="string"),
* @OA\Property(property="description", type="string"),
* @OA\Property(property="language", type="string"),
* @OA\Property(property="code", type="string"),
* @OA\Property(property="timeout", type="integer"),
* @OA\Property(property="run_as_user_id", type="integer"),
* @OA\Property(property="key", type="string"),
* @OA\Property(property="script_category_id", type="integer"),
* ),
* @OA\Schema(
* schema="scripts",
* allOf={
* @OA\Schema(ref="#/components/schemas/scriptsEditable"),
* @OA\Schema(
* @OA\Property(property="id", type="integer", format="id"),
* @OA\Property(property="created_at", type="string", format="date-time"),
* @OA\Property(property="updated_at", type="string", format="date-time"),
* )
* },
* ),
*
* @OA\Schema(
* schema="scriptsPreview",
* @OA\Property(property="status", type="string"),
* @OA\Property(property="key", type="string"),
* )
*/
class Script extends ProcessMakerModel implements ScriptInterface
{
use SerializeToIso8601;
use HideSystemResources;
use HasCategories;
use HasVersioning;
use Exportable;
use ProjectAssetTrait;
use ExtendedPMQL;
const categoryClass = ScriptCategory::class;
protected $connection = 'processmaker';
protected $guarded = [
'id',
'created_at',
'updated_at',
];
protected $casts = [
'timeout' => 'integer',
'retry_attempts' => 'integer',
'retry_wait_time' => 'integer',
];
protected $appends = [
'projects',
];
/**
* Override the default boot method to allow access to lifecycle hooks
*
* @return null
*/
public static function boot()
{
parent::boot();
self::saving(function ($script) {
// If a script executor has not been set, choose one
// automatically based on the scripts set language
$script->setDefaultExecutor();
});
}
/**
* Validation rules
*
* @param $existing
*
* @return array
*/
public static function rules($existing = null)
{
$unique = Rule::unique('scripts')->ignore($existing);
return [
'key' => 'unique:scripts,key',
'title' => ['required', 'string', $unique, 'alpha_spaces'],
'language' => [
'required_without:script_executor_id',
Rule::in(static::scriptFormatValues()),
],
'script_executor_id' => 'required_without:language|exists:script_executors,id',
'description' => 'required',
'run_as_user_id' => 'required',
'timeout' => 'integer|min:0|max:65535',
'script_category_id' => [new CategoryRule($existing)],
];
}
/**
* Executes a script given a configuration and data input.
*
* @param array $data
* @param array $config
*/
public function runScript(array $data, array $config, $tokenId = '', $timeout = null)
{
if (!$timeout) {
$timeout = $this->timeout;
}
if (!$this->scriptExecutor) {
throw new ScriptLanguageNotSupported($this->language);
}
$runner = new ScriptRunner($this->scriptExecutor);
$runner->setTokenId($tokenId);
$user = User::find($this->run_as_user_id);
if (!$user) {
throw new ConfigurationException('A user is required to run scripts');
}
return $runner->run($this->code, $data, $config, $timeout, $user);
}
/**
* Get a configuration array of all supported script formats.
*
* @return array
*/
public static function scriptFormats()
{
return config('script-runners');
}
/**
* Get the configuration for a specific script format.
*
* @param string $format
*
* @return array
*/
public static function scriptFormat($format)
{
$formats = static::scriptFormats();
if (array_key_exists($format, $formats)) {
return $formats[$format];
} else {
return null;
}
}
/**
* Get a basic array of supported script formats.
*
* @return array
*/
public static function scriptFormatValues()
{
$values = [];
$formats = static::scriptFormats();
foreach ($formats as $key => $format) {
$values[] = $key;
}
return $values;
}
/**
* Get a key/value pair array of supported script formats.
*
* @return array
*/
public static function scriptFormatList()
{
$list = [];
$formats = static::scriptFormats();
foreach ($formats as $key => $format) {
$list[$key] = $format['name'];
}
return $list;
}
/**
* Get the language from a script format (MIME type) string.
*
* @param string $mimeType
*
* @return string
*/
public static function scriptFormat2Language($mimeType)
{
$formats = static::scriptFormats();
foreach ($formats as $key => $format) {
if ($mimeType == $format['mime_type']) {
return $key;
}
}
return null;
}
/**
* Get the language name for this script.
*
* @return string
*/
public function getLanguageNameAttribute()
{
if ($format = static::scriptFormat($this->language)) {
return $format['name'];
} else {
return $this->language;
}
}
/**
* Get the associated versions
*/
public function versions()
{
return $this->hasMany(ScriptVersion::class);
}
/**
* Get the associated run_as_user
*/
public function runAsUser()
{
return $this->belongsTo(User::class, 'run_as_user_id');
}
/**
* Get the associated projects
*/
public function projects()
{
return $this->belongsToMany('ProcessMaker\Package\Projects\Models\Project',
'project_assets',
'asset_id',
'project_id',
'id',
'id'
)->wherePivot('asset_type', static::class);
}
// Define the relationship with the ProjectAsset model
public function projectAssets()
{
return $this->belongsToMany('ProcessMaker\Package\Projects\Models\ProjectAsset',
'project_assets', 'asset_id', 'project_id')
->withPivot('asset_type')
->wherePivot('asset_type', static::class)->withTimestamps();
}
/**
* Return the a user for service tasks
*
* @return ProcessMaker\Models\User
*/
public static function defaultRunAsUser()
{
// return the default admin user
return User::where('is_administrator', true)->firstOrFail();
}
/**
* Get the associated category
*/
public function category()
{
return $this->belongsTo(ScriptCategory::class, 'script_category_id');
}
/**
* Set multiple|single categories to the script
*
* @param string $value
*/
public function setScriptCategoryIdAttribute($value)
{
return $this->setMultipleCategories($value, 'script_category_id');
}
/**
* Get multiple|single categories of the script
*
* @param string $value
*/
public function getScriptCategoryIdAttribute($value)
{
return implode(',', $this->categories()->pluck('category_id')->toArray()) ?: $value;
}
/**
* Get the associated executor
*/
public function scriptExecutor()
{
return $this->belongsTo(ScriptExecutor::class, 'script_executor_id');
}
/**
* Save the default executor when only a language is specified
*/
private function setDefaultExecutor()
{
if (empty($this->script_executor_id)) {
$this->script_executor_id = ScriptExecutor::initialExecutor($this->language)->id;
}
if (empty($this->language)) {
$this->language = $this->scriptExecutor->language;
}
}
/**
* PMQL value alias for fulltext field
*
* @param string $value
*
* @return callable
*/
public function valueAliasFullText($value, $expression)
{
return function ($query) use ($value) {
$this->scopeFilter($query, $value);
};
}
/**
* Filter settings with a string
*
* @param $query
*
* @param $filter string
*/
public function scopeFilter($query, $filterStr)
{
$filter = '%' . mb_strtolower($filterStr) . '%';
$query->where(function ($query) use ($filter, $filterStr) {
$query->where('scripts.title', 'like', $filter)
->orWhere('scripts.description', 'like', $filter)
->orWhere('scripts.status', '=', $filterStr)
->orWhereIn('scripts.id', function ($qry) use ($filter) {
$qry->select('assignable_id')
->from('category_assignments')
->leftJoin('script_categories', function ($join) {
$join->on('script_categories.id', '=', 'category_assignments.category_id');
$join->where('category_assignments.category_type', '=', ScriptCategory::class);
$join->where('category_assignments.assignable_type', '=', self::class);
})
->where('script_categories.name', 'like', $filter);
});
});
return $query;
}
}