Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dev 1043 viewable asset and entry condition rules #12266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/elements/conditions/assets/AssetCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ protected function conditionRuleTypes(): array
{
return array_merge(parent::conditionRuleTypes(), [
DateModifiedConditionRule::class,
EditableConditionRule::class,
FileSizeConditionRule::class,
FileTypeConditionRule::class,
FilenameConditionRule::class,
HasAltConditionRule::class,
HeightConditionRule::class,
SavableConditionRule::class,
UploaderConditionRule::class,
ViewableConditionRule::class,
VolumeConditionRule::class,
WidthConditionRule::class,
]);
Expand Down
99 changes: 6 additions & 93 deletions src/elements/conditions/assets/EditableConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,101 +2,14 @@

namespace craft\elements\conditions\assets;

use Craft;
use craft\base\conditions\BaseLightswitchConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;

/**
* Asset editable condition rule.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.3.0
* @deprecated in 4.4.0. [[SavableConditionRule]] should be used instead.
* @phpstan-ignore-next-line
*/
class EditableConditionRule extends BaseLightswitchConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('app', 'Editable');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
if (false) {
class EditableConditionRule
{
return [];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
$user = Craft::$app->getUser()->getIdentity();

if (!$user || $user->admin) {
$admin = $user?->admin;
if ((!$admin && $this->value) || ($admin && !$this->value)) {
$query->id(false);
}
return;
}

$fullySavableVolumes = [];
$restrictedVolumes = [];

foreach (Craft::$app->getVolumes()->getAllVolumes() as $volume) {
if ($user->can("savePeerAssets:$volume->uid")) {
$fullySavableVolumes[] = $volume->id;
} elseif ($user->can("saveAssets:$volume->uid")) {
$restrictedVolumes[] = $volume->id;
}
}

if (!$fullySavableVolumes && !$restrictedVolumes) {
if ($this->value) {
$query->id(false);
}
return;
}

$condition = [];

if ($fullySavableVolumes) {
$condition[] = ['assets.volumeId' => $fullySavableVolumes];
}

if ($restrictedVolumes) {
$condition[] = [
'assets.volumeId' => $restrictedVolumes,
'assets.uploaderId' => $user->id,
];
}

if (count($condition) === 1) {
$condition = reset($condition);
} else {
array_unshift($condition, 'or');
}

if (!$this->value) {
$condition = ['not', $condition];
}

$query->andWhere($condition);
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
$savable = Craft::$app->getElements()->canSave($element);
return $savable === $this->value;
}
}

class_alias(SavableConditionRule::class, EditableConditionRule::class);
53 changes: 53 additions & 0 deletions src/elements/conditions/assets/SavableConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace craft\elements\conditions\assets;

use Craft;
use craft\base\conditions\BaseLightswitchConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\AssetQuery;
use craft\elements\db\ElementQueryInterface;

/**
* Asset savable condition rule.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.4.0
*/
class SavableConditionRule extends BaseLightswitchConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('app', 'Savable');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return ['savable'];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var AssetQuery $query */
$query->savable($this->value);
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
$savable = Craft::$app->getElements()->canSave($element);
return $savable === $this->value;
}
}
53 changes: 53 additions & 0 deletions src/elements/conditions/assets/ViewableConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace craft\elements\conditions\assets;

use Craft;
use craft\base\conditions\BaseLightswitchConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\AssetQuery;
use craft\elements\db\ElementQueryInterface;

/**
* Asset viewable condition rule.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.4.0
*/
class ViewableConditionRule extends BaseLightswitchConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('app', 'Viewable');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
{
return ['editable'];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
/** @var AssetQuery $query */
$query->editable($this->value);
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
$viewable = Craft::$app->getElements()->canView($element);
return $viewable === $this->value;
}
}
100 changes: 6 additions & 94 deletions src/elements/conditions/entries/EditableConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,14 @@

namespace craft\elements\conditions\entries;

use Craft;
use craft\base\conditions\BaseLightswitchConditionRule;
use craft\base\ElementInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\elements\Entry;

/**
* Entry editable condition rule.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.3.0
* @deprecated in 4.4.0. [[SavableConditionRule]] should be used instead.
* @phpstan-ignore-next-line
*/
class EditableConditionRule extends BaseLightswitchConditionRule implements ElementConditionRuleInterface
{
/**
* @inheritdoc
*/
public function getLabel(): string
{
return Craft::t('app', 'Editable');
}

/**
* @inheritdoc
*/
public function getExclusiveQueryParams(): array
if (false) {
class EditableConditionRule
{
return [];
}

/**
* @inheritdoc
*/
public function modifyQuery(ElementQueryInterface $query): void
{
$user = Craft::$app->getUser()->getIdentity();

if (!$user || $user->admin) {
$admin = $user?->admin;
if ((!$admin && $this->value) || ($admin && !$this->value)) {
$query->id(false);
}
return;
}

$fullySavableSections = [];
$restrictedSections = [];

foreach (Craft::$app->getSections()->getAllSections() as $section) {
if ($user->can("savePeerEntries:$section->uid")) {
$fullySavableSections[] = $section->id;
} elseif ($user->can("saveEntries:$section->uid")) {
$restrictedSections[] = $section->id;
}
}

if (!$fullySavableSections && !$restrictedSections) {
if ($this->value) {
$query->id(false);
}
return;
}

$condition = [];

if ($fullySavableSections) {
$condition[] = ['entries.sectionId' => $fullySavableSections];
}

if ($restrictedSections) {
$condition[] = [
'entries.sectionId' => $restrictedSections,
'entries.authorId' => $user->id,
];
}

if (count($condition) === 1) {
$condition = reset($condition);
} else {
array_unshift($condition, 'or');
}

if (!$this->value) {
$condition = ['not', $condition];
}

$query->andWhere($condition);
}

/**
* @inheritdoc
*/
public function matchElement(ElementInterface $element): bool
{
$savable = Craft::$app->getElements()->canSave($element);
return $savable === $this->value;
}
}

class_alias(SavableConditionRule::class, EditableConditionRule::class);
3 changes: 2 additions & 1 deletion src/elements/conditions/entries/EntryCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ protected function conditionRuleTypes(): array
return array_merge(parent::conditionRuleTypes(), [
AuthorConditionRule::class,
AuthorGroupConditionRule::class,
EditableConditionRule::class,
ExpiryDateConditionRule::class,
LevelConditionRule::class,
PostDateConditionRule::class,
SavableConditionRule::class,
SectionConditionRule::class,
TypeConditionRule::class,
ViewableConditionRule::class,
]);
}
}
Loading