Skip to content

Commit 3b3002b

Browse files
committed
API Model updates.
1 parent efef356 commit 3b3002b

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

app/Http/Controllers/Base/APIController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@ class APIController extends Controller
3838
{
3939
public function index(Request $request)
4040
{
41-
$keys = Models\APIKey::where('user', $request->user()->id)->get();
42-
foreach ($keys as &$key) {
43-
$key->permissions = Models\APIPermission::where('key_id', $key->id)->get();
44-
}
45-
4641
return view('base.api.index', [
47-
'keys' => $keys,
42+
'keys' => Models\APIKey::where('user_id', $request->user()->id)->get(),
4843
]);
4944
}
5045

@@ -57,8 +52,11 @@ public function save(Request $request)
5752
{
5853
try {
5954
$repo = new APIRepository($request->user());
60-
$secret = $repo->create($request->except(['_token']));
61-
Alert::success('An API Keypair has successfully been generated. The API secret for this public key is shown below and will not be shown again.<br /><br /><code>' . $secret . '</code>')->flash();
55+
$secret = $repo->create($request->only([
56+
'memo', 'allowed_ips',
57+
'adminPermissions', 'permissions',
58+
]));
59+
Alert::success('An API Key-Pair has successfully been generated. The API secret for this public key is shown below and will not be shown again.<br /><br /><code>' . $secret . '</code>')->flash();
6260

6361
return redirect()->route('account.api');
6462
} catch (DisplayValidationException $ex) {

app/Models/APIKey.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ class APIKey extends Model
4848
* @var array
4949
*/
5050
protected $guarded = ['id', 'created_at', 'updated_at'];
51+
52+
/**
53+
* Gets the permissions associated with a key.
54+
*
55+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
56+
*/
57+
public function permissions()
58+
{
59+
return $this->hasMany(APIPermission::class, 'key_id');
60+
}
5161
}

app/Repositories/APIRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public function __construct(Models\User $user = null)
102102
{
103103
$this->user = is_null($user) ? Auth::user() : $user;
104104
if (is_null($this->user)) {
105-
throw new \Exception('Cannot access API Repository without passing a user to __construct().');
105+
throw new \Exception('Cannot access API Repository without passing a user to constructor.');
106106
}
107107
}
108108

@@ -178,7 +178,7 @@ public function create(array $data)
178178
}
179179
}
180180

181-
if ($this->user->root_admin === 1 && isset($data['adminPermissions'])) {
181+
if ($this->user->isRootAdmin() && isset($data['adminPermissions'])) {
182182
foreach ($data['adminPermissions'] as $permNode) {
183183
if (! strpos($permNode, ':')) {
184184
continue;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class UpdateAPIKeyColumnNames extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('api_keys', function (Blueprint $table) {
17+
$table->dropForeign('api_keys_user_foreign')->dropIndex('api_keys_user_foreign');
18+
19+
$table->renameColumn('user', 'user_id');
20+
$table->foreign('user_id')->references('id')->on('users');
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*
27+
* @return void
28+
*/
29+
public function down()
30+
{
31+
Schema::table('api_keys', function (Blueprint $table) {
32+
$table->dropForeign('api_keys_user_id_foreign')->dropIndex('api_keys_user_id_foreign');
33+
34+
$table->renameColumn('user_id', 'user');
35+
$table->foreign('user')->references('id')->on('users');
36+
});
37+
}
38+
}

0 commit comments

Comments
 (0)