API Model updates.

This commit is contained in:
Dane Everitt 2017-02-10 17:29:10 -05:00
parent efef356870
commit 3b3002b77a
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 56 additions and 10 deletions

View file

@ -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.<br /><br /><code>' . $secret . '</code>')->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.<br /><br /><code>' . $secret . '</code>')->flash();
return redirect()->route('account.api');
} catch (DisplayValidationException $ex) {

View file

@ -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');
}
}

View file

@ -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;

View file

@ -0,0 +1,38 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateAPIKeyColumnNames extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('api_keys', function (Blueprint $table) {
$table->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');
});
}
}