1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Repositories; |
26
|
|
|
|
27
|
|
|
use DB; |
28
|
|
|
use Validator; |
29
|
|
|
use Pterodactyl\Models\ServiceOption; |
30
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
31
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
32
|
|
|
|
33
|
|
|
class OptionRepository |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Creates a new service option on the system. |
37
|
|
|
* |
38
|
|
|
* @param array $data |
39
|
|
|
* @return \Pterodactyl\Models\ServiceOption |
40
|
|
|
* |
41
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
42
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException |
43
|
|
|
*/ |
44
|
|
|
public function create(array $data) |
45
|
|
|
{ |
46
|
|
|
$validator = Validator::make($data, [ |
47
|
|
|
'service_id' => 'required|numeric|exists:services,id', |
48
|
|
|
'name' => 'required|string|max:255', |
49
|
|
|
'description' => 'required|string', |
50
|
|
|
'tag' => 'required|string|max:255|unique:service_options,tag', |
51
|
|
|
'docker_image' => 'required|string|max:255', |
52
|
|
|
'startup' => 'sometimes|nullable|string', |
53
|
|
|
'config_from' => 'sometimes|required|numeric|exists:service_options,id', |
54
|
|
|
'config_startup' => 'required_without:config_from|json', |
55
|
|
|
'config_stop' => 'required_without:config_from|string|max:255', |
56
|
|
|
'config_logs' => 'required_without:config_from|json', |
57
|
|
|
'config_files' => 'required_without:config_from|json', |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
if ($validator->fails()) { |
61
|
|
|
throw new DisplayValidationException(json_encode($validator->errors())); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
if (isset($data['config_from'])) { |
|
|
|
|
65
|
|
|
if (! ServiceOption::where('service_id', $data['service_id'])->where('id', $data['config_from'])->first()) { |
66
|
|
|
throw new DisplayException('The `configuration from` directive must be a child of the assigned service.'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return ServiceOption::create($data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Deletes a service option from the system. |
75
|
|
|
* |
76
|
|
|
* @param int $id |
77
|
|
|
* @return void |
78
|
|
|
* |
79
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function delete($id) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$option = ServiceOption::with('variables')->withCount('servers')->findOrFail($id); |
|
|
|
|
84
|
|
|
|
85
|
|
|
if ($option->servers_count > 0) { |
86
|
|
|
throw new DisplayException('You cannot delete a service option that has servers associated with it.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
DB::transaction(function () use ($option) { |
90
|
|
|
foreach ($option->variables as $variable) { |
91
|
|
|
(new VariableRepository)->delete($variable->id); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$option->delete(); |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Updates a service option in the database which can then be used |
100
|
|
|
* on nodes. |
101
|
|
|
* |
102
|
|
|
* @param int $id |
103
|
|
|
* @param array $data |
104
|
|
|
* @return \Pterodactyl\Models\ServiceOption |
105
|
|
|
* |
106
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
107
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException |
108
|
|
|
*/ |
109
|
|
|
public function update($id, array $data) |
110
|
|
|
{ |
111
|
|
|
$option = ServiceOption::findOrFail($id); |
112
|
|
|
|
113
|
|
|
// Due to code limitations (at least when I am writing this currently) |
114
|
|
|
// we have to make an assumption that if config_from is not passed |
115
|
|
|
// that we should be telling it that no config is wanted anymore. |
116
|
|
|
// |
117
|
|
|
// This really is only an issue if we open API access to this function, |
118
|
|
|
// in which case users will always need to pass `config_from` in order |
119
|
|
|
// to keep it assigned. |
120
|
|
|
if (! isset($data['config_from']) && ! is_null($option->config_from)) { |
121
|
|
|
$option->config_from = null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$validator = Validator::make($data, [ |
125
|
|
|
'name' => 'sometimes|required|string|max:255', |
126
|
|
|
'description' => 'sometimes|required|string', |
127
|
|
|
'tag' => 'sometimes|required|string|max:255|unique:service_options,tag,' . $option->id, |
128
|
|
|
'docker_image' => 'sometimes|required|string|max:255', |
129
|
|
|
'startup' => 'sometimes|required|string', |
130
|
|
|
'config_from' => 'sometimes|required|numeric|exists:service_options,id', |
131
|
|
|
]); |
132
|
|
|
|
133
|
|
|
$validator->sometimes([ |
134
|
|
|
'config_startup', 'config_logs', 'config_files', |
135
|
|
|
], 'required_without:config_from|json', function ($input) use ($option) { |
136
|
|
|
return ! (! $input->config_from && ! is_null($option->config_from)); |
137
|
|
|
}); |
138
|
|
|
|
139
|
|
|
$validator->sometimes('config_stop', 'required_without:config_from|string|max:255', function ($input) use ($option) { |
140
|
|
|
return ! (! $input->config_from && ! is_null($option->config_from)); |
141
|
|
|
}); |
142
|
|
|
|
143
|
|
|
if ($validator->fails()) { |
144
|
|
|
throw new DisplayValidationException(json_encode($validator->errors())); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
View Code Duplication |
if (isset($data['config_from'])) { |
|
|
|
|
148
|
|
|
if (! ServiceOption::where('service_id', $option->service_id)->where('id', $data['config_from'])->first()) { |
149
|
|
|
throw new DisplayException('The `configuration from` directive must be a child of the assigned service.'); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$option->fill($data)->save(); |
154
|
|
|
|
155
|
|
|
return $option; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Updates a service option's scripts in the database. |
160
|
|
|
* |
161
|
|
|
* @param int $id |
162
|
|
|
* @param array $data |
163
|
|
|
* @return \Pterodactyl\Models\ServiceOption |
164
|
|
|
* |
165
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
166
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException |
167
|
|
|
*/ |
168
|
|
|
public function scripts($id, array $data) |
169
|
|
|
{ |
170
|
|
|
$option = ServiceOption::findOrFail($id); |
171
|
|
|
|
172
|
|
|
$data['script_install'] = empty($data['script_install']) ? null : $data['script_install']; |
173
|
|
|
|
174
|
|
|
$validator = Validator::make($data, [ |
175
|
|
|
'script_install' => 'sometimes|nullable|string', |
176
|
|
|
'script_is_privileged' => 'sometimes|required|boolean', |
177
|
|
|
'script_entry' => 'sometimes|required|string', |
178
|
|
|
'script_container' => 'sometimes|required|string', |
179
|
|
|
'copy_script_from' => 'sometimes|nullable|numeric', |
180
|
|
|
]); |
181
|
|
|
|
182
|
|
|
if (isset($data['copy_script_from']) && ! empty($data['copy_script_from'])) { |
183
|
|
|
$select = ServiceOption::whereNull('copy_script_from')->where([ |
184
|
|
|
['id', $data['copy_script_from']], |
185
|
|
|
['service_id', $option->service_id], |
186
|
|
|
])->first(); |
187
|
|
|
|
188
|
|
|
if (! $select) { |
189
|
|
|
throw new DisplayException('The service option selected to copy a script from either does not exist, or is copying from a higher level.'); |
190
|
|
|
} |
191
|
|
|
} else { |
192
|
|
|
$data['copy_script_from'] = null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($validator->fails()) { |
196
|
|
|
throw new DisplayValidationException(json_encode($validator->errors())); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$option->fill($data)->save(); |
200
|
|
|
|
201
|
|
|
return $option; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.