diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-10-04 10:47:04 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-10-04 10:47:04 +0200 |
commit | b69f5baeb997cecd12a0e42b94fe0616058866a2 (patch) | |
tree | a63c89ba5bb52249f2ab858c703bab0dc82b5e84 /util | |
parent | :sparkles: custom user flags offset (diff) | |
parent | Merge pull request #412 from TheArcaneBrony/master (diff) | |
download | server-b69f5baeb997cecd12a0e42b94fe0616058866a2.tar.xz |
Merge branch 'master' of https://github.com/fosscord/fosscord-server
Diffstat (limited to 'util')
-rw-r--r-- | util/src/entities/Config.ts | 10 | ||||
-rw-r--r-- | util/src/entities/Message.ts | 6 | ||||
-rw-r--r-- | util/src/entities/User.ts | 6 | ||||
-rw-r--r-- | util/src/util/BitField.ts | 4 | ||||
-rw-r--r-- | util/src/util/Config.ts | 1 | ||||
-rw-r--r-- | util/src/util/Constants.ts | 4 | ||||
-rw-r--r-- | util/src/util/Permissions.ts | 77 | ||||
-rw-r--r-- | util/src/util/Rights.ts | 73 | ||||
-rw-r--r-- | util/src/util/Token.ts | 5 | ||||
-rw-r--r-- | util/src/util/index.ts | 1 |
10 files changed, 140 insertions, 47 deletions
diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index 6d9db470..edce92a9 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -125,6 +125,7 @@ export interface ConfigValue { required: boolean; minimum: number; // in years }; + disabled: boolean; requireCaptcha: boolean; requireInvite: boolean; allowNewRegistration: boolean; @@ -143,6 +144,10 @@ export interface ConfigValue { useDefaultAsOptimal: boolean; available: Region[]; }; + + guild: { + showAllGuildsInDiscovery: boolean; + }; rabbitmq: { host: string | null; }; @@ -266,6 +271,7 @@ export const DefaultConfigOptions: ConfigValue = { required: false, minimum: 13, }, + disabled: false, requireInvite: false, requireCaptcha: true, allowNewRegistration: true, @@ -293,6 +299,10 @@ export const DefaultConfigOptions: ConfigValue = { }, ], }, + + guild: { + showAllGuildsInDiscovery: false, + }, rabbitmq: { host: null, }, diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts index c4901693..04c3c7aa 100644 --- a/util/src/entities/Message.ts +++ b/util/src/entities/Message.ts @@ -74,9 +74,7 @@ export class Message extends BaseClass { author_id: string; @JoinColumn({ name: "author_id", referencedColumnName: "id" }) - @ManyToOne(() => User, { - onDelete: "CASCADE", - }) + @ManyToOne(() => User) author?: User; @Column({ nullable: true }) @@ -120,7 +118,7 @@ export class Message extends BaseClass { mention_everyone?: boolean; @JoinTable({ name: "message_user_mentions" }) - @ManyToMany(() => User, { orphanedRowAction: "delete", onDelete: "CASCADE", cascade: true }) + @ManyToMany(() => User) mentions: User[]; @JoinTable({ name: "message_role_mentions" }) diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts index 5af755ed..b6f3723c 100644 --- a/util/src/entities/User.ts +++ b/util/src/entities/User.ts @@ -3,7 +3,6 @@ import { BaseClass } from "./BaseClass"; import { BitField } from "../util/BitField"; import { Relationship } from "./Relationship"; import { ConnectedAccount } from "./ConnectedAccount"; -import { HTTPError } from "lambert-server"; export enum PublicUserEnum { username, @@ -126,6 +125,9 @@ export class User extends BaseClass { @Column() public_flags: number; + @Column() + rights: string; // Rights + @JoinColumn({ name: "relationship_ids" }) @OneToMany(() => Relationship, (relationship: Relationship) => relationship.from, { cascade: true, @@ -278,5 +280,7 @@ export class UserFlags extends BitField { UNDERAGE_DELETED: BigInt(1) << BigInt(15), VERIFIED_BOT: BigInt(1) << BigInt(16), EARLY_VERIFIED_BOT_DEVELOPER: BigInt(1) << BigInt(17), + CERTIFIED_MODERATOR: BigInt(1) << BigInt(18), + BOT_HTTP_INTERACTIONS: BigInt(1) << BigInt(19), }; } diff --git a/util/src/util/BitField.ts b/util/src/util/BitField.ts index ac19763e..fb887e05 100644 --- a/util/src/util/BitField.ts +++ b/util/src/util/BitField.ts @@ -141,3 +141,7 @@ export class BitField { throw new RangeError("BITFIELD_INVALID: " + bit); } } + +export function BitFlag(x: bigint | number) { + return BigInt(1) << BigInt(x); +} diff --git a/util/src/util/Config.ts b/util/src/util/Config.ts index 6a9e0e5a..255f425d 100644 --- a/util/src/util/Config.ts +++ b/util/src/util/Config.ts @@ -21,7 +21,6 @@ export const Config = { set: function set(val: Partial<ConfigValue>) { if (!config || !val) return; config = val.merge(config); - console.log(config); return applyConfig(config); }, diff --git a/util/src/util/Constants.ts b/util/src/util/Constants.ts index d2cc5130..5fdf5bc0 100644 --- a/util/src/util/Constants.ts +++ b/util/src/util/Constants.ts @@ -726,7 +726,9 @@ export const DiscordApiErrors = { /** * An error encountered while performing an API request (Fosscord only). Here are the potential errors: */ -export const FosscordApiErrors = {}; +export const FosscordApiErrors = { + MISSING_RIGHTS: new ApiError("You lack rights to perform that action ({})", 50013, undefined, [""]), +}; /** * The value set for a guild's default message notifications, e.g. `ALL`. Here are the available types: diff --git a/util/src/util/Permissions.ts b/util/src/util/Permissions.ts index f0012c96..e5459ab5 100644 --- a/util/src/util/Permissions.ts +++ b/util/src/util/Permissions.ts @@ -3,8 +3,7 @@ import { Channel, ChannelPermissionOverwrite, Guild, Member, Role } from "../entities"; import { BitField } from "./BitField"; import "missing-native-js-functions"; -import { BitFieldResolvable } from "."; -// TODO: check role hierarchy permission +import { BitFieldResolvable, BitFlag } from "./BitField"; var HTTPError: any; @@ -32,44 +31,44 @@ export class Permissions extends BitField { } static FLAGS = { - CREATE_INSTANT_INVITE: BigInt(1) << BigInt(0), - KICK_MEMBERS: BigInt(1) << BigInt(1), - BAN_MEMBERS: BigInt(1) << BigInt(2), - ADMINISTRATOR: BigInt(1) << BigInt(3), - MANAGE_CHANNELS: BigInt(1) << BigInt(4), - MANAGE_GUILD: BigInt(1) << BigInt(5), - ADD_REACTIONS: BigInt(1) << BigInt(6), - VIEW_AUDIT_LOG: BigInt(1) << BigInt(7), - PRIORITY_SPEAKER: BigInt(1) << BigInt(8), - STREAM: BigInt(1) << BigInt(9), - VIEW_CHANNEL: BigInt(1) << BigInt(10), - SEND_MESSAGES: BigInt(1) << BigInt(11), - SEND_TTS_MESSAGES: BigInt(1) << BigInt(12), - MANAGE_MESSAGES: BigInt(1) << BigInt(13), - EMBED_LINKS: BigInt(1) << BigInt(14), - ATTACH_FILES: BigInt(1) << BigInt(15), - READ_MESSAGE_HISTORY: BigInt(1) << BigInt(16), - MENTION_EVERYONE: BigInt(1) << BigInt(17), - USE_EXTERNAL_EMOJIS: BigInt(1) << BigInt(18), - VIEW_GUILD_INSIGHTS: BigInt(1) << BigInt(19), - CONNECT: BigInt(1) << BigInt(20), - SPEAK: BigInt(1) << BigInt(21), - MUTE_MEMBERS: BigInt(1) << BigInt(22), - DEAFEN_MEMBERS: BigInt(1) << BigInt(23), - MOVE_MEMBERS: BigInt(1) << BigInt(24), - USE_VAD: BigInt(1) << BigInt(25), - CHANGE_NICKNAME: BigInt(1) << BigInt(26), - MANAGE_NICKNAMES: BigInt(1) << BigInt(27), - MANAGE_ROLES: BigInt(1) << BigInt(28), - MANAGE_WEBHOOKS: BigInt(1) << BigInt(29), - MANAGE_EMOJIS_AND_STICKERS: BigInt(1) << BigInt(30), - USE_APPLICATION_COMMANDS: BigInt(1) << BigInt(31), - REQUEST_TO_SPEAK: BigInt(1) << BigInt(32), + CREATE_INSTANT_INVITE: BitFlag(0), + KICK_MEMBERS: BitFlag(1), + BAN_MEMBERS: BitFlag(2), + ADMINISTRATOR: BitFlag(3), + MANAGE_CHANNELS: BitFlag(4), + MANAGE_GUILD: BitFlag(5), + ADD_REACTIONS: BitFlag(6), + VIEW_AUDIT_LOG: BitFlag(7), + PRIORITY_SPEAKER: BitFlag(8), + STREAM: BitFlag(9), + VIEW_CHANNEL: BitFlag(10), + SEND_MESSAGES: BitFlag(11), + SEND_TTS_MESSAGES: BitFlag(12), + MANAGE_MESSAGES: BitFlag(13), + EMBED_LINKS: BitFlag(14), + ATTACH_FILES: BitFlag(15), + READ_MESSAGE_HISTORY: BitFlag(16), + MENTION_EVERYONE: BitFlag(17), + USE_EXTERNAL_EMOJIS: BitFlag(18), + VIEW_GUILD_INSIGHTS: BitFlag(19), + CONNECT: BitFlag(20), + SPEAK: BitFlag(21), + MUTE_MEMBERS: BitFlag(22), + DEAFEN_MEMBERS: BitFlag(23), + MOVE_MEMBERS: BitFlag(24), + USE_VAD: BitFlag(25), + CHANGE_NICKNAME: BitFlag(26), + MANAGE_NICKNAMES: BitFlag(27), + MANAGE_ROLES: BitFlag(28), + MANAGE_WEBHOOKS: BitFlag(29), + MANAGE_EMOJIS_AND_STICKERS: BitFlag(30), + USE_APPLICATION_COMMANDS: BitFlag(31), + REQUEST_TO_SPEAK: BitFlag(32), // TODO: what is permission 33? - MANAGE_THREADS: BigInt(1) << BigInt(34), - USE_PUBLIC_THREADS: BigInt(1) << BigInt(35), - USE_PRIVATE_THREADS: BigInt(1) << BigInt(36), - USE_EXTERNAL_STICKERS: BigInt(1) << BigInt(37), + MANAGE_THREADS: BitFlag(34), + USE_PUBLIC_THREADS: BitFlag(35), + USE_PRIVATE_THREADS: BitFlag(36), + USE_EXTERNAL_STICKERS: BitFlag(37), /** * CUSTOM PERMISSIONS ideas: diff --git a/util/src/util/Rights.ts b/util/src/util/Rights.ts new file mode 100644 index 00000000..a266e4f7 --- /dev/null +++ b/util/src/util/Rights.ts @@ -0,0 +1,73 @@ +import { BitField } from "./BitField"; +import "missing-native-js-functions"; +import { BitFieldResolvable, BitFlag } from "./BitField"; + +var HTTPError: any; + +try { + HTTPError = require("lambert-server").HTTPError; +} catch (e) { + HTTPError = Error; +} + +export type RightResolvable = bigint | number | Rights | RightResolvable[] | RightString; + +type RightString = keyof typeof Rights.FLAGS; +// TODO: just like roles for members, users should have privilidges which combine multiple rights into one and make it easy to assign + +export class Rights extends BitField { + constructor(bits: BitFieldResolvable = 0) { + super(bits); + if (this.bitfield & Rights.FLAGS.OPERATOR) { + this.bitfield = ALL_RIGHTS; + } + } + + static FLAGS = { + OPERATOR: BitFlag(0), // has all rights + MANAGE_APPLICATIONS: BitFlag(1), + MANAGE_GUILDS: BitFlag(2), + MANAGE_MESSAGES: BitFlag(3), // Can't see other messages but delete/edit them in channels that they can see + MANAGE_RATE_LIMITS: BitFlag(4), + MANAGE_ROUTING: BitFlag(5), // can create custom message routes to any channel/guild + MANAGE_TICKETS: BitFlag(6), + MANAGE_USERS: BitFlag(7), + ADD_MEMBERS: BitFlag(8), // can manually add any members in their guilds + BYPASS_RATE_LIMITS: BitFlag(9), + CREATE_APPLICATIONS: BitFlag(10), + CREATE_CHANNELS: BitFlag(11), + CREATE_DMS: BitFlag(12), + CREATE_DM_GROUPS: BitFlag(13), + CREATE_GUILDS: BitFlag(14), + CREATE_INVITES: BitFlag(15), + CREATE_ROLES: BitFlag(16), + CREATE_TEMPLATES: BitFlag(17), + CREATE_WEBHOOKS: BitFlag(18), + JOIN_GUILDS: BitFlag(19), + PIN_MESSAGES: BitFlag(20), + SELF_ADD_REACTIONS: BitFlag(21), + SELF_DELETE_MESSAGES: BitFlag(22), + SELF_EDIT_MESSAGES: BitFlag(23), + SELF_EDIT_NAME: BitFlag(24), + SEND_MESSAGES: BitFlag(25), + USE_SCREEN: BitFlag(26), + USE_VIDEO: BitFlag(27), + USE_VOICE: BitFlag(28), + }; + + any(permission: RightResolvable, checkOperator = true) { + return (checkOperator && super.any(Rights.FLAGS.OPERATOR)) || super.any(permission); + } + + has(permission: RightResolvable, checkOperator = true) { + return (checkOperator && super.has(Rights.FLAGS.OPERATOR)) || super.has(permission); + } + + hasThrow(permission: RightResolvable) { + if (this.has(permission)) return true; + // @ts-ignore + throw new HTTPError(`You are missing the following rights ${permission}`, 403); + } +} + +const ALL_RIGHTS = Object.values(Rights.FLAGS).reduce((total, val) => total | val, BigInt(0)); diff --git a/util/src/util/Token.ts b/util/src/util/Token.ts index 111d59a2..7c4cc61d 100644 --- a/util/src/util/Token.ts +++ b/util/src/util/Token.ts @@ -10,7 +10,10 @@ export function checkToken(token: string, jwtSecret: string): Promise<any> { jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => { if (err || !decoded) return rej("Invalid Token"); - const user = await User.findOne({ id: decoded.id }, { select: ["data", "bot", "disabled", "deleted"] }); + const user = await User.findOne( + { id: decoded.id }, + { select: ["data", "bot", "disabled", "deleted", "rights"] } + ); if (!user) return rej("Invalid Token"); // we need to round it to seconds as it saved as seconds in jwt iat and valid_tokens_since is stored in milliseconds if (decoded.iat * 1000 < new Date(user.data.valid_tokens_since).setSeconds(0, 0)) diff --git a/util/src/util/index.ts b/util/src/util/index.ts index d73bf4ca..9c51d3b8 100644 --- a/util/src/util/index.ts +++ b/util/src/util/index.ts @@ -12,6 +12,7 @@ export * from "./MessageFlags"; export * from "./Permissions"; export * from "./RabbitMQ"; export * from "./Regex"; +export * from "./Rights"; export * from "./Snowflake"; export * from "./String"; export * from "./Array"; |