修改收集任务和分享的删除逻辑

删除→禁用
*已知缺陷,分享和收集详情可以被其他用户访问
This commit is contained in:
Chenx221 2024-03-04 16:26:37 +08:00
parent 6b568955a6
commit 63fef55f65
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
4 changed files with 35 additions and 8 deletions

View File

@ -46,7 +46,7 @@ class CollectionController extends Controller
{
$searchModel = new CollectionSearch();
$dataProvider = $searchModel->search($this->request->queryParams);
$dataProvider->query->andWhere(['!=', 'status', 0]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
@ -101,7 +101,18 @@ class CollectionController extends Controller
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
// 获取模型
$model = $this->findModel($id);
// 设置状态为禁用
$model->status = 0;
// 保存模型
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Task delete successfully.');
} else {
Yii::$app->session->setFlash('error', 'Failed to delete task.');
}
return $this->redirect(['index']);
}
@ -137,8 +148,8 @@ class CollectionController extends Controller
$secret = $receive_secret;
}
$model = CollectionTasks::findOne(['id' => $id]);
if ($model === null) {
throw new NotFoundHttpException('请求的文件收集任务不存在');
if ($model === null | $model->status === 0) {
throw new NotFoundHttpException('请求的文件收集任务已失效或不存在');
} elseif (!is_dir(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->user_id . '/' . $model->folder_path)) {
throw new NotFoundHttpException('收集任务的目标路径不存在');
} elseif ($secret === null) {

View File

@ -43,6 +43,7 @@ class ShareController extends Controller
{
$searchModel = new ShareSearch();
$dataProvider = $searchModel->search($this->request->queryParams);
$dataProvider->query->andWhere(['!=', 'status', 0]);
return $this->render('index', [
'searchModel' => $searchModel,
@ -138,7 +139,18 @@ class ShareController extends Controller
*/
public function actionDelete($share_id)
{
$this->findModel($share_id)->delete();
// 获取模型
$model = $this->findModel($share_id);
// 设置状态为禁用
$model->status = 0;
// 保存模型
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Share delete successfully.');
} else {
Yii::$app->session->setFlash('error', 'Failed to delete share.');
}
return $this->redirect(['index']);
}
@ -164,7 +176,7 @@ class ShareController extends Controller
$model = $this->findModel($share_id);
//检查文件/文件夹是否存在
$abp = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->sharer_id . '/' . $model->file_relative_path;
if (!file_exists($abp)) {
if (!file_exists($abp) || $model->status == 0) {
throw new NotFoundHttpException('分享失效,文件或文件夹不存在');
}
if ($this->request->isPost) {

View File

@ -14,6 +14,7 @@ use yii\db\ActiveRecord;
* @property string $folder_path 收集目标文件夹(相对路径)
* @property string $created_at 收集任务创建时间
* @property string $secret 访问密钥
* @property int|null $status 收集任务是否启用
*
* @property CollectionUploaded[] $collectionUploadeds
* @property User $user
@ -38,7 +39,7 @@ class CollectionTasks extends ActiveRecord
return [
[['user_id', 'folder_path', 'secret'], 'required'],
[['folder_path', 'secret'], 'required', 'on' => self::SCENARIO_CREATE],
[['user_id'], 'integer'],
[['user_id', 'status'], 'integer'],
[['created_at'], 'safe'],
[['folder_path', 'secret'], 'string', 'max' => 255],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id']],
@ -56,6 +57,7 @@ class CollectionTasks extends ActiveRecord
'folder_path' => '收集目标文件夹(相对路径)',
'created_at' => '任务创建时间',
'secret' => '访问密钥',
'status' => 'Status',
];
}

View File

@ -14,6 +14,7 @@ use yii\db\ActiveRecord;
* @property string $file_relative_path 文件的相对路径
* @property string $access_code 分享密钥
* @property string $creation_date 分享创建日期
* @property int|null $status 分享是否启用
*
* @property User $sharer
*/
@ -35,7 +36,7 @@ class Share extends ActiveRecord
{
return [
[['file_relative_path', 'access_code'], 'required'],
[['sharer_id'], 'integer'],
[['sharer_id', 'status'], 'integer'],
[['creation_date'], 'safe'],
[['file_relative_path'], 'string', 'max' => 255],
[['access_code'], 'string', 'max' => 4],
@ -55,6 +56,7 @@ class Share extends ActiveRecord
'file_relative_path' => '文件位置',
'access_code' => '访问密码',
'creation_date' => '分享创建日期',
'status' => 'Status',
];
}