Repository navigation
Expand file tree
/
Copy pathEnvironmentVariable.php
More file actions
219 lines (187 loc) · 6.1 KB
/
Copy pathEnvironmentVariable.php
File metadata and controls
219 lines (187 loc) · 6.1 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
<?php
namespace ProcessMaker\Models;
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use ProcessMaker\Enums\ExporterMap;
use ProcessMaker\Traits\Exportable;
/**
* @OA\Schema(
* schema="EnvironmentVariableEditable",
* @OA\Property(property="name", type="string"),
* @OA\Property(property="description", type="string"),
* @OA\Property(property="value", type="string"),
* @OA\Property(property="asset_type", type="string", nullable=true),
* @OA\Property(property="do_not_update", type="boolean"),
* ),
* @OA\Schema(
* schema="EnvironmentVariable",
* allOf={
* @OA\Schema(ref="#/components/schemas/EnvironmentVariableEditable"),
* @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"),
* ),
* },
* )
*/
class EnvironmentVariable extends ProcessMakerModel
{
use Exportable;
protected $connection = 'processmaker';
protected $fillable = [
'name',
'description',
'value',
'asset_type',
'do_not_update',
];
protected $casts = [
'do_not_update' => 'boolean',
];
protected $hidden = [
'value',
];
protected static function boot()
{
parent::boot();
static::saving(function (self $environmentVariable) {
$environmentVariable->normalizeAssetLink();
});
}
/**
* Store the encrypted version of the variable value here
*/
public function setValueAttribute($value)
{
$this->attributes['value'] = encrypt($value);
}
/**
* Fetch the plain text version of the value
*/
public function getValueAttribute()
{
try {
return decrypt($this->attributes['value']);
} catch (Exception $e) {
Log::error(
'Can not decrypt environment variable: ' .
($this->attributes['name'] ?? 'unknown') .
"\n" . $e->getMessage() .
"\n" . $e->getTraceAsString(),
['attributes' => $this->attributes]
);
return null;
}
}
public static function rules($existing = null)
{
$unique = Rule::unique('environment_variables')->ignore($existing);
$validVariableName = '/^[a-zA-Z][a-zA-Z_$0-9]*$/';
$allowedAssetTypes = self::allowedAssetTypes();
return [
'description' => 'required',
'value' => [
'nullable',
'required_with:asset_type',
],
'name' => ['required', "regex:{$validVariableName}", $unique],
'asset_type' => [
'nullable',
'string',
Rule::in($allowedAssetTypes),
],
'do_not_update' => 'boolean',
];
}
/**
* Validate that asset_type + value resolve to an existing asset of that type.
*
* @return Model|null The resolved asset when a link is present
*/
public static function validateAssetLinkConsistency(array $data): ?Model
{
$assetType = $data['asset_type'] ?? null;
$value = $data['value'] ?? null;
if (!$assetType) {
return null;
}
if ($value === null || $value === '') {
throw ValidationException::withMessages([
'value' => [trans('environmentVariables.validation.value.required_with_asset_type')],
]);
}
if (!class_exists($assetType)) {
throw ValidationException::withMessages([
'asset_type' => [__('The selected asset type is not available.')],
]);
}
if (!in_array($assetType, self::allowedAssetTypes(), true)) {
throw ValidationException::withMessages([
'asset_type' => [__('The selected asset type is not allowed.')],
]);
}
$asset = $assetType::find($value);
if (!$asset) {
throw ValidationException::withMessages([
'value' => [trans('environmentVariables.validation.value.not_found_for_type')],
]);
}
// Guard against class hierarchy mismatches (e.g. subclass stored under a parent type).
if (get_class($asset) !== $assetType) {
throw ValidationException::withMessages([
'asset_type' => [trans('environmentVariables.validation.asset_type.mismatch')],
]);
}
return $asset;
}
public static function messages()
{
return [
'name.regex' => trans('environmentVariables.validation.name.invalid_variable_name'),
'value.required_with' => trans('environmentVariables.validation.value.required_with_asset_type'),
];
}
/**
* Exportable model classes that may be linked to an environment variable.
*/
public static function allowedAssetTypes(): array
{
return collect(ExporterMap::TYPES)
->map(fn (array $type) => $type[0])
->filter(fn (string $class) => class_exists($class))
->values()
->all();
}
public function resolveLinkedAsset(): ?Model
{
if (!$this->hasLinkedAsset() || !class_exists($this->asset_type)) {
return null;
}
if ($this->value === null || $this->value === '') {
return null;
}
return $this->asset_type::find($this->value);
}
public function hasLinkedAsset(): bool
{
return !empty($this->asset_type);
}
protected function normalizeAssetLink(): void
{
if ($this->asset_type === '') {
$this->asset_type = null;
}
}
public static function getMetricsApiEndpoint()
{
$variable = self::where('name', 'METRICS_API_ENDPOINT')->first();
if ($variable) {
return $variable->value;
}
return '/api/1.0/processes/{process}/metrics';
}
}