Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
feat: Ability to open access to the document file for all users
Browse files Browse the repository at this point in the history
  • Loading branch information
githubjeka committed Nov 11, 2020
1 parent b5abe37 commit 18e7f34
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 115 deletions.
16 changes: 11 additions & 5 deletions controllers/DocumentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 3 additions & 0 deletions controllers/requests/DocumentRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class DocumentRequest extends \yii\base\Model
{
public $name;
public $description;
public $access;
public $from;
public $registeredAt;
public $to;
Expand All @@ -35,6 +36,7 @@ public function rules()
[['name', 'from', 'to', 'number'], 'string', 'max' => 255],
['file', 'file', 'skipOnEmpty' => false,],
['description', 'string'],
['access', 'boolean'],
];
}

Expand All @@ -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'),
];
}
}
1 change: 1 addition & 0 deletions controllers/services/DocumentCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions controllers/services/DocumentEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -67,6 +68,7 @@ public function save()
'from',
'to',
'category',
'access',
])) {
return false;
}
Expand All @@ -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) {
Expand Down
61 changes: 33 additions & 28 deletions models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'],
Expand All @@ -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'),
];
}

Expand 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
*/
Expand All @@ -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
*/
Expand All @@ -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());
}
}
2 changes: 2 additions & 0 deletions views/document/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

<?= $form->field($documentRequest, 'file')->fileInput() ?>

<?= $form->field($documentRequest, 'access')->checkbox(); ?>

<?= $form->field($documentRequest, 'receivers')
->widget(\humhub\modules\user\widgets\UserPickerField::class,
[
Expand Down
2 changes: 2 additions & 0 deletions views/document/form_change_info.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

<?= $form->errorSummary($requestModel); ?>

<?= $form->field($requestModel, 'access')->checkbox(); ?>

<?= $form->field($requestModel, 'category')
->dropDownList(
ArrayHelper::map(Document::categories(), 'id', 'name'),
Expand Down
Loading

0 comments on commit 18e7f34

Please sign in to comment.