Skip to content

Commit 327abb2

Browse files
committed
Add more granular notes add/delete permissions
Closes #1
1 parent 80ef392 commit 327abb2

File tree

7 files changed

+146
-25
lines changed

7 files changed

+146
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## [Unreleased] 1.0.3
2+
### Added
3+
- Add more granular notes add/delete permissions (Closes #1)
4+
25
### Fixed
36
- Fix note meta only returning full name if user has one when adding notes (Fixes #2)
47

src/Field.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@
2121
class Field extends \craft\base\Field
2222
{
2323

24+
const ADD_ANY = 'addAny';
25+
const ADD_PERMISSION = 'addPermission';
26+
const ADD_NONE = 'addNone';
27+
const DELETE_ANY = 'deleteAny';
28+
const DELETE_PERMISSION = 'deletePermission';
29+
const DELETE_NONE = 'deleteNone';
30+
2431
public static $table = '{{%notes}}';
2532
public static $dateFormat = 'M j, Y g:ia';
2633

27-
/** @var bool Will allow the deleting of notes if true */
28-
public $allowDeleting = false;
34+
/** @var bool|string Manage who can add notes */
35+
public $allowAdding = self::ADD_PERMISSION;
36+
37+
/** @var bool|string Manage who can delete notes */
38+
public $allowDeleting = self::DELETE_PERMISSION;
2939

3040
public static function displayName (): string
3141
{
@@ -58,8 +68,16 @@ public function normalizeValue ($value, ElementInterface $element = null)
5868

5969
public function getSettingsHtml ()
6070
{
71+
// 1.0.2 back-compat
72+
$allowDeleting = $this->allowDeleting;
73+
if ($allowDeleting === true) $allowDeleting = self::DELETE_ANY;
74+
elseif ($allowDeleting === false) $allowDeleting = self::DELETE_NONE;
75+
6176
return Craft::$app->getView()->renderTemplate('notes/settings', [
62-
'allowDeleting' => $this->allowDeleting,
77+
'allowAdding' => $this->allowAdding,
78+
'allowDeleting' => $allowDeleting,
79+
'addOpts' => self::_addOpts(),
80+
'deleteOpts' => self::_deleteOpts(),
6381
]);
6482
}
6583

@@ -75,8 +93,64 @@ protected function inputHtml ($value, ElementInterface $element = null): string
7593
'ns' => $this->handle,
7694
'element' => $element,
7795
'notes' => $value,
78-
'allowDeleting' => $this->allowDeleting,
96+
'allowAdding' => $this->_canAdd(),
97+
'allowDeleting' => $this->_canDelete(),
7998
]);
8099
}
81100

101+
// Helpers
102+
// =========================================================================
103+
104+
private static function _addOpts ()
105+
{
106+
return [
107+
self::ADD_ANY => Craft::t('notes', 'All users'),
108+
self::ADD_PERMISSION => Craft::t('notes', 'User permission'),
109+
self::ADD_NONE => Craft::t('notes', 'Disabled'),
110+
];
111+
}
112+
113+
private static function _deleteOpts ()
114+
{
115+
return [
116+
self::DELETE_ANY => Craft::t('notes', 'All users'),
117+
self::DELETE_PERMISSION => Craft::t('notes', 'User permission'),
118+
self::DELETE_NONE => Craft::t('notes', 'Disabled'),
119+
];
120+
}
121+
122+
private function _canAdd ()
123+
{
124+
switch ($this->allowAdding)
125+
{
126+
case self::ADD_PERMISSION:
127+
return Craft::$app->user->checkPermission('addNotes');
128+
case self::ADD_NONE:
129+
return false;
130+
default:
131+
case self::ADD_ANY:
132+
return true;
133+
}
134+
}
135+
136+
private function _canDelete ()
137+
{
138+
$user = Craft::$app->getUser();
139+
140+
switch ($this->allowDeleting)
141+
{
142+
case self::DELETE_PERMISSION:
143+
return $user->checkPermission('deleteAllNotes')
144+
? true
145+
: $user->checkPermission('deleteOwnNotes')
146+
? 'own'
147+
: false;
148+
case self::DELETE_NONE:
149+
return false;
150+
default:
151+
case self::DELETE_ANY:
152+
return true;
153+
}
154+
}
155+
82156
}

src/Notes.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
namespace ether\notes;
1010

11+
use Craft;
1112
use craft\base\Plugin;
1213
use craft\events\RegisterComponentTypesEvent;
14+
use craft\events\RegisterUserPermissionsEvent;
1315
use craft\services\Fields;
16+
use craft\services\UserPermissions;
1417
use yii\base\Event;
1518

