Skip to content

Commit

Permalink
Refactor hasPermission
Browse files Browse the repository at this point in the history
  • Loading branch information
danielr18 committed Dec 25, 2023
1 parent a82a7c8 commit a3057af
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const CommonMenu: FC<CommonMenuProps> = (props) => {
hasPermission({
commonMember: currentCommonMember,
governance,
key: GovernanceActions.CREATE_SUBCOMMON,
action: GovernanceActions.CREATE_SUBCOMMON,
})
) {
items.push(MenuItem.CreateProject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function DiscussionsComponent({
hasPermission({
commonMember,
governance,
key: GovernanceActions.CREATE_DISCUSSION,
action: GovernanceActions.CREATE_DISCUSSION,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default function ChatComponent({
governance: {
circles: governanceCircles,
},
key: GovernanceActions.HIDE_OR_UNHIDE_MESSAGE,
action: GovernanceActions.HIDE_OR_UNHIDE_MESSAGE,
})
: false;
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
hasPermission({
commonMember,
governance,
key: ProposalsTypes.DELETE_COMMON,
proposal: ProposalsTypes.DELETE_COMMON,
}),
),
[CommonMenuItem.DeleteCommonAction]: ({ commonMember, governance }) =>
Expand All @@ -37,7 +37,7 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
hasPermission({
commonMember,
governance,
key: GovernanceActions.DELETE_COMMON_ACTION,
action: GovernanceActions.DELETE_COMMON,
}),
),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const CommonProjects: FC<CommonProjectsProps> = (props) => {
hasPermission({
commonMember,
governance: { circles },
key: GovernanceActions.CREATE_PROJECT,
action: GovernanceActions.CREATE_PROJECT,
}),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
hasPermission({
commonMember,
governance,
key: ProposalsTypes.SURVEY,
proposal: ProposalsTypes.SURVEY,
}),
),
[CommonAction.NewDiscussion]: ({ commonMember, governance }) =>
Expand All @@ -37,7 +37,7 @@ const MENU_ITEM_TO_CHECK_FUNCTION_MAP: Record<
hasPermission({
commonMember,
governance,
key: GovernanceActions.CREATE_DISCUSSION,
action: GovernanceActions.CREATE_DISCUSSION,
}),
),
[CommonAction.NewContribution]: () => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const checkIsLinkToAllowed = (
governance: {
circles: options.governanceCircles || {},
},
key: GovernanceActions.LINK_FROM_HERE,
action: GovernanceActions.LINK_FROM_HERE,
})
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function checkIsPinUnpinAllowed(
governance: {
circles: options.governanceCircles || {},
},
key: GovernanceActions.PIN_OR_UNPIN_FEED_ITEMS,
action: GovernanceActions.PIN_OR_UNPIN_FEED_ITEMS,
});

return isAllowed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function checkIsRemoveDiscussionAllowed(
hasPermission({
commonMember,
governance: { circles },
key: GovernanceActions.HIDE_OR_UNHIDE_DISCUSSION,
action: GovernanceActions.HIDE_OR_UNHIDE_DISCUSSION,
}) || isDiscussionOwner;

if (!options.discussion?.proposalId) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/Common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class CommonService {

public deleteCommon = async (commonId: string): Promise<void> => {
await Api.post(ApiEndpoint.CreateAction, {
type: GovernanceActions.DELETE_COMMON_ACTION,
type: GovernanceActions.DELETE_COMMON,
args: { commonId },
});
};
Expand Down
4 changes: 2 additions & 2 deletions src/shared/components/ElementDropdown/ElementDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const ElementDropdown: FC<ElementDropdownProps> = ({
governance: {
circles: governanceCircles,
},
key: HideContentTypes[entityType],
action: HideContentTypes[entityType],
}) &&
!isOwner &&
!isHiddenElement
Expand All @@ -211,7 +211,7 @@ const ElementDropdown: FC<ElementDropdownProps> = ({
governance: {
circles: governanceCircles,
},
key: DeleteContentTypes[entityType],
action: DeleteContentTypes[entityType],
})) &&
(isDiscussionMessage || isChatMessage)
) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/constants/governance/GovernanceActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export enum GovernanceActions {

PIN_OR_UNPIN_FEED_ITEMS = "PIN_OR_UNPIN_FEED_ITEMS",

DELETE_COMMON_ACTION = "DELETE_COMMON_ACTION",
DELETE_COMMON = "DELETE_COMMON",

MOVE_TO_HERE = "MOVE_TO_HERE",
LINK_TO_HERE = "LINK_TO_HERE",
Expand Down
37 changes: 21 additions & 16 deletions src/shared/utils/hasPermission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ import { GovernanceActions, ProposalsTypes } from "@/shared/constants";
import { CommonMember, Governance } from "@/shared/models";
import { generateCirclesDataForCommonMember } from "./generateCircleDataForCommonMember";

interface Query {
type Query = {
commonMember: CommonMember;
governance: Pick<Governance, "circles">;
key: ProposalsTypes | GovernanceActions;
}
} & (
| {
action: GovernanceActions;
}
| {
proposal: ProposalsTypes;
}
);

/**
* Doesnt check for nested permissions, i.e assign/remove circle - these are check on the proposal creation level
*/
export const hasPermission = ({
commonMember,
governance,
key,
}: Query): boolean => {
export const hasPermission = (query: Query): boolean => {
const { commonMember, governance } = query;
if (!commonMember || !governance) {
return false;
}
Expand All @@ -26,22 +29,24 @@ export const hasPermission = ({
commonMember.circleIds,
);

if (Object.values(ProposalsTypes).includes(key as ProposalsTypes)) {
if (
"proposal" in query &&
Object.values(ProposalsTypes).includes(query.proposal)
) {
if (
!circlesPermissions?.allowedActions[GovernanceActions.CREATE_PROPOSAL]
) {
return false;
}

return Boolean(
circlesPermissions?.allowedProposals[key as ProposalsTypes],
);
return Boolean(circlesPermissions?.allowedProposals[query.proposal]);
}

if (Object.values(GovernanceActions).includes(key as GovernanceActions)) {
return Boolean(
circlesPermissions?.allowedActions[key as GovernanceActions],
);
if (
"action" in query &&
Object.values(GovernanceActions).includes(query.action)
) {
return Boolean(circlesPermissions?.allowedActions[query.action]);
}
} catch (err) {
return false;
Expand Down

0 comments on commit a3057af

Please sign in to comment.