BUG Ensure that canCreate() context matches that respected by GridFieldAddNewButton

This commit is contained in:
Damian Mooyman 2019-10-04 11:23:24 +13:00
parent 1265f09f4f
commit f1594fd991
1 changed files with 33 additions and 18 deletions

View File

@ -18,6 +18,7 @@ use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\HasManyList;
use SilverStripe\ORM\ManyManyList;
use SilverStripe\ORM\RelationList;
use SilverStripe\ORM\SS_List;
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\ValidationResult;
@ -177,20 +178,6 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
return $controller->redirect($noActionURL, 302);
}
$canView = $this->record->canView();
$canEdit = $this->record->canEdit();
$canDelete = $this->record->canDelete();
$canCreate = $this->record->canCreate();
if (!$canView) {
$controller = $this->getToplevelController();
// TODO More friendly error
return $controller->httpError(403);
}
// Build actions
$actions = $this->getFormActions();
// If we are creating a new record in a has-many list, then
// pre-populate the record's foreign key.
if ($list instanceof HasManyList && !$this->record->isInDB()) {
@ -199,6 +186,12 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
$this->record->$key = $id;
}
if (!$this->record->canView()) {
$controller = $this->getToplevelController();
// TODO More friendly error
return $controller->httpError(403);
}
$fields = $this->component->getFields();
if (!$fields) {
$fields = $this->record->getCMSFields();
@ -218,20 +211,23 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
$this,
'ItemEditForm',
$fields,
$actions,
$this->getFormActions(),
$this->component->getValidator()
);
$form->loadDataFrom($this->record, $this->record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT);
if ($this->record->ID && !$canEdit) {
if ($this->record->ID && !$this->record->canEdit()) {
// Restrict editing of existing records
$form->makeReadonly();
// Hack to re-enable delete button if user can delete
if ($canDelete) {
if ($this->record->canDelete()) {
$form->Actions()->fieldByName('action_doDelete')->setReadonly(false);
}
} elseif (!$this->record->ID && !$canCreate) {
} elseif (
!$this->record->ID
&& !$this->record->canCreate(null, $this->getCreateContext())
) {
// Restrict creation of new records
$form->makeReadonly();
}
@ -271,6 +267,25 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
return $form;
}
/**
* Build context for verifying canCreate
* @see GridFieldAddNewButton::getHTMLFragments()
*
* @return array
*/
protected function getCreateContext()
{
$gridField = $this->gridField;
$context = [];
if ($gridField->getList() instanceof RelationList) {
$record = $gridField->getForm()->getRecord();
if ($record && $record instanceof DataObject) {
$context['Parent'] = $record;
}
}
return $context;
}
/**
* @return CompositeField Returns the right aligned toolbar group field along with its FormAction's
*/