mirror of
https://github.com/flarum/core.git
synced 2025-08-15 12:54:47 +02:00
27
extensions/tags/js/admin/src/addTagChangePermission.js
Normal file
27
extensions/tags/js/admin/src/addTagChangePermission.js
Normal file
@@ -0,0 +1,27 @@
|
||||
import { extend } from 'flarum/extend';
|
||||
import PermissionGrid from 'flarum/components/PermissionGrid';
|
||||
import SettingDropdown from 'flarum/components/SettingDropdown';
|
||||
|
||||
export default function() {
|
||||
extend(PermissionGrid.prototype, 'startItems', items => {
|
||||
items.add('allowTagChange', {
|
||||
icon: 'tags',
|
||||
label: app.translator.trans('flarum-tags.admin.permissions.allow_edit_tags_label'),
|
||||
setting: () => {
|
||||
const minutes = parseInt(app.settings.allow_tag_change, 10);
|
||||
|
||||
return SettingDropdown.component({
|
||||
defaultLabel: minutes
|
||||
? app.translator.transChoice('core.admin.permissions_controls.allow_some_minutes_button', minutes, {count: minutes})
|
||||
: app.translator.trans('core.admin.permissions_controls.allow_indefinitely_button'),
|
||||
key: 'allow_tag_change',
|
||||
options: [
|
||||
{value: '-1', label: app.translator.trans('core.admin.permissions_controls.allow_indefinitely_button')},
|
||||
{value: '10', label: app.translator.trans('core.admin.permissions_controls.allow_ten_minutes_button')},
|
||||
{value: 'reply', label: app.translator.trans('core.admin.permissions_controls.allow_until_reply_button')}
|
||||
]
|
||||
});
|
||||
}
|
||||
}, 90);
|
||||
});
|
||||
}
|
@@ -3,6 +3,7 @@ import addTagsPermissionScope from 'flarum/tags/addTagsPermissionScope';
|
||||
import addTagPermission from 'flarum/tags/addTagPermission';
|
||||
import addTagsPane from 'flarum/tags/addTagsPane';
|
||||
import addTagsHomePageOption from 'flarum/tags/addTagsHomePageOption';
|
||||
import addTagChangePermission from 'flarum/tags/addTagChangePermission';
|
||||
|
||||
app.initializers.add('flarum-tags', app => {
|
||||
app.store.models.tags = Tag;
|
||||
@@ -11,4 +12,5 @@ app.initializers.add('flarum-tags', app => {
|
||||
addTagPermission();
|
||||
addTagsPane();
|
||||
addTagsHomePageOption();
|
||||
addTagChangePermission();
|
||||
});
|
||||
|
@@ -10,10 +10,12 @@
|
||||
|
||||
namespace Flarum\Tags\Access;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Flarum\Core\Access\AbstractPolicy;
|
||||
use Flarum\Core\Discussion;
|
||||
use Flarum\Core\User;
|
||||
use Flarum\Event\ScopeHiddenDiscussionVisibility;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\Tags\Tag;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -21,11 +23,26 @@ use Illuminate\Database\Query\Expression;
|
||||
|
||||
class DiscussionPolicy extends AbstractPolicy
|
||||
{
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $model = Discussion::class;
|
||||
|
||||
/**
|
||||
* @var SettingsRepositoryInterface
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param Gate $gate
|
||||
*/
|
||||
public function __construct(SettingsRepositoryInterface $settings)
|
||||
{
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -103,4 +120,25 @@ class DiscussionPolicy extends AbstractPolicy
|
||||
->where('discussions.id', new Expression('discussion_id'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* This method checks, if the user is still allowed to edit the tags
|
||||
* based on the configuration item
|
||||
* @param User $actor
|
||||
* @param Discussion $discussion
|
||||
* @return bool
|
||||
*/
|
||||
public function tag(User $actor, Discussion $discussion)
|
||||
{
|
||||
if ($discussion->start_user_id == $actor->id) {
|
||||
$allowEditTags = $this->settings->get('allow_tag_change');
|
||||
|
||||
if ($allowEditTags === '-1'
|
||||
|| ($allowEditTags === 'reply' && $discussion->participants_count <= 1)
|
||||
|| ($discussion->start_time->diffInMinutes(new Carbon) < $allowEditTags)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user