New controllers and services; Auth
This commit is contained in:
parent
2fc0f9c404
commit
d17f37749d
35 changed files with 1040 additions and 164 deletions
|
|
@ -1,5 +1,9 @@
|
|||
import { API_ERROR } from "../../controllers/errors.js";
|
||||
import type { Channel } from "../../generated/prisma/client.js";
|
||||
import { getDB } from "../../store/store.js";
|
||||
import { getUserFromAuth, isUserAllowed } from "../auth/helpers.js";
|
||||
import { PERMISSION } from "../auth/permission.js";
|
||||
import { getCommunityById } from "../community/community.js";
|
||||
|
||||
const getChannelById = async (id: string): Promise<Channel | null> => {
|
||||
return await getDB().channel.findUnique({
|
||||
|
|
@ -7,4 +11,28 @@ const getChannelById = async (id: string): Promise<Channel | null> => {
|
|||
});
|
||||
};
|
||||
|
||||
export { getChannelById };
|
||||
const getChannelByIdAuth = async (
|
||||
id: string,
|
||||
authHeader: string | undefined,
|
||||
): Promise<Channel | null | API_ERROR.ACCESS_DENIED> => {
|
||||
const authUser = await getUserFromAuth(authHeader);
|
||||
const channel = await getChannelById(id);
|
||||
const community = await getCommunityById(channel?.communityId ?? "");
|
||||
|
||||
if (
|
||||
!(await isUserAllowed(
|
||||
authUser,
|
||||
{
|
||||
channel: channel,
|
||||
},
|
||||
community,
|
||||
[PERMISSION.CHANNELS_READ],
|
||||
))
|
||||
) {
|
||||
return API_ERROR.ACCESS_DENIED;
|
||||
}
|
||||
|
||||
return channel;
|
||||
};
|
||||
|
||||
export { getChannelById, getChannelByIdAuth };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue