More subuser Model updates.
This commit is contained in:
parent
c8f6c93d26
commit
99812b0407
|
@ -49,7 +49,7 @@ class SubuserController extends Controller
|
|||
|
||||
public function getIndex(Request $request, $uuid)
|
||||
{
|
||||
$server = Models\Server::byUuid($uuid);
|
||||
$server = Models\Server::byUuid($uuid)->load('subusers.user');
|
||||
$this->authorize('list-subusers', $server);
|
||||
|
||||
$server->js();
|
||||
|
@ -57,35 +57,17 @@ class SubuserController extends Controller
|
|||
return view('server.users.index', [
|
||||
'server' => $server,
|
||||
'node' => $server->node,
|
||||
'subusers' => Models\Subuser::select('subusers.*', 'users.email', 'users.username', 'users.use_totp')
|
||||
->join('users', 'users.id', '=', 'subusers.user_id')
|
||||
->where('server_id', $server->id)
|
||||
->get(),
|
||||
'subusers' => $server->subusers,
|
||||
]);
|
||||
}
|
||||
|
||||
public function getView(Request $request, $uuid, $id)
|
||||
{
|
||||
$server = Models\Server::byUuid($uuid);
|
||||
$server = Models\Server::byUuid($uuid)->load('node');
|
||||
$this->authorize('view-subuser', $server);
|
||||
|
||||
$subuser = Models\Subuser::select('subusers.*', 'users.email as a_userEmail')
|
||||
->join('users', 'users.id', '=', 'subusers.user_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;
|
||||
}
|
||||
$subuser = Models\Subuser::with('permissions', 'user')
|
||||
->where('server_id', $server->id)->findOrFail($id);
|
||||
|
||||
$server->js();
|
||||
|
||||
|
@ -93,7 +75,9 @@ class SubuserController extends Controller
|
|||
'server' => $server,
|
||||
'node' => $server->node,
|
||||
'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);
|
||||
$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 {
|
||||
if (! $subuser) {
|
||||
throw new DisplayException('Unable to locate a subuser by that ID.');
|
||||
} elseif ($subuser->user_id === Auth::user()->id) {
|
||||
if ($subuser->user_id === Auth::user()->id) {
|
||||
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', [
|
||||
'uuid' => $uuid,
|
||||
'id' => md5($subuser->id),
|
||||
'id' => $subuser->id,
|
||||
]);
|
||||
} catch (DisplayValidationException $ex) {
|
||||
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);
|
||||
|
||||
try {
|
||||
$subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first();
|
||||
if (! $subuser) {
|
||||
throw new DisplayException('No subuser by that ID was found on the system.');
|
||||
}
|
||||
$subuser = Models\Subuser::where('server_id', $server->id)->findOrFail($id);
|
||||
|
||||
$repo = new SubuserRepository;
|
||||
$repo->delete($subuser->id);
|
||||
|
|
|
@ -28,6 +28,14 @@ use Illuminate\Database\Eloquent\Model;
|
|||
|
||||
class Permission extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* Should timestamps be used on this model.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
|
|
|
@ -196,6 +196,16 @@ class Server extends Model
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -87,6 +87,6 @@ class Subuser extends Model
|
|||
*/
|
||||
public function permissions()
|
||||
{
|
||||
return $this->hasMany(Pemission::class);
|
||||
return $this->hasMany(Permission::class);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -167,8 +167,7 @@ class SubuserRepository
|
|||
}
|
||||
|
||||
Models\Permission::create([
|
||||
'user_id' => $user->id,
|
||||
'server_id' => $server->id,
|
||||
'subuser_id' => $subuser->id,
|
||||
'permission' => $permission,
|
||||
]);
|
||||
}
|
||||
|
@ -213,14 +212,12 @@ class SubuserRepository
|
|||
*/
|
||||
public function delete($id)
|
||||
{
|
||||
$subuser = Models\Subuser::with('server.node', 'permissions')->findOrFail($id);
|
||||
$subuser = Models\Subuser::with('server.node')->findOrFail($id);
|
||||
$server = $subuser->server;
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
try {
|
||||
Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete();
|
||||
|
||||
$server->node->guzzleClient([
|
||||
'X-Access-Server' => $server->uuid,
|
||||
'X-Access-Token' => $server->node->daemonSecret,
|
||||
|
@ -232,6 +229,9 @@ class SubuserRepository
|
|||
],
|
||||
]);
|
||||
|
||||
foreach($subuser->permissions as &$permission) {
|
||||
$permission->delete();
|
||||
}
|
||||
$subuser->delete();
|
||||
DB::commit();
|
||||
|
||||
|
@ -273,7 +273,9 @@ class SubuserRepository
|
|||
DB::beginTransaction();
|
||||
|
||||
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;
|
||||
foreach ($data['permissions'] as $permission) {
|
||||
|
@ -282,13 +284,10 @@ class SubuserRepository
|
|||
if (! is_null($this->permissions[$permission])) {
|
||||
array_push($daemonPermissions, $this->permissions[$permission]);
|
||||
}
|
||||
$model = new Models\Permission;
|
||||
$model->fill([
|
||||
'user_id' => $data['user'],
|
||||
'server_id' => $data['server'],
|
||||
Models\Permission::create([
|
||||
'subuser_id' => $subuser->id,
|
||||
'permission' => $permission,
|
||||
]);
|
||||
$model->save();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,8 @@ class SetupPermissionsPivotTable extends Migration
|
|||
|
||||
$table->dropColumn('server_id');
|
||||
$table->dropColumn('user_id');
|
||||
$table->dropColumn('created_at');
|
||||
$table->dropColumn('updated_at');
|
||||
$table->foreign('subuser_id')->references('id')->on('subusers');
|
||||
});
|
||||
}
|
||||
|
@ -49,6 +51,7 @@ class SetupPermissionsPivotTable extends Migration
|
|||
Schema::table('permissions', function (Blueprint $table) {
|
||||
$table->unsignedInteger('server_id')->after('subuser_id');
|
||||
$table->unsignedInteger('user_id')->after('server_id');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
DB::transaction(function () {
|
||||
|
|
|
@ -51,29 +51,29 @@
|
|||
@can('view-subuser', $server)<th></th>@endcan
|
||||
@can('delete-subuser', $server)<th></th>@endcan
|
||||
</tr>
|
||||
@foreach($subusers as $user)
|
||||
@foreach($subusers as $subuser)
|
||||
<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="middle">{{ $user->username }}
|
||||
<td class="middle"><code>{{ $user->email }}</code></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">{{ $subuser->user->username }}
|
||||
<td class="middle"><code>{{ $subuser->user->email }}</code></td>
|
||||
<td class="middle text-center">
|
||||
@if($user->use_totp)
|
||||
@if($subuser->user->use_totp)
|
||||
<i class="fa fa-lock text-green"></i>
|
||||
@else
|
||||
<i class="fa fa-unlock text-red"></i>
|
||||
@endif
|
||||
</td>
|
||||
<td class="middle hidden-xs">{{ $user->created_at }}</td>
|
||||
<td class="middle hidden-xs">{{ $subuser->user->created_at }}</td>
|
||||
@can('view-subuser', $server)
|
||||
<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>
|
||||
</a>
|
||||
</td>
|
||||
@endcan
|
||||
@can('delete-subuser', $server)
|
||||
<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>
|
||||
</a>
|
||||
</td>
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
@section('content')
|
||||
@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
|
||||
<div class="row">
|
||||
<div class="col-sm-12">
|
||||
|
@ -45,7 +45,7 @@
|
|||
<label class="control-label">@lang('server.users.new.email')</label>
|
||||
<div>
|
||||
{!! 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>
|
||||
|
|
Loading…
Reference in New Issue