Skip to content

Commit

Permalink
Fix all transaction try/catches, closes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
DaneEveritt committed Feb 27, 2016
1 parent e7436aa commit 84a7eec
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 235 deletions.
51 changes: 28 additions & 23 deletions app/Repositories/APIRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,27 @@ public function new(array $data)

DB::beginTransaction();

$secretKey = str_random(16) . '.' . str_random(15);
$key = new Models\APIKey;
$key->fill([
'public' => str_random(16),
'secret' => Crypt::encrypt($secretKey),
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed)
]);
$key->save();

foreach($data['permissions'] as $permission) {
if (in_array($permission, $this->permissions)) {
$model = new Models\APIPermission;
$model->fill([
'key_id' => $key->id,
'permission' => $permission
]);
$model->save();
try {
$secretKey = str_random(16) . '.' . str_random(15);
$key = new Models\APIKey;
$key->fill([
'public' => str_random(16),
'secret' => Crypt::encrypt($secretKey),
'allowed_ips' => empty($this->allowed) ? null : json_encode($this->allowed)
]);
$key->save();

foreach($data['permissions'] as $permission) {
if (in_array($permission, $this->permissions)) {
$model = new Models\APIPermission;
$model->fill([
'key_id' => $key->id,
'permission' => $permission
]);
$model->save();
}
}
}

try {
DB::commit();
return $secretKey;
} catch (\Exception $ex) {
Expand All @@ -164,11 +164,16 @@ public function revoke(string $key)
{
DB::beginTransaction();

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

DB::commit();
DB::commit();
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
}

}
57 changes: 29 additions & 28 deletions app/Repositories/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,51 +151,52 @@ public function addAllocations($id, array $allocations)
$node = Models\Node::findOrFail($id);

DB::beginTransaction();
foreach($allocations as $rawIP => $ports) {
$parsedIP = Network::parse($rawIP);
foreach($parsedIP as $ip) {
foreach($ports as $port) {
if (!is_int($port) && !preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
}
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
foreach(range($matches[1], $matches[2]) as $assignPort) {

try {
foreach($allocations as $rawIP => $ports) {
$parsedIP = Network::parse($rawIP);
foreach($parsedIP as $ip) {
foreach($ports as $port) {
if (!is_int($port) && !preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
}
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
foreach(range($matches[1], $matches[2]) as $assignPort) {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort
]);
if (!$alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort,
'assigned_to' => null
]);
$alloc->save();
}
}
} else {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort
'port' => $port
]);
if (!$alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort,
'port' => $port,
'assigned_to' => null
]);
$alloc->save();
}
}
} else {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
'port' => $port
]);
if (!$alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $port,
'assigned_to' => null
]);
$alloc->save();
}
}
}
}
}

try {
DB::commit();
return true;
} catch (\Exception $ex) {
Expand Down
Loading

0 comments on commit 84a7eec

Please sign in to comment.