feat: permissions

This commit is contained in:
JaguarJack
2022-12-07 19:28:57 +08:00
parent a1d9468a91
commit 8c537e6656
45 changed files with 1030 additions and 372 deletions

View File

@@ -32,4 +32,6 @@ interface ModuleRepositoryInterface
public function disOrEnable(string $name): bool|int;
public function getEnabled(): Collection;
public function enabled(string $moduleName): bool;
}

View File

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

View File

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

View File

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

View File

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