'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; } }