From d8a8ddc61dc02ceed710e4e8b6ec7ee71a8c1e8c Mon Sep 17 00:00:00 2001 From: pjayme Date: Wed, 13 Mar 2019 09:20:51 +1300 Subject: [PATCH 1/2] re-added can* methods for EditableOption field --- .../EditableFormField/EditableOption.php | 89 ++++++++++++++++++- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/code/Model/EditableFormField/EditableOption.php b/code/Model/EditableFormField/EditableOption.php index f6d4c44..3cd0e2f 100644 --- a/code/Model/EditableFormField/EditableOption.php +++ b/code/Model/EditableFormField/EditableOption.php @@ -2,8 +2,10 @@ namespace SilverStripe\UserForms\Model\EditableFormField; +use SilverStripe\Control\Controller; use SilverStripe\Core\Convert; use SilverStripe\ORM\DataObject; +use SilverStripe\Security\Member; use SilverStripe\Versioned\Versioned; /** @@ -30,12 +32,12 @@ class EditableOption extends DataObject ]; private static $extensions = [ - Versioned::class . "('Stage', 'Live')" + Versioned::class . "('Stage', 'Live')", ]; private static $summary_fields = [ 'Title', - 'Default' + 'Default', ]; protected static $allow_empty_values = false; @@ -94,4 +96,87 @@ class EditableOption extends DataObject parent::onBeforeWrite(); } + + /** + * @param Member $member + * + * @return boolean + */ + public function canEdit($member = null) + { + return $this->Parent()->canEdit($member); + } + /** + * @param Member $member + * + * @return boolean + */ + public function canDelete($member = null) + { + return $this->canEdit($member); + } + + /** + * @param Member $member + * @return bool + */ + public function canView($member = null) + { + return $this->Parent()->canView($member); + } + + /** + * Return whether a user can create an object of this type + * + * @param Member $member + * @param array $context Virtual parameter to allow context to be passed in to check + * @return bool + */ + public function canCreate($member = null, $context = []) + { + // Check parent page + $parent = $this->getCanCreateContext(func_get_args()); + if ($parent) { + return $parent->canEdit($member); + } + // Fall back to secure admin permissions + return parent::canCreate($member); + } + + /** + * @param Member $member + * @return bool + */ + public function canPublish($member = null) + { + return $this->canEdit($member); + } + /** + * @param Member $member + * @return bool + */ + public function canUnpublish($member = null) + { + return $this->canDelete($member); + } + + /** + * Helper method to check the parent for this object + * + * @param array $args List of arguments passed to canCreate + * @return DataObject Some parent dataobject to inherit permissions from + */ + protected function getCanCreateContext($args) + { + // Inspect second parameter to canCreate for a 'Parent' context + if (isset($args[1]['Parent'])) { + return $args[1]['Parent']; + } + // Hack in currently edited page if context is missing + if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { + return Controller::curr()->currentPage(); + } + // No page being edited + return null; + } } From b49d38ff0f68eaaceb2e95497b44011adc67c9a5 Mon Sep 17 00:00:00 2001 From: pjayme Date: Wed, 13 Mar 2019 10:18:41 +1300 Subject: [PATCH 2/2] updated canCreate function to check for Parent object --- .../EditableFormField/EditableOption.php | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/code/Model/EditableFormField/EditableOption.php b/code/Model/EditableFormField/EditableOption.php index 3cd0e2f..5bac3dc 100644 --- a/code/Model/EditableFormField/EditableOption.php +++ b/code/Model/EditableFormField/EditableOption.php @@ -2,7 +2,6 @@ namespace SilverStripe\UserForms\Model\EditableFormField; -use SilverStripe\Control\Controller; use SilverStripe\Core\Convert; use SilverStripe\ORM\DataObject; use SilverStripe\Security\Member; @@ -134,11 +133,12 @@ class EditableOption extends DataObject */ public function canCreate($member = null, $context = []) { - // Check parent page - $parent = $this->getCanCreateContext(func_get_args()); + // Check parent object + $parent = $this->Parent(); if ($parent) { - return $parent->canEdit($member); + return $parent->canCreate($member); } + // Fall back to secure admin permissions return parent::canCreate($member); } @@ -159,24 +159,4 @@ class EditableOption extends DataObject { return $this->canDelete($member); } - - /** - * Helper method to check the parent for this object - * - * @param array $args List of arguments passed to canCreate - * @return DataObject Some parent dataobject to inherit permissions from - */ - protected function getCanCreateContext($args) - { - // Inspect second parameter to canCreate for a 'Parent' context - if (isset($args[1]['Parent'])) { - return $args[1]['Parent']; - } - // Hack in currently edited page if context is missing - if (Controller::has_curr() && Controller::curr() instanceof CMSMain) { - return Controller::curr()->currentPage(); - } - // No page being edited - return null; - } }