mirror of
https://github.com/flarum/framework.git
synced 2025-02-17 00:12:45 +08:00
feat(permission): add the no one permission
One thing that has been very frustrating while developing extensions is that you cannot easily create a feature that you would like to allow disabling through the permission grid. This Proof of Concept PR is a question, whether I'm the only one running into this limitation for Flarum. It does: - introduce a fake permission, similar to Guest - removes the whole permission entry from the db when selected - restores the permission visually on the frontend It has to: - confirm admins also cannot use the permission if selected - not break v1.x functionality or be moved to v2.0 - appeal to core and community
This commit is contained in:
parent
bddc9d96f2
commit
7dcbbf29ab
30362
framework/core/js/dist/admin.js
generated
vendored
30362
framework/core/js/dist/admin.js
generated
vendored
File diff suppressed because one or more lines are too long
2
framework/core/js/dist/admin.js.map
generated
vendored
2
framework/core/js/dist/admin.js.map
generated
vendored
File diff suppressed because one or more lines are too long
37513
framework/core/js/dist/forum.js
generated
vendored
37513
framework/core/js/dist/forum.js
generated
vendored
File diff suppressed because one or more lines are too long
2
framework/core/js/dist/forum.js.map
generated
vendored
2
framework/core/js/dist/forum.js.map
generated
vendored
File diff suppressed because one or more lines are too long
|
@ -48,15 +48,18 @@ export default class PermissionDropdown<CustomAttrs extends IPermissionDropdownA
|
||||||
view(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
view(vnode: Mithril.Vnode<CustomAttrs, this>) {
|
||||||
const children = [];
|
const children = [];
|
||||||
|
|
||||||
let groupIds = app.data.permissions[this.attrs.permission] || [];
|
let groupIds = app.data.permissions[this.attrs.permission] || [Group.NO_ONE_ID];
|
||||||
|
|
||||||
groupIds = filterByRequiredPermissions(groupIds, this.attrs.permission);
|
groupIds = filterByRequiredPermissions(groupIds, this.attrs.permission);
|
||||||
|
|
||||||
|
const no_one = groupIds.includes(Group.NO_ONE_ID);
|
||||||
const everyone = groupIds.includes(Group.GUEST_ID);
|
const everyone = groupIds.includes(Group.GUEST_ID);
|
||||||
const members = groupIds.includes(Group.MEMBER_ID);
|
const members = groupIds.includes(Group.MEMBER_ID);
|
||||||
const adminGroup = app.store.getById<Group>('groups', Group.ADMINISTRATOR_ID)!;
|
const adminGroup = app.store.getById<Group>('groups', Group.ADMINISTRATOR_ID)!;
|
||||||
|
|
||||||
if (everyone) {
|
if (no_one) {
|
||||||
|
this.attrs.label = Badge.component({ icon: 'fas fa-user-slash' });
|
||||||
|
} else if (everyone) {
|
||||||
this.attrs.label = Badge.component({ icon: 'fas fa-globe' });
|
this.attrs.label = Badge.component({ icon: 'fas fa-globe' });
|
||||||
} else if (members) {
|
} else if (members) {
|
||||||
this.attrs.label = Badge.component({ icon: 'fas fa-user' });
|
this.attrs.label = Badge.component({ icon: 'fas fa-user' });
|
||||||
|
@ -65,6 +68,18 @@ export default class PermissionDropdown<CustomAttrs extends IPermissionDropdownA
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.showing) {
|
if (this.showing) {
|
||||||
|
if (this.attrs.allowNoOne) {
|
||||||
|
children.push(
|
||||||
|
Button.component(
|
||||||
|
{
|
||||||
|
icon: no_one ? 'fas fa-check' : true,
|
||||||
|
onclick: () => this.save([Group.NO_ONE_ID]),
|
||||||
|
disabled: this.isGroupDisabled(Group.NO_ONE_ID),
|
||||||
|
},
|
||||||
|
[Badge.component({ icon: 'fas fa-user-slash' }), ' ', app.translator.trans('core.admin.permissions_controls.no_one_button')]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
if (this.attrs.allowGuest) {
|
if (this.attrs.allowGuest) {
|
||||||
children.push(
|
children.push(
|
||||||
Button.component(
|
Button.component(
|
||||||
|
|
|
@ -12,6 +12,7 @@ export interface PermissionConfig {
|
||||||
icon: string;
|
icon: string;
|
||||||
label: Mithril.Children;
|
label: Mithril.Children;
|
||||||
allowGuest?: boolean;
|
allowGuest?: boolean;
|
||||||
|
allowNoOne?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PermissionSetting {
|
export interface PermissionSetting {
|
||||||
|
@ -435,6 +436,7 @@ export default class PermissionGrid<CustomAttrs extends IPermissionGridAttrs = I
|
||||||
return PermissionDropdown.component({
|
return PermissionDropdown.component({
|
||||||
permission: item.permission,
|
permission: item.permission,
|
||||||
allowGuest: item.allowGuest,
|
allowGuest: item.allowGuest,
|
||||||
|
allowNoOne: item.allowNoOne,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import Model from '../Model';
|
import Model from '../Model';
|
||||||
|
|
||||||
export default class Group extends Model {
|
export default class Group extends Model {
|
||||||
|
static NO_ONE_ID = '-1';
|
||||||
static ADMINISTRATOR_ID = '1';
|
static ADMINISTRATOR_ID = '1';
|
||||||
static GUEST_ID = '2';
|
static GUEST_ID = '2';
|
||||||
static MEMBER_ID = '3';
|
static MEMBER_ID = '3';
|
||||||
|
|
|
@ -235,6 +235,7 @@ core:
|
||||||
members_button: => core.group.members
|
members_button: => core.group.members
|
||||||
signup_closed_button: Closed
|
signup_closed_button: Closed
|
||||||
signup_open_button: Open
|
signup_open_button: Open
|
||||||
|
no_one_button: No one
|
||||||
|
|
||||||
# These translations are used generically in setting fields.
|
# These translations are used generically in setting fields.
|
||||||
settings:
|
settings:
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
namespace Flarum\Api\Controller;
|
namespace Flarum\Api\Controller;
|
||||||
|
|
||||||
|
use Flarum\Group\Group;
|
||||||
use Flarum\Group\Permission;
|
use Flarum\Group\Permission;
|
||||||
use Flarum\Http\RequestUtil;
|
use Flarum\Http\RequestUtil;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
@ -32,6 +33,11 @@ class SetPermissionController implements RequestHandlerInterface
|
||||||
|
|
||||||
Permission::where('permission', $permission)->delete();
|
Permission::where('permission', $permission)->delete();
|
||||||
|
|
||||||
|
// Permission set to no one.
|
||||||
|
if (count($groupIds) === 1 && intval($groupIds[0]) === Group::NO_ONE_ID) {
|
||||||
|
return new EmptyResponse(204);
|
||||||
|
}
|
||||||
|
|
||||||
Permission::insert(array_map(function ($groupId) use ($permission) {
|
Permission::insert(array_map(function ($groupId) use ($permission) {
|
||||||
return [
|
return [
|
||||||
'permission' => $permission,
|
'permission' => $permission,
|
||||||
|
|
|
@ -32,6 +32,11 @@ class Group extends AbstractModel
|
||||||
use EventGeneratorTrait;
|
use EventGeneratorTrait;
|
||||||
use ScopeVisibilityTrait;
|
use ScopeVisibilityTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The temporary ID for when no one can do something.
|
||||||
|
*/
|
||||||
|
const NO_ONE_ID = -1;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The ID of the administrator group.
|
* The ID of the administrator group.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue
Block a user