Skip to content

Commit

Permalink
chore: fix user role checks
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyjoygh committed Sep 2, 2024
1 parent 7a9f619 commit 40d59fc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion web/src/components/MemoContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useCurrentUser from "@/hooks/useCurrentUser";
import { useMemoStore } from "@/store/v1";
import { Node, NodeType } from "@/types/proto/api/v1/markdown_service";
import { useTranslate } from "@/utils/i18n";
import { isSuperUser } from "@/utils/user";
import Renderer from "./Renderer";
import { RendererContext } from "./types";

Expand Down Expand Up @@ -33,7 +34,7 @@ const MemoContent: React.FC<Props> = (props: Props) => {
const memoContentContainerRef = useRef<HTMLDivElement>(null);
const [showCompactMode, setShowCompactMode] = useState<boolean>(false);
const memo = memoName ? memoStore.getMemoByName(memoName) : null;
const allowEdit = !props.readonly && memo && currentUser?.name === memo.creator;
const allowEdit = !props.readonly && memo && (currentUser?.name === memo.creator || isSuperUser(currentUser));

// Initial compact mode.
useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/MemoView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import useNavigateTo from "@/hooks/useNavigateTo";
import { useUserStore, useWorkspaceSettingStore } from "@/store/v1";
import { MemoRelation_Type } from "@/types/proto/api/v1/memo_relation_service";
import { Memo, Visibility } from "@/types/proto/api/v1/memo_service";
import { User_Role } from "@/types/proto/api/v1/user_service";
import { WorkspaceMemoRelatedSetting } from "@/types/proto/api/v1/workspace_setting_service";
import { WorkspaceSettingKey } from "@/types/proto/store/workspace_setting";
import { useTranslate } from "@/utils/i18n";
import { convertVisibilityToString } from "@/utils/memo";
import { isSuperUser } from "@/utils/user";
import MemoActionMenu from "./MemoActionMenu";
import MemoContent from "./MemoContent";
import MemoEditor from "./MemoEditor";
Expand Down Expand Up @@ -54,7 +54,7 @@ const MemoView: React.FC<Props> = (props: Props) => {
(relation) => relation.type === MemoRelation_Type.COMMENT && relation.relatedMemo === memo.name,
).length;
const relativeTimeFormat = Date.now() - memo.displayTime!.getTime() > 1000 * 60 * 60 * 24 ? "datetime" : "auto";
const readonly = memo.creator !== user?.name && user.role === User_Role.USER;
const readonly = memo.creator !== user?.name || isSuperUser(user);
const isInMemoDetailPage = location.pathname.startsWith(`/m/${memo.uid}`);

// Initial related data: creator.
Expand Down
5 changes: 5 additions & 0 deletions web/src/utils/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { User, User_Role } from "@/types/proto/api/v1/user_service";

export const isSuperUser = (user: User) => {
return user.role === User_Role.ADMIN || user.role === User_Role.HOST;
};

0 comments on commit 40d59fc

Please sign in to comment.