Disable posting and reposting if current user doesn't have "create_post" permission

This commit is contained in:
silverpill 2023-01-25 07:01:42 +00:00
parent f5081116fe
commit 94eaa3eed4
4 changed files with 39 additions and 5 deletions

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased] ## [Unreleased]
### Changed
- Disabled posting and reposting if current user doesn't have `create_post` permission.
## [1.11.0] - 2023-01-23 ## [1.11.0] - 2023-01-23
### Added ### Added

View file

@ -24,6 +24,16 @@ interface Source {
fields: ProfileField[]; fields: ProfileField[];
} }
interface Role {
id: number,
name: string,
permissions: string[],
}
export enum Permissions {
CreatePost = "create_post",
}
export interface Profile { export interface Profile {
id: string; id: string;
username: string; username: string;
@ -67,6 +77,7 @@ export function defaultProfile(): Profile {
export interface User extends Profile { export interface User extends Profile {
source: Source; source: Source;
role: Role,
} }
export interface ProfileWrapper extends Profile {} export interface ProfileWrapper extends Profile {}

View file

@ -255,7 +255,7 @@ import {
createRepost, createRepost,
deleteRepost, deleteRepost,
} from "@/api/posts" } from "@/api/posts"
import { ProfileWrapper } from "@/api/users" import { Permissions, ProfileWrapper } from "@/api/users"
import Avatar from "@/components/Avatar.vue" import Avatar from "@/components/Avatar.vue"
import CryptoAddress from "@/components/CryptoAddress.vue" import CryptoAddress from "@/components/CryptoAddress.vue"
import PostAttachment from "@/components/PostAttachment.vue" import PostAttachment from "@/components/PostAttachment.vue"
@ -348,7 +348,10 @@ function getQuoteAuthorDisplayName(post: Post): string | null {
} }
function canReply(): boolean { function canReply(): boolean {
return currentUser !== null if (currentUser === null) {
return false
}
return currentUser.role.permissions.includes(Permissions.CreatePost)
} }
function onCommentCreated(post: Post) { function onCommentCreated(post: Post) {
@ -357,7 +360,13 @@ function onCommentCreated(post: Post) {
} }
function canRepost(): boolean { function canRepost(): boolean {
return currentUser !== null && props.post.visibility === "public" if (currentUser === null) {
return false
}
return (
props.post.visibility === "public" &&
currentUser.role.permissions.includes(Permissions.CreatePost)
)
} }
async function toggleRepost() { async function toggleRepost() {

View file

@ -1,7 +1,11 @@
<template> <template>
<sidebar-layout> <sidebar-layout>
<template #content> <template #content>
<post-editor :in-reply-to="null" @post-created="insertPost"></post-editor> <post-editor
v-if="canCreatePost()"
:in-reply-to="null"
@post-created="insertPost"
></post-editor>
<post-list :posts="posts" @load-next-page="loadNextPage"></post-list> <post-list :posts="posts" @load-next-page="loadNextPage"></post-list>
<loader v-if="isLoading"></loader> <loader v-if="isLoading"></loader>
</template> </template>
@ -13,13 +17,14 @@ import { onMounted } from "vue"
import { $ref } from "vue/macros" import { $ref } from "vue/macros"
import { Post, getHomeTimeline } from "@/api/posts" import { Post, getHomeTimeline } from "@/api/posts"
import { Permissions } from "@/api/users"
import Loader from "@/components/Loader.vue" import Loader from "@/components/Loader.vue"
import PostEditor from "@/components/PostEditor.vue" import PostEditor from "@/components/PostEditor.vue"
import PostList from "@/components/PostList.vue" import PostList from "@/components/PostList.vue"
import SidebarLayout from "@/components/SidebarLayout.vue" import SidebarLayout from "@/components/SidebarLayout.vue"
import { useCurrentUser } from "@/store/user" import { useCurrentUser } from "@/store/user"
const { ensureAuthToken } = useCurrentUser() const { ensureAuthToken, ensureCurrentUser } = useCurrentUser()
let posts = $ref<Post[]>([]) let posts = $ref<Post[]>([])
let isLoading = $ref(false) let isLoading = $ref(false)
@ -31,6 +36,11 @@ onMounted(async () => {
isLoading = false isLoading = false
}) })
function canCreatePost(): boolean {
const user = ensureCurrentUser()
return user.role.permissions.includes(Permissions.CreatePost)
}
function insertPost(post: Post) { function insertPost(post: Post) {
posts = [post, ...posts] posts = [post, ...posts]
} }