feat: 新增项目任务创建权限功能 - 90%

This commit is contained in:
ganzizi
2023-11-24 18:25:28 +08:00
parent ecdbf8765f
commit b0742021b6
7 changed files with 339 additions and 75 deletions

View File

@@ -32,7 +32,8 @@ use Request;
* @property int|null $archived_follow 跟随项目归档(项目取消归档时任务也取消归档)
* @property string|null $complete_at 完成时间
* @property int|null $userid 创建人
* @property int|null $is_all_visible 是否所有人可见
* @property int|null $visibility 任务可见性1-项目人员 2-任务人员 3-指定成员
* @property int|null $is_default 是否默认任务
* @property int|null $p_level 优先级
* @property string|null $p_name 优先级名称
* @property string|null $p_color 优先级颜色
@@ -81,7 +82,7 @@ use Request;
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereFlowItemId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereFlowItemName($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereIsAllVisible($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereIsDefault($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereLoop($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereLoopAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereName($value)
@@ -94,6 +95,7 @@ use Request;
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereStartAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereUserid($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereVisibility($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask withoutTrashed()
* @mixin \Eloquent
@@ -1667,8 +1669,8 @@ class ProjectTask extends AbstractModel
/**
* 移动任务
* @param int $project_id
* @param int $column_id
* @param int $project_id
* @param int $column_id
* @return bool
*/
public function moveTask(int $projectId, int $columnId)
@@ -1694,11 +1696,11 @@ class ProjectTask extends AbstractModel
$taskUser->project_id = $projectId;
$taskUser->save();
}
//
//
$this->project_id = $projectId;
$this->column_id = $columnId;
$this->save();
//
//
$this->addLog("移动{任务}");
});
$this->pushMsg('update');
@@ -1728,14 +1730,11 @@ class ProjectTask extends AbstractModel
* @param int $task_id
* @param bool $archived true:仅限未归档, false:仅限已归档, null:不限制
* @param bool $trashed true:仅限未删除, false:仅限已删除, null:不限制
* @param int|bool $permission
* - 0|false 限制:项目成员、任务成员、任务群聊成员(任务成员 = 任务创建人+任务协助人+任务负责人)
* - 1|true 限制:项目负责人、任务成员
* - 2 已有负责人才限制true (子任务时如果是主任务负责人也可以)
* @param string $action 动作名称
* @param array $with
* @return self
*/
public static function userTask($task_id, $archived = true, $trashed = true, $permission = false, $with = [])
public static function userTask($task_id, $archived = true, $trashed = true, $action = '', $with = [])
{
$builder = self::with($with)->allData()->where("project_tasks.id", intval($task_id));
if ($trashed === false) {
@@ -1758,7 +1757,7 @@ class ProjectTask extends AbstractModel
try {
$project = Project::userProject($task->project_id);
} catch (\Throwable $e) {
if ($task->owner !== null || (!$permission && $task->permission(4))) {
if ($task->owner !== null || (empty($action) && $task->permission(4))) {
$project = Project::find($task->project_id);
if (empty($project)) {
throw new ApiException('项目不存在或已被删除', [ 'task_id' => $task_id ], -4002);
@@ -1767,14 +1766,52 @@ class ProjectTask extends AbstractModel
throw new ApiException($e->getMessage(), [ 'task_id' => $task_id ], -4002);
}
}
//
if ($permission >= 2) {
$permission = $task->hasOwner() ? 1 : 0;
}
if ($permission && !$project->owner && !$task->permission(3)) {
throw new ApiException('仅限项目负责人、任务负责人、协助人员或任务创建者操作');
if ($action) {
self::userTaskPermission($action, $project, $task);
}
//
return $task;
}
/**
* 检查用户是否有执行特定动作的权限
* @param string $action 动作名称
* @param Project $project 项目实例
* @param Task $task 任务实例
* @return bool
*/
public static function userTaskPermission($action, $project, $task = null)
{
$permissions = ProjectPermission::getPermission($project->id, $action);
foreach ($permissions as $permission) {
switch ($permission) {
case 1:
// 项目负责人
if (!$project->owner) {
throw new ApiException('仅限项目负责人操作', [ 'project_id' => $project->id ]);
}
break;
case 2:
// 任务负责人
if (!$task->isOwner()) {
throw new ApiException('仅限任务负责人操作', [ 'project_id' => $project->id ]);
}
break;
case 3:
// 项目成员
$instance = new self();
if (!($instance->useridInTheProject(User::userid()) === 1)) {
throw new ApiException('仅限项目成员操作', [ 'project_id' => $project->id ]);
}
break;
case 4:
// 任务成员(任务成员 = 任务创建人+任务协助人+任务负责人)
if ($task->isCreater()) {
throw new ApiException('仅限任务成员操作', [ 'project_id' => $project->id ]);
}
break;
}
}
return true;
}
}