1619
/**
@@ -36,11 +39,32 @@ public function init ()
3639
Fields::EVENT_REGISTER_FIELD_TYPES,
3740
[$this, 'onRegisterFieldTypes']
3841
);
42+
43+
Event::on(
44+
UserPermissions::class,
45+
UserPermissions::EVENT_REGISTER_PERMISSIONS,
46+
[$this, 'onRegisterPermissions']
47+
);
3948
}
4049

4150
public function onRegisterFieldTypes (RegisterComponentTypesEvent $event)
4251
{
4352
$event->types[] = Field::class;
4453
}
4554

55+
public function onRegisterPermissions (RegisterUserPermissionsEvent $event)
56+
{
57+
$event->permissions['Notes'] = [
58+
'addNotes' => [
59+
'label' => Craft::t('notes', 'Add notes'),
60+
],
61+
'deleteOwnNotes' => [
62+
'label' => Craft::t('notes', 'Delete own notes'),
63+
],
64+
'deleteAllNotes' => [
65+
'label' => Craft::t('notes', 'Delete all notes'),
66+
],
67+
];
68+
}
69+
4670
}

src/templates/field.twig

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{% namespace ns %}
2-
{% import "_includes/forms" as forms %}
2+
{% if allowAdding %}
3+
{% import "_includes/forms" as forms %}
34

4-
{{ forms.textarea({
5-
placeholder: 'Add a note'|t('notes'),
6-
class: 'notes-input',
7-
id: 'input',
8-
})|attr({
9-
'oninput': "this.style.height = '';this.style.height = this.scrollHeight + 2 + 'px';",
10-
}) }}
5+
{{ forms.textarea({
6+
placeholder: 'Add a note'|t('notes'),
7+
class: 'notes-input',
8+
id: 'input',
9+
})|attr({
10+
'oninput': "this.style.height = '';this.style.height = this.scrollHeight + 2 + 'px';",
11+
}) }}
1112

12-
<div>
13-
<button class="btn notes-add" type="button" id="add">+ {{ 'Add Note'|t('notes') }}</button>
14-
<div class="spinner hidden" id="spin"></div>
15-
</div>
13+
<div>
14+
<button class="btn notes-add" type="button" id="add">+ {{ 'Add Note'|t('notes') }}</button>
15+
<div class="spinner hidden" id="spin"></div>
16+
</div>
17+
{% endif %}
1618

1719
<ul class="notes" id="notes">
1820
{% for note in notes %}

src/templates/settings.twig

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{% import '_includes/forms' as forms %}
22

3-
{{ forms.lightswitchField({
3+
{{ forms.selectField({
44
first: true,
5-
label: 'Allow Deleting'|t('notes'),
6-
instructions: 'Allow users to delete notes'|t('notes'),
7-
id: 'allowDeleting',
5+
label: 'Adding'|t('notes'),
6+
instructions: 'Manage who can add notes'|t('notes'),
7+
name: 'allowAdding',
8+
options: addOpts,
9+
value: allowAdding,
10+
}) }}
11+
12+
{{ forms.selectField({
13+
label: 'Deleting'|t('notes'),
14+
instructions: 'Manage who can delete notes'|t('notes'),
815
name: 'allowDeleting',
9-
on: allowDeleting
16+
options: deleteOpts,
17+
value: allowDeleting,
1018
}) }}

src/translations/en/notes.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
88

99
return [
1010
'Notes' => 'Notes',
11-
'Allow Deleting' => 'Allow Deleting',
12-
'Allow users to delete notes' => 'Allow users to delete notes',
1311
'You must save the element before you can add notes!' => 'You must save the element before you can add notes!',
1412
'Add a note' => 'Add a note',
1513
'Add Note' => 'Add Note',
14+
'Add notes' => 'Add notes',
15+
'Delete own notes' => 'Delete own notes',
16+
'Delete all notes' => 'Delete all notes',
17+
18+
'All users' => 'All users',
19+
'User permission' => 'User permission',
20+
'Disabled' => 'Disabled',
21+
22+
'Adding' => 'Adding',
23+
'Deleting' => 'Deleting',
24+
'Manage who can add notes' => 'Manage who can add notes',
25+
'Manage who can delete notes' => 'Manage who can delete notes',
1626
];

src/web/notes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function NotesField ([ns, siteId, elementId, userId, allowDeleting]) {
3434
spin.classList.add('hidden');
3535
};
3636

37-
input.addEventListener('keydown', e => {
37+
input && input.addEventListener('keydown', e => {
3838
if (!(e.key === 'Enter' && e.metaKey)) return;
3939
save();
4040
});
@@ -43,7 +43,7 @@ function NotesField ([ns, siteId, elementId, userId, allowDeleting]) {
4343
a.addEventListener('click', NotesField.delete);
4444
});
4545

46-
add.addEventListener('click', save);
46+
add && add.addEventListener('click', save);
4747
}
4848

4949
NotesField.delete = async e => {

0 commit comments

Comments
 (0)