Skip to content
Open
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/user.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
$_lang['role_desc_name'] = 'A name for the Role, such as Content Editor, Publisher, System Administrator, etc.';
$_lang['role_desc_description'] = 'A short description of the Role.';
$_lang['role_err_ae'] = 'A role already exists with that name.';
$_lang['role_err_authority_exists'] = 'The specified Authority level is being used by another Role. (Authority levels must be unique.)';
$_lang['role_err_duplicate'] = 'An error occurred while duplicating the role.';
$_lang['role_err_has_users'] = 'There are users with this role. It cannot be deleted.';
$_lang['role_err_nf'] = 'Role not found.';
Expand Down
17 changes: 16 additions & 1 deletion core/src/Revolution/Processors/Security/Role/Create.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

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

namespace MODX\Revolution\Processors\Security\Role;


use MODX\Revolution\Processors\Model\CreateProcessor;
use MODX\Revolution\modUserGroupRole;

Expand All @@ -31,13 +31,18 @@ class Create extends CreateProcessor
public function beforeSave()
{
$name = $this->getProperty('name');
$authority = (int)$this->getProperty('authority', 0);

if (empty($name)) {
$this->addFieldError('name', $this->modx->lexicon('role_err_ns_name'));
}

if ($this->alreadyExists($name)) {
$this->addFieldError('name', $this->modx->lexicon('role_err_ae'));
}
if ($this->authorityExists($authority)) {
$this->addFieldError('authority', $this->modx->lexicon('role_err_authority_exists'));
}

return parent::beforeSave();
}
Expand All @@ -51,4 +56,14 @@ public function alreadyExists($name)
{
return $this->modx->getCount(modUserGroupRole::class, ['name' => $name]) > 0;
}

/**
* Check whether the specified authority level already exists
* @param int $authority
* @return boolean
*/
public function authorityExists(int $authority)
{
return $this->modx->getCount(modUserGroupRole::class, ['authority' => $authority]) > 0;
}
}
36 changes: 35 additions & 1 deletion core/src/Revolution/Processors/Security/Role/Update.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

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

namespace MODX\Revolution\Processors\Security\Role;


use MODX\Revolution\Processors\Model\UpdateProcessor;
use MODX\Revolution\modUserGroupRole;

Expand All @@ -32,10 +32,44 @@ class Update extends UpdateProcessor
public function beforeSave()
{
$name = $this->getProperty('name');
$authority = (int)$this->getProperty('authority', 0);

if (empty($name)) {
$this->addFieldError('name', $this->modx->lexicon('role_err_ns_name'));
}
if ($this->alreadyExists($name)) {
$this->addFieldError('name', $this->modx->lexicon('role_err_ae'));
}
if ($this->authorityExists($authority)) {
$this->addFieldError('authority', $this->modx->lexicon('role_err_authority_exists'));
}

return parent::beforeSave();
}

/**
* Check to see if a role already exists with the specified name
* @param string $name
* @return boolean
*/
public function alreadyExists($name)
{
return $this->modx->getCount(
modUserGroupRole::class,
['name' => $name, 'id:!=' => $this->getProperty('id')]
) > 0;
}

/**
* Check whether the specified authority level already exists
* @param int $authority
* @return boolean
*/
public function authorityExists(int $authority)
{
return $this->modx->getCount(
modUserGroupRole::class,
['authority' => $authority, 'id:!=' => $this->getProperty('id')]
) > 0;
}
}
3 changes: 2 additions & 1 deletion manager/assets/modext/widgets/security/modx.grid.role.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ MODx.window.CreateRole = function(config = {}) {
action: 'Security/Role/Create',
formDefaults: {
allowBlank: false,
anchor: '100%'
anchor: '100%',
msgTarget: 'under'
},
fields: [{
name: 'name',
Expand Down