Skip to content

Commit

Permalink
admin api: new admin crud rights, and admin guild create route
Browse files Browse the repository at this point in the history
  • Loading branch information
Puyodead1 committed Dec 3, 2023
1 parent a31fc9d commit 074c4b0
Show file tree
Hide file tree
Showing 14 changed files with 16,618 additions and 11,826 deletions.
28,287 changes: 16,492 additions & 11,795 deletions assets/schemas.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/api/routes/admin/guilds/#guild_id/bans/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ router.get(
"/",
route({
description: "Get bans of a guild",
right: "MANAGE_GUILDS",
right: "ADMIN_READ_GUILD_BANS",
query: {
limit: {
type: "number",
Expand Down
16 changes: 8 additions & 8 deletions src/api/routes/admin/guilds/#guild_id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import { route } from "@spacebar/api";
import {
AdminGuildModifySchema,
AdminGuildProjection,
Channel,
Guild,
GuildAdminModifySchema,
GuildUpdateEvent,
Permissions,
emitEvent,
Expand All @@ -34,10 +34,10 @@ router.get(
"/",
route({
description: "Get a guild",
right: "MANAGE_GUILDS",
right: "ADMIN_READ_GUILDS",
responses: {
200: {
body: "GuildAdminResponse",
body: "AdminGuildResponse",
},
400: {
body: "APIErrorResponse",
Expand All @@ -62,11 +62,11 @@ router.get(
router.patch(
"/",
route({
requestBody: "GuildAdminModifySchema",
right: "MANAGE_GUILDS",
requestBody: "AdminGuildModifySchema",
right: "ADMIN_UPDATE_GUILDS",
responses: {
"200": {
body: "GuildAdminResponse",
body: "AdminGuildResponse",
},
401: {
body: "APIErrorResponse",
Expand All @@ -80,7 +80,7 @@ router.patch(
},
}),
async (req: Request, res: Response) => {
const body = req.body as GuildAdminModifySchema;
const body = req.body as AdminGuildModifySchema;
const { id: guild_id } = req.params;

const guild = await Guild.findOneOrFail({
Expand Down Expand Up @@ -212,7 +212,7 @@ router.delete(
"/",
route({
description: "Delete a guild",
right: "MANAGE_GUILDS",
right: "ADMIN_DELETE_GUILDS",
responses: {
200: {},
400: {
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/admin/guilds/#guild_id/members/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ router.get(
"/",
route({
description: "Get members of a guild",
right: "MANAGE_GUILDS",
right: "ADMIN_READ_MEMBERS",
query: {
limit: {
type: "number",
Expand Down
44 changes: 41 additions & 3 deletions src/api/routes/admin/guilds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,55 @@
*/

import { route } from "@spacebar/api";
import { AdminGuildProjection, Guild } from "@spacebar/util";
import {
AdminGuildCreateSchema,
AdminGuildProjection,
Guild,
Member,
} from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
import { ILike, MoreThan } from "typeorm";
const router = Router();

router.post(
"/",
route({
requestBody: "AdminGuildCreateSchema",
right: "ADMIN_CREATE_GUILDS",
responses: {
201: {
body: "AdminGuildResponse",
},
400: {
body: "APIErrorResponse",
},
403: {
body: "APIErrorResponse",
},
},
}),
async (req: Request, res: Response) => {
const body = req.body as AdminGuildCreateSchema;

const ownerId = body.owner_id || req.user_id;

const guild = await Guild.createGuild({
...body,
owner_id: ownerId,
});

await Member.addToGuild(ownerId, guild.id);

res.status(201).json(guild);
},
);

router.get(
"/",
route({
description: "Get a list of guilds",
right: "MANAGE_GUILDS",
right: "ADMIN_READ_GUILDS",
query: {
limit: {
description:
Expand All @@ -48,7 +86,7 @@ router.get(
},
responses: {
200: {
body: "GuildsAdminResponse",
body: "AdminGuildsResponse",
},
400: {
body: "APIErrorResponse",
Expand Down
16 changes: 8 additions & 8 deletions src/api/routes/admin/users/#id/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

import { route } from "@spacebar/api";
import {
AdminUserModifySchema,
AdminUserProjection,
FieldErrors,
User,
UserAdminModifySchema,
UserUpdateEvent,
emitEvent,
handleFile,
Expand All @@ -34,10 +34,10 @@ router.get(
"/",
route({
description: "Get a user",
right: "MANAGE_USERS",
right: "ADMIN_READ_USERS",
responses: {
200: {
body: "UserAdminResponse",
body: "AdminUserResponse",
},
400: {
body: "APIErrorResponse",
Expand All @@ -61,11 +61,11 @@ router.patch(
"/",
route({
description: "Update a user",
right: "MANAGE_USERS",
requestBody: "UserAdminModifySchema",
right: "ADMIN_UPDATE_USERS",
requestBody: "AdminUserModifySchema",
responses: {
200: {
body: "UserAdminResponse",
body: "AdminUserResponse",
},
400: {
body: "APIErrorResponse",
Expand All @@ -76,7 +76,7 @@ router.patch(
},
}),
async (req: Request, res: Response) => {
const body = req.body as UserAdminModifySchema;
const body = req.body as AdminUserModifySchema;

const user = await User.findOneOrFail({
where: { id: req.user_id },
Expand Down Expand Up @@ -136,7 +136,7 @@ router.delete(
"/",
route({
description: "Delete a user",
right: "MANAGE_USERS",
right: "ADMIN_DELETE_USERS",
responses: {
200: {},
400: {
Expand Down
4 changes: 2 additions & 2 deletions src/api/routes/admin/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const router = Router();
router.get(
"/",
route({
right: "MANAGE_USERS",
right: "ADMIN_READ_USERS",
description: "Get a list of users",
query: {
limit: {
Expand All @@ -48,7 +48,7 @@ router.get(
},
responses: {
200: {
body: "UsersAdminResponse",
body: "AdminUsersResponse",
},
400: {
body: "APIErrorResponse",
Expand Down
36 changes: 36 additions & 0 deletions src/util/schemas/admin/AdminGuildCreateSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { AdminGuild } from "../../interfaces";

export type AdminGuildCreateSchema = Partial<
Exclude<
AdminGuild,
| "id"
| "bans"
| "members"
| "template_id"
| "invites"
| "owner"
| "public_updates_channel"
| "rules_channel"
| "system_channel"
| "widget_channel"
| "channel_ordering"
>
>;
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@

import { AdminGuild } from "@spacebar/util";

export type GuildAdminModifySchema = Partial<Exclude<AdminGuild, "id">>;
export type AdminGuildModifySchema = Partial<Exclude<AdminGuild, "id">>;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { AdminUser } from "@spacebar/util";

export type UserAdminModifySchema = Partial<
export type AdminUserModifySchema = Partial<
Exclude<AdminUser, "id"> & {
password?: string;
}
Expand Down
5 changes: 3 additions & 2 deletions src/util/schemas/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

export * from "./GuildAdminModifySchema";
export * from "./UserAdminModifySchema";
export * from "./AdminGuildCreateSchema";
export * from "./AdminGuildModifySchema";
export * from "./AdminUserModifySchema";
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

import { AdminUser } from "@spacebar/util";

export type UserAdminResponse = AdminUser;
export type UsersAdminResponse = AdminUser[];
export type AdminUserResponse = AdminUser;
export type AdminUsersResponse = AdminUser[];
4 changes: 2 additions & 2 deletions src/util/schemas/responses/GuildListAdminResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@

import { AdminGuild } from "@spacebar/util";

export type GuildAdminResponse = AdminGuild;
export type GuildsAdminResponse = AdminGuild[];
export type AdminGuildResponse = AdminGuild;
export type AdminGuildsResponse = AdminGuild[];
20 changes: 20 additions & 0 deletions src/util/util/Rights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ export class Rights extends BitField {
MANAGE_GROUPS: BitFlag(47), // can manage others' groups
VIEW_SERVER_STATS: BitFlag(48), // added per @chrischrome's request — can view server stats)
RESEND_VERIFICATION_EMAIL: BitFlag(49), // can resend verification emails (/auth/verify/resend)
// start of admin rights
ADMIN_CREATE_USERS: BitFlag(50), // can create users
ADMIN_READ_USERS: BitFlag(51), // can read users
ADMIN_UPDATE_USERS: BitFlag(52), // can update users
ADMIN_DELETE_USERS: BitFlag(53), // can delete users
//
ADMIN_CREATE_GUILDS: BitFlag(54), // can create guilds for other users
ADMIN_READ_GUILDS: BitFlag(55), // can read guilds
ADMIN_UPDATE_GUILDS: BitFlag(56), // can update guilds
ADMIN_DELETE_GUILDS: BitFlag(57), // can delete guilds
//
ADMIN_CREATE_MEMBERS: BitFlag(58), // can "create" members, aka force add members to a guild
ADMIN_READ_MEMBERS: BitFlag(59), // can read members
ADMIN_UPDATE_MEMBERS: BitFlag(60), // can update members
ADMIN_DELETE_MEMBERS: BitFlag(61), // can delete members
//
ADMIN_CREATE_GUILD_BANS: BitFlag(62), // can create guild bans
ADMIN_READ_GUILD_BANS: BitFlag(63), // can read guild bans
ADMIN_UPDATE_GUILD_BANS: BitFlag(64), // can update guild bans
ADMIN_DELETE_GUILD_BANS: BitFlag(65), // can delete guild bans
};

any(permission: RightResolvable, checkOperator = true) {
Expand Down

0 comments on commit 074c4b0

Please sign in to comment.