added new conductors

This commit is contained in:
2023-03-12 13:51:23 +10:00
parent 3d13fc6864
commit 615abcc8e3
5 changed files with 335 additions and 39 deletions

View File

@@ -1,4 +1,5 @@
<?php
namespace App\Conductors;
use Illuminate\Database\Eloquent\Builder;
@@ -7,47 +8,75 @@ use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class UserConductor extends Conductor {
class UserConductor extends Conductor
{
/**
* The Model Class
* @var string
*/
protected $class = '\App\Models\User';
public function fields(Model $model) {
/**
* Return the visible API fields.
*
* @param Model $model The model.
* @return string[] The fields visible.
*/
public function fields(Model $model)
{
$user = auth()->user();
if($user === null || $user->hasPermission('admin/users') === false) {
if ($user === null || $user->hasPermission('admin/users') === false) {
return ['id', 'username'];
}
return parent::fields($model);
}
public function transform(Model $model) {
/**
* Transform the passed Model to an array
*
* @param Model $model The model to transform.
* @return array The transformed model.
*/
public function transform(Model $model)
{
$user = auth()->user();
$data = $model->toArray();
if($user === null || strcasecmp($user->id, $model->id) !== 0) {
if ($user === null || strcasecmp($user->id, $model->id) !== 0) {
$fields = ['id', 'username'];
$data = array_intersect_key($data, array_flip($fields));
$data = arrayOnlyItems($data, $fields);
}
return $data;
}
public static function viewable(Model $model) {
return true;
}
public static function updatable(Model $model) {
/**
* Return if the current model is updatable.
*
* @param Model $model The model.
* @return boolean Allow updating model.
*/
public static function updatable(Model $model)
{
$user = auth()->user();
if($user !== null) {
return $user->hasPermission('admin/users') === true || strcasecmp($user->id, $model->id) === 0;
if ($user !== null) {
return ($user->hasPermission('admin/users') === true || strcasecmp($user->id, $model->id) === 0);
}
return false;
}
public static function destroyable(Model $model) {
/**
* Return if the current model is deletable.
*
* @param Model $model The model.
* @return boolean Allow deleting model.
*/
public static function destroyable(Model $model)
{
$user = auth()->user();
return $user !== null && $user->hasPermission('admin/users') === true;
return ($user !== null && $user->hasPermission('admin/users') === true);
}
}
}