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

Make “root” a reserved (restricted) key for Contexts #16475

Merged
merged 3 commits into from
Feb 16, 2024
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
1 change: 1 addition & 0 deletions core/lexicon/en/context.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
$_lang['context_err_ns'] = 'Context not specified.';
$_lang['context_err_ns_key'] = 'Please specify a valid key for the Context.';
$_lang['context_err_remove'] = 'An error occurred while trying to delete the Context.';
$_lang['context_err_reserved'] = 'The Context key you chose is reserved for system use only. Please specify a different key.';
$_lang['context_err_save'] = 'An error occurred while saving the Context.';
$_lang['context_id'] = 'Ctx ID';
$_lang['context_key'] = 'Context Key';
Expand Down
21 changes: 15 additions & 6 deletions core/src/Revolution/Processors/Context/Create.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of the MODX Revolution package.
*
Expand All @@ -10,7 +11,6 @@

namespace MODX\Revolution\Processors\Context;


use MODX\Revolution\modAccessContext;
use MODX\Revolution\modAccessPolicy;
use MODX\Revolution\modContext;
Expand All @@ -35,15 +35,24 @@
public function beforeSave()
{
$key = $this->getProperty('key');
if (empty($key)) {
$this->addFieldError('key', $this->modx->lexicon('context_err_ns_key'));

switch (true) {
case empty($key):
$this->addFieldError('key', $this->modx->lexicon('context_err_ns_key'));
break;

Check warning on line 42 in core/src/Revolution/Processors/Context/Create.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Context/Create.php#L41-L42

Added lines #L41 - L42 were not covered by tests
case in_array(strtolower($key), $this->classKey::RESERVED_KEYS):
$this->addFieldError('key', $this->modx->lexicon('context_err_reserved'));
break;

Check warning on line 45 in core/src/Revolution/Processors/Context/Create.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Context/Create.php#L44-L45

Added lines #L44 - L45 were not covered by tests
case $this->alreadyExists($key):
$this->addFieldError('key', $this->modx->lexicon('context_err_ae'));

Check warning on line 47 in core/src/Revolution/Processors/Context/Create.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Context/Create.php#L47

Added line #L47 was not covered by tests
// no default
}
if ($this->alreadyExists($key)) {
$this->addFieldError('key', $this->modx->lexicon('context_err_ae'));
if ($this->hasErrors()) {
return false;

Check warning on line 51 in core/src/Revolution/Processors/Context/Create.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Context/Create.php#L51

Added line #L51 was not covered by tests
}
$this->object->set('key', $key);

return !$this->hasErrors();
return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/Revolution/Processors/Context/GetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function prepareRow(xPDOObject $object)
if ($this->canEdit) {
$contextArray['perm'][] = 'pedit';
}
if (!in_array($object->get('key'), ['mgr', 'web']) && $this->canRemove) {
if (!in_array($object->get('key'), $this->classKey::RESERVED_KEYS) && $this->canRemove) {
$contextArray['perm'][] = 'premove';
}

Expand Down
7 changes: 7 additions & 0 deletions core/src/Revolution/modContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
*/
class modContext extends modAccessibleObject
{
/**
* A set of Context keys that are restricted to system use only
*
* @var array RESERVED_KEYS
*/
public const RESERVED_KEYS = ['mgr', 'web', 'root'];

/**
* An array of configuration options for this context
*
Expand Down
Loading