More subuser Model updates.

This commit is contained in:
Dane Everitt 2017-02-09 19:38:54 -05:00
parent c8f6c93d26
commit 99812b0407
No known key found for this signature in database
GPG Key ID: EEA66103B3D71F53
8 changed files with 54 additions and 55 deletions

View File

@ -49,7 +49,7 @@ class SubuserController extends Controller
public function getIndex(Request $request, $uuid) public function getIndex(Request $request, $uuid)
{ {
$server = Models\Server::byUuid($uuid); $server = Models\Server::byUuid($uuid)->load('subusers.user');
$this->authorize('list-subusers', $server); $this->authorize('list-subusers', $server);
$server->js(); $server->js();
@ -57,35 +57,17 @@ class SubuserController extends Controller
return view('server.users.index', [ return view('server.users.index', [
'server' => $server, 'server' => $server,
'node' => $server->node, 'node' => $server->node,
'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp') 'subusers' => $server->subusers,
->join('users', 'users.id', '=', 'subusers.user_id')
->where('server_id', $server->id)
->get(),
]); ]);
} }
public function getView(Request $request, $uuid, $id) public function getView(Request $request, $uuid, $id)
{ {
$server = Models\Server::byUuid($uuid); $server = Models\Server::byUuid($uuid)->load('node');
$this->authorize('view-subuser', $server); $this->authorize('view-subuser', $server);
$subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail') $subuser = Models\Subuser::with('permissions', 'user')
->join('users', 'users.id', '=', 'subusers.user_id') ->where('server_id', $server->id)->findOrFail($id);
->where(DB::raw('md5(subusers.id)'), $id)->where('subusers.server_id', $server->id)
->first();
if (! $subuser) {
abort(404);
}
$permissions = [];
$modelPermissions = Models\Permission::select('permission')
->where('user_id', $subuser->user_id)->where('server_id', $server->id)
->get();
foreach ($modelPermissions as &$perm) {
$permissions[$perm->permission] = true;
}
$server->js(); $server->js();
@ -93,7 +75,9 @@ class SubuserController extends Controller
'server' => $server, 'server' => $server,
'node' => $server->node, 'node' => $server->node,
'subuser' => $subuser, 'subuser' => $subuser,
'permissions' => $permissions, 'permissions' => $subuser->permissions->mapWithKeys(function ($item, $key) {
return [$item->permission => true];
}),
]); ]);
} }
@ -102,12 +86,10 @@ class SubuserController extends Controller
$server = Models\Server::byUuid($uuid); $server = Models\Server::byUuid($uuid);
$this->authorize('edit-subuser', $server); $this->authorize('edit-subuser', $server);
$subuser = Models\Subuser::where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
try { try {
if (! $subuser) { if ($subuser->user_id === Auth::user()->id) {
throw new DisplayException('Unable to locate a subuser by that ID.');
} elseif ($subuser->user_id === Auth::user()->id) {
throw new DisplayException('You are not authorized to edit you own account.'); throw new DisplayException('You are not authorized to edit you own account.');
} }
@ -163,7 +145,7 @@ class SubuserController extends Controller
return redirect()->route('server.subusers.view', [ return redirect()->route('server.subusers.view', [
'uuid' => $uuid, 'uuid' => $uuid,
'id' => md5($subuser->id), 'id' => $subuser->id,
]); ]);
} catch (DisplayValidationException $ex) { } catch (DisplayValidationException $ex) {
return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput(); return redirect()->route('server.subusers.new', $uuid)->withErrors(json_decode($ex->getMessage()))->withInput();
@ -183,10 +165,7 @@ class SubuserController extends Controller
$this->authorize('delete-subuser', $server); $this->authorize('delete-subuser', $server);
try { try {
$subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first(); $subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
if (! $subuser) {
throw new DisplayException('No subuser by that ID was found on the system.');
}
$repo = new SubuserRepository; $repo = new SubuserRepository;
$repo->delete($subuser->id); $repo->delete($subuser->id);

View File

@ -28,6 +28,14 @@ use Illuminate\Database\Eloquent\Model;
class Permission extends Model class Permission extends Model
{ {
/**
* Should timestamps be used on this model.
*
* @var boolean
*/
public $timestamps = false;
/** /**
* The table associated with the model. * The table associated with the model.
* *

View File

@ -196,6 +196,16 @@ class Server extends Model
return $this->belongsTo(User::class, 'owner_id'); return $this->belongsTo(User::class, 'owner_id');
} }
/**
* Gets the subusers associated with a server.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function subusers()
{
return $this->hasMany(Subuser::class);
}
/** /**
* Gets the default allocation for a server. * Gets the default allocation for a server.
* *

View File

@ -87,6 +87,6 @@ class Subuser extends Model
*/ */
public function permissions() public function permissions()
{ {
return $this->hasMany(Pemission::class); return $this->hasMany(Permission::class);
} }
} }

View File

@ -167,8 +167,7 @@ class SubuserRepository
} }
Models\Permission::create([ Models\Permission::create([
'user_id' => $user->id, 'subuser_id' => $subuser->id,
'server_id' => $server->id,
'permission' => $permission, 'permission' => $permission,
]); ]);
} }
@ -213,14 +212,12 @@ class SubuserRepository
*/ */
public function delete($id) public function delete($id)
{ {
$subuser = Models\Subuser::with('server.node', 'permissions')->findOrFail($id); $subuser = Models\Subuser::with('server.node')->findOrFail($id);
$server = $subuser->server; $server = $subuser->server;
DB::beginTransaction(); DB::beginTransaction();
try { try {
Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete();
$server->node->guzzleClient([ $server->node->guzzleClient([
'X-Access-Server' => $server->uuid, 'X-Access-Server' => $server->uuid,
'X-Access-Token' => $server->node->daemonSecret, 'X-Access-Token' => $server->node->daemonSecret,
@ -232,6 +229,9 @@ class SubuserRepository
], ],
]); ]);
foreach($subuser->permissions as &$permission) {
$permission->delete();
}
$subuser->delete(); $subuser->delete();
DB::commit(); DB::commit();
@ -273,7 +273,9 @@ class SubuserRepository
DB::beginTransaction(); DB::beginTransaction();
try { try {
Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete(); foreach($subuser->permissions as &$permission) {
$permission->delete();
}
$daemonPermissions = $this->coreDaemonPermissions; $daemonPermissions = $this->coreDaemonPermissions;
foreach ($data['permissions'] as $permission) { foreach ($data['permissions'] as $permission) {
@ -282,13 +284,10 @@ class SubuserRepository
if (! is_null($this->permissions[$permission])) { if (! is_null($this->permissions[$permission])) {
array_push($daemonPermissions, $this->permissions[$permission]); array_push($daemonPermissions, $this->permissions[$permission]);
} }
$model = new Models\Permission; Models\Permission::create([
$model->fill([ 'subuser_id' => $subuser->id,
'user_id' => $data['user'],
'server_id' => $data['server'],
'permission' => $permission, 'permission' => $permission,
]); ]);
$model->save();
} }
} }

View File

@ -35,6 +35,8 @@ class SetupPermissionsPivotTable extends Migration
$table->dropColumn('server_id'); $table->dropColumn('server_id');
$table->dropColumn('user_id'); $table->dropColumn('user_id');
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
$table->foreign('subuser_id')->references('id')->on('subusers'); $table->foreign('subuser_id')->references('id')->on('subusers');
}); });
} }
@ -49,6 +51,7 @@ class SetupPermissionsPivotTable extends Migration
Schema::table('permissions', function (Blueprint $table) { Schema::table('permissions', function (Blueprint $table) {
$table->unsignedInteger('server_id')->after('subuser_id'); $table->unsignedInteger('server_id')->after('subuser_id');
$table->unsignedInteger('user_id')->after('server_id'); $table->unsignedInteger('user_id')->after('server_id');
$table->timestamps();
}); });
DB::transaction(function () { DB::transaction(function () {

View File

@ -51,29 +51,29 @@
@can('view-subuser', $server)<th></th>@endcan @can('view-subuser', $server)<th></th>@endcan
@can('delete-subuser', $server)<th></th>@endcan @can('delete-subuser', $server)<th></th>@endcan
</tr> </tr>
@foreach($subusers as $user) @foreach($subusers as $subuser)
<tr> <tr>
<td class="text-center middle"><img class="img-circle" src="https://www.gravatar.com/avatar/{{ md5($user->email) }}?s=128" style="height:20px;" alt="User Image"></td> <td class="text-center middle"><img class="img-circle" src="https://www.gravatar.com/avatar/{{ md5($subuser->user->email) }}?s=128" style="height:20px;" alt="User Image"></td>
<td class="middle">{{ $user->username }} <td class="middle">{{ $subuser->user->username }}
<td class="middle"><code>{{ $user->email }}</code></td> <td class="middle"><code>{{ $subuser->user->email }}</code></td>
<td class="middle text-center"> <td class="middle text-center">
@if($user->use_totp) @if($subuser->user->use_totp)
<i class="fa fa-lock text-green"></i> <i class="fa fa-lock text-green"></i>
@else @else
<i class="fa fa-unlock text-red"></i> <i class="fa fa-unlock text-red"></i>
@endif @endif
</td> </td>
<td class="middle hidden-xs">{{ $user->created_at }}</td> <td class="middle hidden-xs">{{ $subuser->user->created_at }}</td>
@can('view-subuser', $server) @can('view-subuser', $server)
<td class="text-center middle"> <td class="text-center middle">
<a href="{{ route('server.subusers.view', ['server' => $server->uuidShort, 'id' => md5($user->id)]) }}"> <a href="{{ route('server.subusers.view', ['server' => $server->uuidShort, 'id' => $subuser->id]) }}">
<button class="btn btn-xs btn-primary">@lang('server.users.configure')</button> <button class="btn btn-xs btn-primary">@lang('server.users.configure')</button>
</a> </a>
</td> </td>
@endcan @endcan
@can('delete-subuser', $server) @can('delete-subuser', $server)
<td class="text-center middle"> <td class="text-center middle">
<a href="#/delete/{{ md5($user->id) }}" data-action="delete" data-id="{{ md5($user->id) }}"> <a href="#/delete/{{ $subuser->id }}" data-action="delete" data-id="{{ $subuser->id }}">
<button class="btn btn-xs btn-danger">@lang('strings.revoke')</button> <button class="btn btn-xs btn-danger">@lang('strings.revoke')</button>
</a> </a>
</td> </td>

View File

@ -35,7 +35,7 @@
@section('content') @section('content')
@can('edit-subuser', $server) @can('edit-subuser', $server)
<form action="{{ route('server.subusers.view', [ 'uuid' => $server->uuidShort, 'id' => md5($subuser->id) ]) }}" method="POST"> <form action="{{ route('server.subusers.view', [ 'uuid' => $server->uuidShort, 'id' => $subuser->id ]) }}" method="POST">
@endcan @endcan
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
@ -45,7 +45,7 @@
<label class="control-label">@lang('server.users.new.email')</label> <label class="control-label">@lang('server.users.new.email')</label>
<div> <div>
{!! csrf_field() !!} {!! csrf_field() !!}
<input type="email" class="form-control" disabled value="{{ $subuser->a_userEmail }}" /> <input type="email" class="form-control" disabled value="{{ $subuser->user->email }}" />
</div> </div>
</div> </div>
</div> </div>