From 18e7f34c9daf3f61408174bbc6ae566fbe357a9b Mon Sep 17 00:00:00 2001 From: githubjeka Date: Wed, 11 Nov 2020 14:42:46 +0300 Subject: [PATCH] feat: Ability to open access to the document file for all users --- controllers/DocumentController.php | 16 ++- controllers/requests/DocumentRequest.php | 3 + controllers/services/DocumentCreator.php | 1 + controllers/services/DocumentEditor.php | 3 + models/Document.php | 61 +++++---- views/document/_form.php | 2 + views/document/form_change_info.php | 2 + views/document/view.php | 167 ++++++++++++----------- widgets/LinkToDocFileWidget.php | 4 +- 9 files changed, 144 insertions(+), 115 deletions(-) diff --git a/controllers/DocumentController.php b/controllers/DocumentController.php index 8e92495..f17680b 100644 --- a/controllers/DocumentController.php +++ b/controllers/DocumentController.php @@ -248,17 +248,23 @@ public function actionToAddReceivers($id) */ protected function findModelForUser($id, \yii\web\IdentityInterface $user, $andDownload = false) { - $model = Document::find()->readable($user, $andDownload)->byId($id)->one(); + $document = Document::find()->byId($id)->one(); + + if ((bool)$document->access_for_all === true) { + return $document; + } - if ($model === null) { + $document = Document::find()->readable($user, $andDownload)->byId($id)->one(); + + if ($document === null) { - $model = Document::find()->byCreator($user)->byId($id)->one(); + $document = Document::find()->byCreator($user)->byId($id)->one(); - if ($model === null) { + if ($document === null) { throw new NotFoundHttpException('The requested page does not exist.'); } } - return $model; + return $document; } } diff --git a/controllers/requests/DocumentRequest.php b/controllers/requests/DocumentRequest.php index dbe5d1e..27f9f7e 100644 --- a/controllers/requests/DocumentRequest.php +++ b/controllers/requests/DocumentRequest.php @@ -11,6 +11,7 @@ class DocumentRequest extends \yii\base\Model { public $name; public $description; + public $access; public $from; public $registeredAt; public $to; @@ -35,6 +36,7 @@ public function rules() [['name', 'from', 'to', 'number'], 'string', 'max' => 255], ['file', 'file', 'skipOnEmpty' => false,], ['description', 'string'], + ['access', 'boolean'], ]; } @@ -52,6 +54,7 @@ public function attributeLabels() 'receivers' => \Yii::t('TrackerIssuesModule.views', 'Receivers'), 'registeredAt' => \Yii::t('TrackerIssuesModule.views', 'Registered at'), 'space' => \Yii::t('TrackerIssuesModule.views', 'Space'), + 'access' => \Yii::t('TrackerIssuesModule.views', 'Accessible to all'), ]; } } diff --git a/controllers/services/DocumentCreator.php b/controllers/services/DocumentCreator.php index ab96e81..a8813cf 100644 --- a/controllers/services/DocumentCreator.php +++ b/controllers/services/DocumentCreator.php @@ -94,6 +94,7 @@ public function create() $documentModel->type = $this->requestForm->type; $documentModel->created_by = \Yii::$app->user->id; $documentModel->created_at = time(); + $documentModel->access_for_all = $this->requestForm->access; $registeredAtDateObj = \DateTime::createFromFormat('Y-m-d', $this->requestForm->registeredAt); if ($registeredAtDateObj === false) { diff --git a/controllers/services/DocumentEditor.php b/controllers/services/DocumentEditor.php index 7ade385..0c3eda6 100644 --- a/controllers/services/DocumentEditor.php +++ b/controllers/services/DocumentEditor.php @@ -41,6 +41,7 @@ public function __construct(Document $document, array $config = []) $this->requestForm->to = $this->document->to; $this->requestForm->type = $this->document->type; $this->requestForm->category = $this->document->category; + $this->requestForm->access = $this->document->access_for_all; } /** @@ -67,6 +68,7 @@ public function save() 'from', 'to', 'category', + 'access', ])) { return false; } @@ -79,6 +81,7 @@ public function save() $this->document->to = $this->requestForm->to; $this->document->from = $this->requestForm->from; $this->document->type = $this->requestForm->type; + $this->document->access_for_all = $this->requestForm->access; $registeredAtDateObj = \DateTime::createFromFormat('Y-m-d', $this->requestForm->registeredAt); if ($registeredAtDateObj === false) { diff --git a/models/Document.php b/models/Document.php index 1c34ca0..657b254 100644 --- a/models/Document.php +++ b/models/Document.php @@ -22,6 +22,7 @@ * @property integer $registered_at * @property integer $created_at * @property string $created_by + * @property bool|int $access_for_all * @property DocumentReceiver[] $receivers * @property Issue[] $issues * @property DocumentType|null $typeModel @@ -38,13 +39,43 @@ public static function tableName() return '{{%tracker_document}}'; } + /** + * Returns List of DocumentType models + * + * @return DocumentType[] + */ + public static function types() + { + return DocumentType::findByType(DocumentType::class)->all(); + } + + /** + * Returns List of DocumentCategory models + * + * @return DocumentCategory[] + */ + public static function categories() + { + return DocumentCategory::findByType(DocumentCategory::class)->all(); + } + + /** + * @inheritdoc + * @return DocumentQuery the active query used by this AR class. + */ + public static function find() + { + return new DocumentQuery(get_called_class()); + } + /** * @inheritdoc */ public function rules() { return [ - [['name', 'created_by', 'registered_at', 'created_at'], 'required'], + ['access_for_all', 'default', 'value' => false], + [['name', 'created_by', 'registered_at', 'created_at', 'access_for_all'], 'required'], [['registered_at', 'created_at', 'created_by'], 'integer'], [['description'], 'string'], [['type', 'category'], 'integer'], @@ -68,6 +99,7 @@ public function attributeLabels() 'type' => Yii::t('TrackerIssuesModule.views', 'Type'), 'category' => Yii::t('TrackerIssuesModule.views', 'Category'), 'registered_at' => Yii::t('TrackerIssuesModule.views', 'Registered at'), + 'access_for_all' => Yii::t('TrackerIssuesModule.views', 'Accessible to all'), ]; } @@ -79,15 +111,6 @@ public function getReceivers() return $this->hasMany(DocumentReceiver::className(), ['document_id' => 'id'])->inverseOf('document'); } - /** - * Returns List of DocumentType models - * @return DocumentType[] - */ - public static function types() - { - return DocumentType::findByType(DocumentType::class)->all(); - } - /** * @return ActiveQuery */ @@ -96,15 +119,6 @@ public function getTypeModel() return $this->hasOne(DocumentType::class, ['id' => 'type']); } - /** - * Returns List of DocumentCategory models - * @return DocumentCategory[] - */ - public static function categories() - { - return DocumentCategory::findByType(DocumentCategory::class)->all(); - } - /** * @return ActiveQuery */ @@ -124,13 +138,4 @@ public function getFile() { return $this->hasOne(DocumentFile::class, ['document_id' => 'id'])->andWhere(['is_show' => true]); } - - /** - * @inheritdoc - * @return DocumentQuery the active query used by this AR class. - */ - public static function find() - { - return new DocumentQuery(get_called_class()); - } } diff --git a/views/document/_form.php b/views/document/_form.php index 0785b52..a99a40a 100644 --- a/views/document/_form.php +++ b/views/document/_form.php @@ -21,6 +21,8 @@ field($documentRequest, 'file')->fileInput() ?> + field($documentRequest, 'access')->checkbox(); ?> + field($documentRequest, 'receivers') ->widget(\humhub\modules\user\widgets\UserPickerField::class, [ diff --git a/views/document/form_change_info.php b/views/document/form_change_info.php index 6ed61fb..84fa754 100644 --- a/views/document/form_change_info.php +++ b/views/document/form_change_info.php @@ -37,6 +37,8 @@ errorSummary($requestModel); ?> + field($requestModel, 'access')->checkbox(); ?> + field($requestModel, 'category') ->dropDownList( ArrayHelper::map(Document::categories(), 'id', 'name'), diff --git a/views/document/view.php b/views/document/view.php index 7f26d00..eb060a9 100644 --- a/views/document/view.php +++ b/views/document/view.php @@ -90,6 +90,13 @@ \tracker\widgets\LinkToDocFileWidget::widget(['document' => $model]) . '', ], + [ + 'label' => Yii::t('TrackerIssuesModule.views', 'Accessible to all'), + 'format' => 'raw', + 'value' => $model->access_for_all + ? '' + : '' + ], [ 'attribute' => 'number', 'format' => 'html', @@ -195,100 +202,100 @@ class="img-rounded tt img_margin" user->isGuest && Document::find() - ->readable(\Yii::$app->user->identity, true) - ->byId($model->id) - ->exists()) : ?> + ->readable(\Yii::$app->user->identity, true) + ->byId($model->id) + ->exists()) : ?> -
-
-

+
+
+

: -

-
+

+
-
- getIssues() - ->leftJoin( - Link::tableName(), - \tracker\models\Issue::tableName() . '.id = child_id' - ) - ->andWhere('parent_id IS NULL') - ->orderBy([\tracker\models\Issue::tableName() . '.deadline' => SORT_ASC]) - ->all(); - ?> - 0) : ?> +
'size']); + $issues = $model + ->getIssues() + ->leftJoin( + Link::tableName(), + \tracker\models\Issue::tableName() . '.id = child_id' + ) + ->andWhere('parent_id IS NULL') + ->orderBy([\tracker\models\Issue::tableName() . '.deadline' => SORT_ASC]) + ->all(); + ?> + 0) : ?> + 'size']); - echo Html::beginTag('i'); - echo Html::beginTag('small'); - echo StatusIssueWidget::widget(['status' => $issue->status]); - if (empty($issue->deadline) && $issue->status === \tracker\enum\IssueStatusEnum::TYPE_WORK) { - echo ' '; - echo Html::beginTag('span', ['class' => 'label label-success']); - echo Yii::t('TrackerIssuesModule.views', 'constantly'); - echo Html::endTag('span'); - } + echo Html::beginTag('i'); + echo Html::beginTag('small'); + echo StatusIssueWidget::widget(['status' => $issue->status]); + if (empty($issue->deadline) && $issue->status === \tracker\enum\IssueStatusEnum::TYPE_WORK) { + echo ' '; + echo Html::beginTag('span', ['class' => 'label label-success']); + echo Yii::t('TrackerIssuesModule.views', 'constantly'); + echo Html::endTag('span'); + } - if ($issue->deadline) { - echo ' '; - echo Html::beginTag('span', [ - 'data-toggle' => 'tooltip', - 'title' => Yii::t('TrackerIssuesModule.views', 'Deadline') - ]); - echo $formatter->asDate($issue->deadline); - echo Html::endTag('span'); - } + if ($issue->deadline) { + echo ' '; + echo Html::beginTag('span', [ + 'data-toggle' => 'tooltip', + 'title' => Yii::t('TrackerIssuesModule.views', 'Deadline') + ]); + echo $formatter->asDate($issue->deadline); + echo Html::endTag('span'); + } - echo ' '; - $user = $issue->content->getCreatedBy()->one(); - $createrImage = \humhub\modules\user\widgets\Image::widget([ - 'user' => $user, - 'width' => 18, - ]) . ' ' . $user->displayName; - echo $createrImage; - echo ' '; - echo Html::endTag('small'); - echo Html::endTag('i'); + echo ' '; + $user = $issue->content->getCreatedBy()->one(); + $createrImage = \humhub\modules\user\widgets\Image::widget([ + 'user' => $user, + 'width' => 18, + ]) . ' ' . $user->displayName; + echo $createrImage; + echo ' '; + echo Html::endTag('small'); + echo Html::endTag('i'); - if ($issue->getContent()->one()->canView(\Yii::$app->user->identity)) { - echo Html::beginTag('b'); - echo Html::a( - $issue->description ? Html::encode($issue->description) - : Yii::t('TrackerIssuesModule.base', 'Issue'), - $issue->content->getUrl(), - ['class' => 'text-info'] - ); - echo Html::endTag('b'); - } else { - echo Html::encode($issue->description); - } - echo Html::beginTag('ul', ['style' => 'list-style: none;']); - foreach ($issue->subtasks as $subtask) { - subtaskRender($subtask, $formatter); + if ($issue->getContent()->one()->canView(\Yii::$app->user->identity)) { + echo Html::beginTag('b'); + echo Html::a( + $issue->description ? Html::encode($issue->description) + : Yii::t('TrackerIssuesModule.base', 'Issue'), + $issue->content->getUrl(), + ['class' => 'text-info'] + ); + echo Html::endTag('b'); + } else { + echo Html::encode($issue->description); + } + echo Html::beginTag('ul', ['style' => 'list-style: none;']); + foreach ($issue->subtasks as $subtask) { + subtaskRender($subtask, $formatter); + } + echo Html::endTag('ul'); + echo Html::endTag('li'); } - echo Html::endTag('ul'); - echo Html::endTag('li'); - } - ?> -
    - $issue) : ?> - - -
- - - + ?> +
    + $issue) : ?> + + +
+ + + +
-
diff --git a/widgets/LinkToDocFileWidget.php b/widgets/LinkToDocFileWidget.php index c618d59..32dc75d 100644 --- a/widgets/LinkToDocFileWidget.php +++ b/widgets/LinkToDocFileWidget.php @@ -12,6 +12,7 @@ class LinkToDocFileWidget extends Widget { + /** @var Document|null */ public $document; public $icon = ''; public $text; @@ -24,14 +25,13 @@ public function run() return ''; } - if (!Document::find() + if (!$this->document->access_for_all && !Document::find() ->readable(\Yii::$app->user->identity, true) ->byId($this->document->id) ->exists()) { return \Yii::t('TrackerIssuesModule.views', 'Viewing the file is denied'); } - if (empty($this->text)) { $this->text = \Yii::t('TrackerIssuesModule.views', 'Show the file'); }