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
267 lines (238 loc) · 6.43 KB
/
Script.php
File metadata and controls
267 lines (238 loc) · 6.43 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
<?php
namespace ProcessMaker\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;
use ProcessMaker\Traits\SerializeToIso8601;
use ProcessMaker\GenerateAccessToken;
use ProcessMaker\Models\User;
use ProcessMaker\ScriptRunners\ScriptRunner;
use ProcessMaker\Models\ScriptCategory;
use ProcessMaker\Traits\HasCategories;
use ProcessMaker\Traits\HasVersioning;
use ProcessMaker\Traits\HideSystemResources;
use ProcessMaker\Validation\CategoryRule;
/**
* Represents an Eloquent model of a Script
*
* @package ProcessMaker\Model
*
* @property integer id
* @property string key
* @property string title
* @property text description
* @property string language
* @property text code
* @property integer 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\Schema(
* schema="scripts",
* allOf={@OA\Schema(ref="#/components/schemas/scriptsEditable")},
* @OA\Property(property="id", type="string", 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 Model
{
use SerializeToIso8601;
use HideSystemResources;
use HasCategories;
use HasVersioning;
const categoryClass = ScriptCategory::class;
protected $connection = 'processmaker';
protected $guarded = [
'id',
'created_at',
'updated_at',
];
protected $casts = [
'timeout' => 'integer',
];
/**
* 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',
Rule::in(static::scriptFormatValues())
],
'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)
{
$runner = new ScriptRunner($this->language);
$user = User::find($this->run_as_user_id);
if (!$user) {
throw new \RuntimeException("A user is required to run scripts");
}
return $runner->run($this->code, $data, $config, $this->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');
}
/**
* 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;
}
}