Skip to content

Commit 84a7eec

Browse files
committed
Fix all transaction try/catches, closes pterodactyl#57
1 parent e7436aa commit 84a7eec

File tree

4 files changed

+238
-235
lines changed

4 files changed

+238
-235
lines changed

app/Repositories/APIRepository.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,27 +122,27 @@ public function new(array $data)
122122

123123
DB::beginTransaction();
124124

125-
$secretKey = str_random(16) . '.' . str_random(15);
126-
$key = new Models\APIKey;
127-
$key->fill([
128-
'public' => str_random(16),
129-
'secret' => Crypt::encrypt($secretKey),
130-
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed)
131-
]);
132-
$key->save();
133-
134-
foreach($data['permissions'] as $permission) {
135-
if (in_array($permission, $this->permissions)) {
136-
$model = new Models\APIPermission;
137-
$model->fill([
138-
'key_id' => $key->id,
139-
'permission' => $permission
140-
]);
141-
$model->save();
125+
try {
126+
$secretKey = str_random(16) . '.' . str_random(15);
127+
$key = new Models\APIKey;
128+
$key->fill([
129+
'public' => str_random(16),
130+
'secret' => Crypt::encrypt($secretKey),
131+
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed)
132+
]);
133+
$key->save();
134+
135+
foreach($data['permissions'] as $permission) {
136+
if (in_array($permission, $this->permissions)) {
137+
$model = new Models\APIPermission;
138+
$model->fill([
139+
'key_id' => $key->id,
140+
'permission' => $permission
141+
]);
142+
$model->save();
143+
}
142144
}
143-
}
144145

145-
try {
146146
DB::commit();
147147
return $secretKey;
148148
} catch (\Exception $ex) {
@@ -164,11 +164,16 @@ public function revoke(string $key)
164164
{
165165
DB::beginTransaction();
166166

167-
$model = Models\APIKey::where('public', $key)->firstOrFail();
168-
$permissions = Models\APIPermission::where('key_id', $model->id)->delete();
169-
$model->delete();
167+
try {
168+
$model = Models\APIKey::where('public', $key)->firstOrFail();
169+
$permissions = Models\APIPermission::where('key_id', $model->id)->delete();
170+
$model->delete();
170171

171-
DB::commit();
172+
DB::commit();
173+
} catch (\Exception $ex) {
174+
DB::rollBack();
175+
throw $ex;
176+
}
172177
}
173178

174179
}

app/Repositories/NodeRepository.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -151,51 +151,52 @@ public function addAllocations($id, array $allocations)
151151
$node = Models\Node::findOrFail($id);
152152

153153
DB::beginTransaction();
154-
foreach($allocations as $rawIP => $ports) {
155-
$parsedIP = Network::parse($rawIP);
156-
foreach($parsedIP as $ip) {
157-
foreach($ports as $port) {
158-
if (!is_int($port) && !preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
159-
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
160-
}
161-
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
162-
foreach(range($matches[1], $matches[2]) as $assignPort) {
154+
155+
try {
156+
foreach($allocations as $rawIP => $ports) {
157+
$parsedIP = Network::parse($rawIP);
158+
foreach($parsedIP as $ip) {
159+
foreach($ports as $port) {
160+
if (!is_int($port) && !preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
161+
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
162+
}
163+
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
164+
foreach(range($matches[1], $matches[2]) as $assignPort) {
165+
$alloc = Models\Allocation::firstOrNew([
166+
'node' => $node->id,
167+
'ip' => $ip,
168+
'port' => $assignPort
169+
]);
170+
if (!$alloc->exists) {
171+
$alloc->fill([
172+
'node' => $node->id,
173+
'ip' => $ip,
174+
'port' => $assignPort,
175+
'assigned_to' => null
176+
]);
177+
$alloc->save();
178+
}
179+
}
180+
} else {
163181
$alloc = Models\Allocation::firstOrNew([
164182
'node' => $node->id,
165183
'ip' => $ip,
166-
'port' => $assignPort
184+
'port' => $port
167185
]);
168186
if (!$alloc->exists) {
169187
$alloc->fill([
170188
'node' => $node->id,
171189
'ip' => $ip,
172-
'port' => $assignPort,
190+
'port' => $port,
173191
'assigned_to' => null
174192
]);
175193
$alloc->save();
176194
}
177195
}
178-
} else {
179-
$alloc = Models\Allocation::firstOrNew([
180-
'node' => $node->id,
181-
'ip' => $ip,
182-
'port' => $port
183-
]);
184-
if (!$alloc->exists) {
185-
$alloc->fill([
186-
'node' => $node->id,
187-
'ip' => $ip,
188-
'port' => $port,
189-
'assigned_to' => null
190-
]);
191-
$alloc->save();
192-
}
193196
}
194197
}
195198
}
196-
}
197199

198-
try {
199200
DB::commit();
200201
return true;
201202
} catch (\Exception $ex) {

0 commit comments

Comments
 (0)