Skip to content

Commit

Permalink
[WEB-1255] fix: edit and delete access control for views (#4964)
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulramesha committed Jun 28, 2024
1 parent 3b2af2d commit 96563b4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
8 changes: 5 additions & 3 deletions web/core/components/views/quick-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ export const ViewQuickActions: React.FC<Props> = observer((props) => {
// store hooks
const {
membership: { currentProjectRole },
data,
} = useUser();
// auth
const isEditingAllowed = !!currentProjectRole && currentProjectRole >= EUserProjectRoles.MEMBER;
const isOwner = view?.owned_by === data?.id;
const isAdmin = !!currentProjectRole && currentProjectRole == EUserProjectRoles.ADMIN;

const viewLink = `${workspaceSlug}/projects/${projectId}/views/${view.id}`;
const handleCopyText = () =>
Expand All @@ -53,7 +55,7 @@ export const ViewQuickActions: React.FC<Props> = observer((props) => {
action: () => setCreateUpdateViewModal(true),
title: "Edit",
icon: Pencil,
shouldRender: isEditingAllowed,
shouldRender: isOwner,
},
{
key: "open-new-tab",
Expand All @@ -72,7 +74,7 @@ export const ViewQuickActions: React.FC<Props> = observer((props) => {
action: () => setDeleteViewModal(true),
title: "Delete",
icon: Trash2,
shouldRender: isEditingAllowed,
shouldRender: isOwner || isAdmin,
},
];

Expand Down
4 changes: 2 additions & 2 deletions web/core/components/views/view-list-item-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const ViewListItemAction: FC<Props> = observer((props) => {
removeViewFromFavorites(workspaceSlug.toString(), projectId.toString(), view.id);
};

const createdByDetails = view.created_by ? getUserDetails(view.created_by) : undefined;
const ownedByDetails = view.owned_by ? getUserDetails(view.owned_by) : undefined;

return (
<>
Expand All @@ -81,7 +81,7 @@ export const ViewListItemAction: FC<Props> = observer((props) => {
</div>

{/* created by */}
{<ButtonAvatars showTooltip={false} userIds={createdByDetails?.id ?? []} />}
{<ButtonAvatars showTooltip={false} userIds={ownedByDetails?.id ?? []} />}

{isEditingAllowed && (
<FavoriteStar
Expand Down
10 changes: 6 additions & 4 deletions web/core/components/workspace/views/quick-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { ContextMenu, CustomMenu, TContextMenuItem, TOAST_TYPE, setToast } from
// components
import { CreateUpdateWorkspaceViewModal, DeleteGlobalViewModal } from "@/components/workspace";
// constants
import { EUserProjectRoles } from "@/constants/project";
import { EViewAccess } from "@/constants/views";
import { EUserWorkspaceRoles } from "@/constants/workspace";
// helpers
import { cn } from "@/helpers/common.helper";
import { copyUrlToClipboard } from "@/helpers/string.helper";
Expand All @@ -35,9 +35,11 @@ export const WorkspaceViewQuickActions: React.FC<Props> = observer((props) => {
// store hooks
const {
membership: { currentWorkspaceRole },
data,
} = useUser();
// auth
const isEditingAllowed = !!currentWorkspaceRole && currentWorkspaceRole >= EUserProjectRoles.MEMBER;
const isOwner = view?.owned_by === data?.id;
const isAdmin = !!currentWorkspaceRole && currentWorkspaceRole === EUserWorkspaceRoles.ADMIN;

const viewLink = `${workspaceSlug}/workspace-views/${view.id}`;
const handleCopyText = () =>
Expand All @@ -56,7 +58,7 @@ export const WorkspaceViewQuickActions: React.FC<Props> = observer((props) => {
action: () => setUpdateViewModal(true),
title: "Edit",
icon: Pencil,
shouldRender: isEditingAllowed,
shouldRender: isOwner,
},
{
key: "open-new-tab",
Expand All @@ -75,7 +77,7 @@ export const WorkspaceViewQuickActions: React.FC<Props> = observer((props) => {
action: () => setDeleteViewModal(true),
title: "Delete",
icon: Trash2,
shouldRender: isEditingAllowed,
shouldRender: isOwner || isAdmin,
},
];

Expand Down
9 changes: 4 additions & 5 deletions web/core/store/member/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { action, makeObservable, observable } from "mobx";
// types
import { makeObservable, observable } from "mobx";
import { computedFn } from "mobx-utils";
// type
import { IUserLite } from "@plane/types";
// store
import { CoreRootStore } from "../root.store";
Expand Down Expand Up @@ -27,8 +28,6 @@ export class MemberRootStore implements IMemberRootStore {
makeObservable(this, {
// observables
memberMap: observable,
// computed actions
getUserDetails: action,
});
// sub-stores
this.workspace = new WorkspaceMemberStore(this, _rootStore);
Expand All @@ -39,5 +38,5 @@ export class MemberRootStore implements IMemberRootStore {
* @description get user details rom userId
* @param userId
*/
getUserDetails = (userId: string): IUserLite | undefined => this.memberMap?.[userId] ?? undefined;
getUserDetails = computedFn((userId: string): IUserLite | undefined => this.memberMap?.[userId] ?? undefined);
}

0 comments on commit 96563b4

Please sign in to comment.