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

allow to limit source editing by user groups on per-field basis #318

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
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
50 changes: 49 additions & 1 deletion src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use craft\elements\Asset;
use craft\elements\Category;
use craft\elements\Entry;
use craft\elements\User;
use craft\errors\InvalidHtmlTagException;
use craft\helpers\ArrayHelper;
use craft\helpers\Html;
Expand Down Expand Up @@ -146,6 +147,13 @@ public static function textPartLanguage(): array
*/
public bool $enableSourceEditingForNonAdmins = false;

/**
* @var string|array|null User groups whose members should be able to edit the source even if they're not admin users
* if the $enableSourceEditingForNonAdmins is on.
* @since 3.11.0
*/
public string|array|null $sourceEditingGroups = '*';

/**
* @var bool Whether to show volumes the user doesn’t have permission to view.
* @since 1.2.0
Expand Down Expand Up @@ -234,6 +242,16 @@ public function getSettingsHtml(): ?string
{
$view = Craft::$app->getView();

$userGroupOptions = [];
foreach (Craft::$app->getUserGroups()->getAllGroups() as $group) {
if ($group->can('accessCp')) {
$userGroupOptions[] = [
'label' => $group->name,
'value' => $group->uid,
];
}
}

$volumeOptions = [];
foreach (Craft::$app->getVolumes()->getAllVolumes() as $volume) {
if ($volume->getFs()->hasUrls) {
Expand All @@ -254,6 +272,7 @@ public function getSettingsHtml(): ?string

return $view->renderTemplate('ckeditor/_field-settings.twig', [
'field' => $this,
'userGroupOptions' => $userGroupOptions,
'purifierConfigOptions' => $this->configOptions('htmlpurifier'),
'volumeOptions' => $volumeOptions,
'transformOptions' => $transformOptions,
Expand Down Expand Up @@ -303,7 +322,7 @@ protected function inputHtml(mixed $value, ElementInterface $element = null): st
// Toolbar cleanup
$toolbar = array_merge($ckeConfig->toolbar);

if (!$this->enableSourceEditingForNonAdmins && !Craft::$app->getUser()->getIsAdmin()) {
if (!$this->isSourceEditingAllowed(Craft::$app->getUser()->getIdentity())) {
ArrayHelper::removeValue($toolbar, 'sourceEditing');
}

Expand Down Expand Up @@ -549,6 +568,35 @@ protected function prepValueForInput($value, ?ElementInterface $element): string
return parent::prepValueForInput($value, $element);
}

/**
* Returns if user belongs to a group whose members are allowed to edit source even if they're not admins
* @param User $user
* @return bool
*/
private function isSourceEditingAllowed(User $user): bool
{
if ($user->admin) {
return true;
}

if (!$this->enableSourceEditingForNonAdmins) {
return false;
}

$allowedGroups = $this->sourceEditingGroups;
if ($allowedGroups === '*') {
return true;
}

if (is_string($allowedGroups)) {
$allowedGroups = [$allowedGroups];
}

$userGroups = array_keys(Collection::make($user->getGroups())->keyBy('uid')->all());

return !empty(array_intersect($allowedGroups, $userGroups));
}

/**
* @inheritdoc
*/
Expand Down
1 change: 0 additions & 1 deletion src/console/controllers/ConvertController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace craft\ckeditor\console\controllers;

use Composer\Util\Platform;
use Craft;
use craft\ckeditor\CkeConfig;
use craft\ckeditor\CkeConfigs;
Expand Down
14 changes: 14 additions & 0 deletions src/templates/_field-settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,21 @@
name: 'enableSourceEditingForNonAdmins',
label: 'Show the “Source” button for non-admin users?'|t('ckeditor'),
on: field.enableSourceEditingForNonAdmins,
toggle: 'source-editing-groups'
}) }}
{% if userGroupOptions|length %}
<div id="source-editing-groups">
{{ forms.checkboxSelectField({
id: 'sourceEditingGroups',
name: 'sourceEditingGroups',
label: 'Available User Groups'|t('ckeditor'),
instructions: 'The user groups whose members should see the “Source” button even if they’re not admins.'|t('ckeditor'),
options: userGroupOptions,
values: field.sourceEditingGroups,
showAllOption: userGroupOptions|length ? true : false
}) }}
</div>
{% endif %}

<hr>

Expand Down
2 changes: 2 additions & 0 deletions src/translations/en/ckeditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

return [
'Advanced' => 'Advanced',
'Available User Groups' => 'Available User Groups',
'Available Transforms' => 'Available Transforms',
'CKEditor Config' => 'CKEditor Config',
'CKEditor Configs' => 'CKEditor Configs',
Expand Down Expand Up @@ -38,6 +39,7 @@
'The default transform that should be applied when inserting an image.' => 'The default transform that should be applied when inserting an image.',
'The transforms that should be available when inserting images.' => 'The transforms that should be available when inserting images.',
'The type of column this field should get in the database.' => 'The type of column this field should get in the database.',
'The user groups whose members should see the “Source” button even if they’re not admins.' => 'The user groups whose members should see the “Source” button even if they’re not admins.',
'Toolbar' => 'Toolbar',
'View available settings' => 'View available settings',
'Word Limit' => 'Word Limit',
Expand Down
Loading