feat: permissions
This commit is contained in:
@@ -32,4 +32,6 @@ interface ModuleRepositoryInterface
|
||||
public function disOrEnable(string $name): bool|int;
|
||||
|
||||
public function getEnabled(): Collection;
|
||||
|
||||
public function enabled(string $moduleName): bool;
|
||||
}
|
||||
|
@@ -135,6 +135,18 @@ class DatabaseDriver implements ModuleRepositoryInterface
|
||||
return $this->model->where('status', Status::Enable->value())->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* enabled
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return bool
|
||||
*/
|
||||
public function enabled(string $moduleName): bool
|
||||
{
|
||||
// TODO: Implement enabled() method.
|
||||
return $this->getEnabled()->pluck('path')->contains($moduleName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $module
|
||||
|
@@ -172,6 +172,17 @@ class FileDriver implements ModuleRepositoryInterface
|
||||
return $this->all()->where('enable', true)->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* enabled
|
||||
* @param string $moduleName
|
||||
* @return bool
|
||||
*/
|
||||
public function enabled(string $moduleName): bool
|
||||
{
|
||||
// TODO: Implement enabled() method.
|
||||
return $this->getEnabled()->pluck('path')->contains($moduleName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $module
|
||||
|
@@ -141,4 +141,15 @@ class ModuleRepository
|
||||
// TODO: Implement getEnabled() method.
|
||||
return $this->moduleRepository->getEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* enabled
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @return bool
|
||||
*/
|
||||
public function enabled(string $moduleName): bool
|
||||
{
|
||||
return $this->moduleRepository->enabled($moduleName);
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,9 @@ declare(strict_types=1);
|
||||
namespace Catch\Traits\DB;
|
||||
|
||||
use Catch\Enums\Status;
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
|
||||
/**
|
||||
@@ -27,6 +29,12 @@ trait BaseOperate
|
||||
|
||||
protected bool $sortDesc = true;
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected string $parentId = 'parent_id';
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
@@ -178,7 +186,36 @@ trait BaseOperate
|
||||
|
||||
$model->{$field} = $model->{$field} == Status::Enable->value() ? Status::Disable->value() : Status::Enable->value();
|
||||
|
||||
return $model->save();
|
||||
if ($model->save() && in_array($this->parentId, $this->getFillable())) {
|
||||
$this->updateChildren($id, $field, $model->{$field});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 递归处理
|
||||
*
|
||||
* @param int|array $parentId
|
||||
* @param string $field
|
||||
* @param int $value
|
||||
*/
|
||||
public function updateChildren(mixed $parentId, string $field, mixed $value): void
|
||||
{
|
||||
if (! $parentId instanceof Arrayable) {
|
||||
$parentId = Collection::make([$parentId]);
|
||||
}
|
||||
|
||||
$childrenId = $this->whereIn('parent_id', $parentId)->pluck('id');
|
||||
|
||||
if ($childrenId->count()) {
|
||||
if ($this->whereIn('parent_id', $parentId)->update([
|
||||
$field => $value
|
||||
])) {
|
||||
$this->updateChildren($childrenId, $field, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user