- 创建 SopTemplatePolicy 策略类 - 实现查看、创建、更新、删除权限 - 实现发布和归档权限 - 已发布的模板不能编辑和删除 - 只有草稿状态可以发布 - 只有已发布状态可以归档 - 在 AppServiceProvider 中注册策略
76 lines
1.4 KiB
PHP
76 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\SopTemplate;
|
|
use App\Models\User;
|
|
|
|
class SopTemplatePolicy
|
|
{
|
|
/**
|
|
* 查看任何 SOP 模板
|
|
*/
|
|
public function viewAny(User $user): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 查看 SOP 模板
|
|
*/
|
|
public function view(User $user, SopTemplate $sopTemplate): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 创建 SOP 模板
|
|
*/
|
|
public function create(User $user): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 更新 SOP 模板
|
|
*/
|
|
public function update(User $user, SopTemplate $sopTemplate): bool
|
|
{
|
|
// 已发布的模板不能直接编辑
|
|
if ($sopTemplate->status === 'published') {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除 SOP 模板
|
|
*/
|
|
public function delete(User $user, SopTemplate $sopTemplate): bool
|
|
{
|
|
// 已发布的模板不能删除
|
|
if ($sopTemplate->status === 'published') {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 发布 SOP 模板
|
|
*/
|
|
public function publish(User $user, SopTemplate $sopTemplate): bool
|
|
{
|
|
return $sopTemplate->status === 'draft';
|
|
}
|
|
|
|
/**
|
|
* 归档 SOP 模板
|
|
*/
|
|
public function archive(User $user, SopTemplate $sopTemplate): bool
|
|
{
|
|
return $sopTemplate->status === 'published';
|
|
}
|
|
}
|