diff --git a/app/Http/Controllers/Base/APIController.php b/app/Http/Controllers/Base/APIController.php
index 611638c95..2130ee760 100644
--- a/app/Http/Controllers/Base/APIController.php
+++ b/app/Http/Controllers/Base/APIController.php
@@ -38,13 +38,8 @@ class APIController extends Controller
{
public function index(Request $request)
{
- $keys = Models\APIKey::where('user', $request->user()->id)->get();
- foreach ($keys as &$key) {
- $key->permissions = Models\APIPermission::where('key_id', $key->id)->get();
- }
-
return view('base.api.index', [
- 'keys' => $keys,
+ 'keys' => Models\APIKey::where('user_id', $request->user()->id)->get(),
]);
}
@@ -57,8 +52,11 @@ class APIController extends Controller
{
try {
$repo = new APIRepository($request->user());
- $secret = $repo->create($request->except(['_token']));
- 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.
' . $secret . '
')->flash();
+ $secret = $repo->create($request->only([
+ 'memo', 'allowed_ips',
+ 'adminPermissions', 'permissions',
+ ]));
+ 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.
' . $secret . '
')->flash();
return redirect()->route('account.api');
} catch (DisplayValidationException $ex) {
diff --git a/app/Models/APIKey.php b/app/Models/APIKey.php
index 4b94b6782..68e481712 100644
--- a/app/Models/APIKey.php
+++ b/app/Models/APIKey.php
@@ -48,4 +48,14 @@ class APIKey extends Model
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
+
+ /**
+ * Gets the permissions associated with a key.
+ *
+ * @return \Illuminate\Database\Eloquent\Relations\HasMany
+ */
+ public function permissions()
+ {
+ return $this->hasMany(APIPermission::class, 'key_id');
+ }
}
diff --git a/app/Repositories/APIRepository.php b/app/Repositories/APIRepository.php
index 7ce94f34b..382e6a274 100644
--- a/app/Repositories/APIRepository.php
+++ b/app/Repositories/APIRepository.php
@@ -102,7 +102,7 @@ class APIRepository
{
$this->user = is_null($user) ? Auth::user() : $user;
if (is_null($this->user)) {
- throw new \Exception('Cannot access API Repository without passing a user to __construct().');
+ throw new \Exception('Cannot access API Repository without passing a user to constructor.');
}
}
@@ -178,7 +178,7 @@ class APIRepository
}
}
- if ($this->user->root_admin === 1 && isset($data['adminPermissions'])) {
+ if ($this->user->isRootAdmin() && isset($data['adminPermissions'])) {
foreach ($data['adminPermissions'] as $permNode) {
if (! strpos($permNode, ':')) {
continue;
diff --git a/database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php b/database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php
new file mode 100644
index 000000000..358f9938d
--- /dev/null
+++ b/database/migrations/2017_02_10_171858_UpdateAPIKeyColumnNames.php
@@ -0,0 +1,38 @@
+dropForeign('api_keys_user_foreign')->dropIndex('api_keys_user_foreign');
+
+ $table->renameColumn('user', 'user_id');
+ $table->foreign('user_id')->references('id')->on('users');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('api_keys', function (Blueprint $table) {
+ $table->dropForeign('api_keys_user_id_foreign')->dropIndex('api_keys_user_id_foreign');
+
+ $table->renameColumn('user_id', 'user');
+ $table->foreign('user')->references('id')->on('users');
+ });
+ }
+}