Improve permissions check
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
fa18698cd3
commit
0879222aa2
5 changed files with 15 additions and 11 deletions
|
@ -9,7 +9,7 @@ import { openModal } from 'soapbox/actions/modals';
|
|||
import GroupCard from 'soapbox/components/group-card';
|
||||
import ScrollableList from 'soapbox/components/scrollable-list';
|
||||
import { Button, Column, Spinner, Stack, Text } from 'soapbox/components/ui';
|
||||
import { useAppSelector, useOwnAccount } from 'soapbox/hooks';
|
||||
import { useAppSelector } from 'soapbox/hooks';
|
||||
import { PERMISSION_CREATE_GROUPS, hasPermission } from 'soapbox/utils/permissions';
|
||||
|
||||
import PlaceholderGroupCard from '../placeholder/components/placeholder-group-card';
|
||||
|
@ -32,9 +32,9 @@ const getOrderedGroups = createSelector([
|
|||
|
||||
const Groups: React.FC = () => {
|
||||
const dispatch = useDispatch();
|
||||
const account = useOwnAccount();
|
||||
|
||||
const { groups, isLoading } = useAppSelector((state) => getOrderedGroups(state));
|
||||
const canCreateGroup = useAppSelector((state) => hasPermission(state, PERMISSION_CREATE_GROUPS));
|
||||
|
||||
useEffect(() => {
|
||||
dispatch(fetchGroups());
|
||||
|
@ -74,7 +74,7 @@ const Groups: React.FC = () => {
|
|||
|
||||
return (
|
||||
<Stack className='gap-4'>
|
||||
{hasPermission(account, PERMISSION_CREATE_GROUPS) && (
|
||||
{canCreateGroup && (
|
||||
<Button
|
||||
className='sm:w-fit sm:self-end xl:hidden'
|
||||
icon={require('@tabler/icons/circles.svg')}
|
||||
|
|
|
@ -3,18 +3,19 @@ import { FormattedMessage } from 'react-intl';
|
|||
|
||||
import { openModal } from 'soapbox/actions/modals';
|
||||
import { Button, Stack, Text } from 'soapbox/components/ui';
|
||||
import { useAppDispatch, useOwnAccount } from 'soapbox/hooks';
|
||||
import { useAppDispatch, useAppSelector } from 'soapbox/hooks';
|
||||
import { PERMISSION_CREATE_GROUPS, hasPermission } from 'soapbox/utils/permissions';
|
||||
|
||||
const NewGroupPanel = () => {
|
||||
const dispatch = useAppDispatch();
|
||||
const account = useOwnAccount();
|
||||
|
||||
const canCreateGroup = useAppSelector((state) => hasPermission(state, PERMISSION_CREATE_GROUPS));
|
||||
|
||||
const createGroup = () => {
|
||||
dispatch(openModal('MANAGE_GROUP'));
|
||||
};
|
||||
|
||||
if (!hasPermission(account, PERMISSION_CREATE_GROUPS)) return null;
|
||||
if (!canCreateGroup) return null;
|
||||
|
||||
return (
|
||||
<Stack space={2}>
|
||||
|
|
|
@ -47,7 +47,6 @@ export const AccountRecord = ImmutableRecord({
|
|||
mute_expires_at: null as string | null,
|
||||
note: '',
|
||||
pleroma: ImmutableMap<string, any>(),
|
||||
role: ImmutableMap<string, any>(),
|
||||
source: ImmutableMap<string, any>(),
|
||||
statuses_count: 0,
|
||||
uri: '',
|
||||
|
|
|
@ -12,10 +12,11 @@ import type { AnyAction } from 'redux';
|
|||
|
||||
const MetaRecord = ImmutableRecord({
|
||||
pleroma: ImmutableMap<string, any>(),
|
||||
role: null as ImmutableMap<string, any> | null,
|
||||
source: ImmutableMap<string, any>(),
|
||||
});
|
||||
|
||||
type Meta = ReturnType<typeof MetaRecord>;
|
||||
export type Meta = ReturnType<typeof MetaRecord>;
|
||||
type State = ImmutableMap<string, Meta>;
|
||||
|
||||
const importAccount = (state: State, account: ImmutableMap<string, any>) => {
|
||||
|
@ -23,6 +24,7 @@ const importAccount = (state: State, account: ImmutableMap<string, any>) => {
|
|||
|
||||
return state.set(accountId, MetaRecord({
|
||||
pleroma: account.get('pleroma', ImmutableMap()).delete('settings_store'),
|
||||
role: account.get('role', null),
|
||||
source: account.get('source', ImmutableMap()),
|
||||
}));
|
||||
};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import type { Account } from 'soapbox/types/entities';
|
||||
import type { RootState } from 'soapbox/store';
|
||||
|
||||
export const PERMISSION_CREATE_GROUPS = 0x0000000000100000;
|
||||
export const PERMISSION_INVITE_USERS = 0x0000000000010000;
|
||||
|
@ -7,8 +7,10 @@ export const PERMISSION_MANAGE_REPORTS = 0x0000000000000010;
|
|||
|
||||
type Permission = typeof PERMISSION_CREATE_GROUPS | typeof PERMISSION_INVITE_USERS | typeof PERMISSION_MANAGE_USERS | typeof PERMISSION_MANAGE_REPORTS
|
||||
|
||||
export const hasPermission = (account: Account | null, permission: Permission) => {
|
||||
if (!account) return false;
|
||||
export const hasPermission = (state: RootState, permission: Permission) => {
|
||||
const account = state.accounts_meta.get(state.me as string)!;
|
||||
|
||||
if (!account?.role) return false;
|
||||
|
||||
const permissions = account.getIn(['role', 'permissions']) as number;
|
||||
|
||||
|
|
Loading…
Reference in a new issue