Improve permissions check

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-12-19 14:10:13 +01:00
parent fa18698cd3
commit 0879222aa2
5 changed files with 15 additions and 11 deletions

View file

@ -9,7 +9,7 @@ import { openModal } from 'soapbox/actions/modals';
import GroupCard from 'soapbox/components/group-card'; import GroupCard from 'soapbox/components/group-card';
import ScrollableList from 'soapbox/components/scrollable-list'; import ScrollableList from 'soapbox/components/scrollable-list';
import { Button, Column, Spinner, Stack, Text } from 'soapbox/components/ui'; 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 { PERMISSION_CREATE_GROUPS, hasPermission } from 'soapbox/utils/permissions';
import PlaceholderGroupCard from '../placeholder/components/placeholder-group-card'; import PlaceholderGroupCard from '../placeholder/components/placeholder-group-card';
@ -32,9 +32,9 @@ const getOrderedGroups = createSelector([
const Groups: React.FC = () => { const Groups: React.FC = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const account = useOwnAccount();
const { groups, isLoading } = useAppSelector((state) => getOrderedGroups(state)); const { groups, isLoading } = useAppSelector((state) => getOrderedGroups(state));
const canCreateGroup = useAppSelector((state) => hasPermission(state, PERMISSION_CREATE_GROUPS));
useEffect(() => { useEffect(() => {
dispatch(fetchGroups()); dispatch(fetchGroups());
@ -74,7 +74,7 @@ const Groups: React.FC = () => {
return ( return (
<Stack className='gap-4'> <Stack className='gap-4'>
{hasPermission(account, PERMISSION_CREATE_GROUPS) && ( {canCreateGroup && (
<Button <Button
className='sm:w-fit sm:self-end xl:hidden' className='sm:w-fit sm:self-end xl:hidden'
icon={require('@tabler/icons/circles.svg')} icon={require('@tabler/icons/circles.svg')}

View file

@ -3,18 +3,19 @@ import { FormattedMessage } from 'react-intl';
import { openModal } from 'soapbox/actions/modals'; import { openModal } from 'soapbox/actions/modals';
import { Button, Stack, Text } from 'soapbox/components/ui'; 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'; import { PERMISSION_CREATE_GROUPS, hasPermission } from 'soapbox/utils/permissions';
const NewGroupPanel = () => { const NewGroupPanel = () => {
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const account = useOwnAccount();
const canCreateGroup = useAppSelector((state) => hasPermission(state, PERMISSION_CREATE_GROUPS));
const createGroup = () => { const createGroup = () => {
dispatch(openModal('MANAGE_GROUP')); dispatch(openModal('MANAGE_GROUP'));
}; };
if (!hasPermission(account, PERMISSION_CREATE_GROUPS)) return null; if (!canCreateGroup) return null;
return ( return (
<Stack space={2}> <Stack space={2}>

View file

@ -47,7 +47,6 @@ export const AccountRecord = ImmutableRecord({
mute_expires_at: null as string | null, mute_expires_at: null as string | null,
note: '', note: '',
pleroma: ImmutableMap<string, any>(), pleroma: ImmutableMap<string, any>(),
role: ImmutableMap<string, any>(),
source: ImmutableMap<string, any>(), source: ImmutableMap<string, any>(),
statuses_count: 0, statuses_count: 0,
uri: '', uri: '',

View file

@ -12,10 +12,11 @@ import type { AnyAction } from 'redux';
const MetaRecord = ImmutableRecord({ const MetaRecord = ImmutableRecord({
pleroma: ImmutableMap<string, any>(), pleroma: ImmutableMap<string, any>(),
role: null as ImmutableMap<string, any> | null,
source: ImmutableMap<string, any>(), source: ImmutableMap<string, any>(),
}); });
type Meta = ReturnType<typeof MetaRecord>; export type Meta = ReturnType<typeof MetaRecord>;
type State = ImmutableMap<string, Meta>; type State = ImmutableMap<string, Meta>;
const importAccount = (state: State, account: ImmutableMap<string, any>) => { 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({ return state.set(accountId, MetaRecord({
pleroma: account.get('pleroma', ImmutableMap()).delete('settings_store'), pleroma: account.get('pleroma', ImmutableMap()).delete('settings_store'),
role: account.get('role', null),
source: account.get('source', ImmutableMap()), source: account.get('source', ImmutableMap()),
})); }));
}; };

View file

@ -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_CREATE_GROUPS = 0x0000000000100000;
export const PERMISSION_INVITE_USERS = 0x0000000000010000; 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 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) => { export const hasPermission = (state: RootState, permission: Permission) => {
if (!account) return false; const account = state.accounts_meta.get(state.me as string)!;
if (!account?.role) return false;
const permissions = account.getIn(['role', 'permissions']) as number; const permissions = account.getIn(['role', 'permissions']) as number;