2016-02-20 06:12:41 +00:00
|
|
|
<?php namespace Backend\FormWidgets;
|
|
|
|
|
|
|
|
|
|
use Backend\Classes\FormWidgetBase;
|
|
|
|
|
use BackendAuth;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User/group permission editor
|
|
|
|
|
* This widget is used by the system internally on the System / Administrators pages.
|
|
|
|
|
*
|
|
|
|
|
* @package october\backend
|
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
|
*/
|
|
|
|
|
class PermissionEditor extends FormWidgetBase
|
|
|
|
|
{
|
2016-02-23 05:52:23 +00:00
|
|
|
public $mode;
|
|
|
|
|
|
2016-02-20 06:12:41 +00:00
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2016-02-20 06:12:41 +00:00
|
|
|
*/
|
|
|
|
|
public function init()
|
|
|
|
|
{
|
2016-02-23 05:52:23 +00:00
|
|
|
$this->fillFromConfig([
|
|
|
|
|
'mode'
|
|
|
|
|
]);
|
2016-02-20 06:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2016-02-20 06:12:41 +00:00
|
|
|
*/
|
|
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
$this->prepareVars();
|
|
|
|
|
return $this->makePartial('permissioneditor');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepares the list data
|
|
|
|
|
*/
|
|
|
|
|
public function prepareVars()
|
|
|
|
|
{
|
2016-02-23 05:52:23 +00:00
|
|
|
$permissionsData = $this->formField->getValueFromData($this->model);
|
|
|
|
|
if (!is_array($permissionsData)) {
|
|
|
|
|
$permissionsData = [];
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-27 20:50:06 +00:00
|
|
|
$this->vars['checkboxMode'] = $this->getControlMode() === 'checkbox';
|
2017-07-10 02:24:17 +00:00
|
|
|
$this->vars['permissions'] = $this->getFilteredPermissions();
|
2016-11-27 20:50:06 +00:00
|
|
|
$this->vars['baseFieldName'] = $this->getFieldName();
|
2016-02-23 05:52:23 +00:00
|
|
|
$this->vars['permissionsData'] = $permissionsData;
|
|
|
|
|
$this->vars['field'] = $this->formField;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2016-02-23 05:52:23 +00:00
|
|
|
*/
|
|
|
|
|
public function getSaveValue($value)
|
|
|
|
|
{
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return [];
|
2016-02-20 06:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-03-15 19:26:14 +00:00
|
|
|
* @inheritDoc
|
2016-02-20 06:12:41 +00:00
|
|
|
*/
|
|
|
|
|
protected function loadAssets()
|
|
|
|
|
{
|
|
|
|
|
$this->addCss('css/permissioneditor.css', 'core');
|
2016-02-24 05:44:05 +00:00
|
|
|
$this->addJs('js/permissioneditor.js', 'core');
|
2016-02-20 06:12:41 +00:00
|
|
|
}
|
2016-02-23 05:52:23 +00:00
|
|
|
|
|
|
|
|
protected function getControlMode()
|
|
|
|
|
{
|
|
|
|
|
return strlen($this->mode) ? $this->mode : 'radio';
|
|
|
|
|
}
|
2017-07-10 02:24:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the available permissions; removing those that the logged-in user does not have access to
|
|
|
|
|
*
|
|
|
|
|
* @return array The permissions that the logged-in user does have access to
|
|
|
|
|
*/
|
|
|
|
|
protected function getFilteredPermissions()
|
|
|
|
|
{
|
|
|
|
|
$permissions = BackendAuth::listTabbedPermissions();
|
|
|
|
|
$user = BackendAuth::getUser();
|
|
|
|
|
foreach ($permissions as $tab => $permissionsArray) {
|
|
|
|
|
foreach ($permissionsArray as $index => $permission) {
|
|
|
|
|
if (!$user->hasAccess($permission->code)) {
|
|
|
|
|
unset($permissionsArray[$index]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($permissionsArray)) {
|
|
|
|
|
unset($permissions[$tab]);
|
|
|
|
|
} else {
|
|
|
|
|
$permissions[$tab] = $permissionsArray;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $permissions;
|
|
|
|
|
}
|
2017-04-24 11:38:19 +00:00
|
|
|
}
|