fix(types): permissions to string/bigint to prevent int scalability concerns

This commit is contained in:
41666 2020-12-15 15:54:39 -05:00
parent c55ce3b828
commit 6d1037f25e
8 changed files with 77 additions and 76 deletions

View file

@ -1,8 +1,9 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "./dist", "outDir": "./dist",
"lib": ["esnext", "webworker"], "lib": ["esnext", "webworker", "ES2020.BigInt", "ES2020.Promise"],
"types": ["@cloudflare/workers-types"] "types": ["@cloudflare/workers-types"],
"target": "ES2019"
}, },
"include": [ "include": [
"./*.ts", "./*.ts",

View file

@ -5,7 +5,7 @@ export const demoData: Role[] = [
id: '557812805546541066', id: '557812805546541066',
name: 'a cute role ♡', name: 'a cute role ♡',
color: 0xd19494, color: 0xd19494,
permissions: 0, permissions: '0',
safety: 0, safety: 0,
managed: false, managed: false,
position: 0, position: 0,
@ -14,7 +14,7 @@ export const demoData: Role[] = [
id: '557812901717737472', id: '557812901717737472',
name: 'a vanity role ♡', name: 'a vanity role ♡',
color: 0xd1d194, color: 0xd1d194,
permissions: 0, permissions: '0',
safety: 0, safety: 0,
managed: false, managed: false,
position: 0, position: 0,
@ -23,7 +23,7 @@ export const demoData: Role[] = [
id: '557812915386843170', id: '557812915386843170',
name: 'a brave role ♡', name: 'a brave role ♡',
color: 0x94d194, color: 0x94d194,
permissions: 0, permissions: '0',
safety: 0, safety: 0,
managed: false, managed: false,
position: 0, position: 0,
@ -32,7 +32,7 @@ export const demoData: Role[] = [
id: '557824893241131029', id: '557824893241131029',
name: 'a proud role ♡', name: 'a proud role ♡',
color: 0x94d1d1, color: 0x94d1d1,
permissions: 0, permissions: '0',
safety: 0, safety: 0,
managed: false, managed: false,
position: 0, position: 0,
@ -41,7 +41,7 @@ export const demoData: Role[] = [
id: '557824994269200384', id: '557824994269200384',
name: 'a wonderful role ♡', name: 'a wonderful role ♡',
color: 0x9494d1, color: 0x9494d1,
permissions: 0, permissions: '0',
safety: 0, safety: 0,
managed: false, managed: false,
position: 0, position: 0,
@ -50,7 +50,7 @@ export const demoData: Role[] = [
id: '557825026406088717', id: '557825026406088717',
name: 'a 日本語 role ♡', name: 'a 日本語 role ♡',
color: 0xd194d1, color: 0xd194d1,
permissions: 0, permissions: '0',
safety: 0, safety: 0,
managed: false, managed: false,
position: 0, position: 0,

View file

@ -16,7 +16,7 @@ import {
export const roleCategory: Role[] = [ export const roleCategory: Role[] = [
{ {
id: 'aaa', id: 'aaa',
permissions: 0, permissions: '0',
name: 'She/Her', name: 'She/Her',
color: 0xffc0cb, color: 0xffc0cb,
position: 1, position: 1,
@ -25,7 +25,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'bbb', id: 'bbb',
permissions: 0, permissions: '0',
name: 'He/Him', name: 'He/Him',
color: 0xc0ebff, color: 0xc0ebff,
position: 2, position: 2,
@ -34,7 +34,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'ccc', id: 'ccc',
permissions: 0, permissions: '0',
name: 'They/Them', name: 'They/Them',
color: 0xc0ffd5, color: 0xc0ffd5,
position: 3, position: 3,
@ -43,7 +43,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'ddd', id: 'ddd',
permissions: 0, permissions: '0',
name: 'Reee', name: 'Reee',
color: 0xff0000, color: 0xff0000,
position: 4, position: 4,
@ -52,7 +52,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'eee', id: 'eee',
permissions: 0, permissions: '0',
name: 'black but actually bravely default', name: 'black but actually bravely default',
color: 0x000000, color: 0x000000,
position: 5, position: 5,
@ -61,7 +61,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'fff', id: 'fff',
permissions: 0, permissions: '0',
name: 'b̻͌̆̽ͣ̃ͭ̊l͚̥͙̔ͨ̊aͥć͕k͎̟͍͕ͥ̋ͯ̓̈̉̋i͛̄̔͂̚̚҉̳͈͔̖̼̮ṣ̤̗̝͊̌͆h͈̭̰͔̥̯ͅ', name: 'b̻͌̆̽ͣ̃ͭ̊l͚̥͙̔ͨ̊aͥć͕k͎̟͍͕ͥ̋ͯ̓̈̉̋i͛̄̔͂̚̚҉̳͈͔̖̼̮ṣ̤̗̝͊̌͆h͈̭̰͔̥̯ͅ',
color: 0x1, color: 0x1,
position: 6, position: 6,
@ -70,7 +70,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'unsafe1', id: 'unsafe1',
permissions: 0, permissions: '0',
name: 'too high', name: 'too high',
color: 0xff0088, color: 0xff0088,
position: 7, position: 7,
@ -79,7 +79,7 @@ export const roleCategory: Role[] = [
}, },
{ {
id: 'unsafe2', id: 'unsafe2',
permissions: 0x00000008 | 0x10000000, permissions: String(0x00000008 | 0x10000000),
name: 'too strong', name: 'too strong',
color: 0x00ff88, color: 0x00ff88,
position: 8, position: 8,
@ -100,7 +100,7 @@ export const mockCategory: Category = {
export const roleCategory2: Role[] = [ export const roleCategory2: Role[] = [
{ {
id: 'ddd2', id: 'ddd2',
permissions: 0, permissions: '0',
name: 'red', name: 'red',
color: 0xff0000, color: 0xff0000,
position: 9, position: 9,
@ -109,7 +109,7 @@ export const roleCategory2: Role[] = [
}, },
{ {
id: 'eee2', id: 'eee2',
permissions: 0, permissions: '0',
name: 'green', name: 'green',
color: 0x00ff00, color: 0x00ff00,
position: 10, position: 10,
@ -142,9 +142,7 @@ export const guild: Guild = {
name: 'emoji megaporium', name: 'emoji megaporium',
id: '421896162539470888', id: '421896162539470888',
icon: '3372fd895ed913b55616c5e49cd50e60', icon: '3372fd895ed913b55616c5e49cd50e60',
ownerid: 'bbb', roles: [],
membercount: 23453,
splash: '',
}; };
export const guildMap: { [x: string]: Guild } = { export const guildMap: { [x: string]: Guild } = {
@ -153,25 +151,19 @@ export const guildMap: { [x: string]: Guild } = {
name: 'Roleypoly', name: 'Roleypoly',
id: '203493697696956418', id: '203493697696956418',
icon: 'ff08d36f5aee1ff48f8377b65d031ab0', icon: 'ff08d36f5aee1ff48f8377b65d031ab0',
ownerid: 'bbb', roles: [],
membercount: 23453,
splash: '',
}, },
'chamber of secrets': { 'chamber of secrets': {
name: 'chamber of secrets', name: 'chamber of secrets',
id: 'aaa', id: 'aaa',
icon: '', icon: '',
ownerid: 'bbb', roles: [],
membercount: 23453,
splash: '',
}, },
Eclipse: { Eclipse: {
name: 'Eclipse', name: 'Eclipse',
id: '408821059161423873', id: '408821059161423873',
icon: '49dfdd8b2456e2977e80a8b577b19c0d', icon: '49dfdd8b2456e2977e80a8b577b19c0d',
ownerid: 'bbb', roles: [],
membercount: 23453,
splash: '',
}, },
}; };

View file

@ -5,16 +5,17 @@ import { hasPermission, hasPermissionOrAdmin, permissions } from './hasPermissio
const roles: Role[] = [ const roles: Role[] = [
{ {
...guildRoles.rolesList[0], ...guildRoles.rolesList[0],
permissions: permissions.ADMINISTRATOR, permissions: String(permissions.ADMINISTRATOR),
}, },
{ {
...guildRoles.rolesList[0], ...guildRoles.rolesList[0],
permissions: permissions: String(
permissions.SPEAK | permissions.BAN_MEMBERS | permissions.CHANGE_NICKNAME, permissions.SPEAK | permissions.BAN_MEMBERS | permissions.CHANGE_NICKNAME
),
}, },
{ {
...guildRoles.rolesList[0], ...guildRoles.rolesList[0],
permissions: permissions.BAN_MEMBERS, permissions: String(permissions.BAN_MEMBERS),
}, },
]; ];

View file

@ -1,47 +1,53 @@
import { Role } from '../types'; import { Role } from '../types';
export const evaluatePermission = (haystack: number, needle: number): boolean => { export const evaluatePermission = <T extends number | bigint>(
haystack: T,
needle: T
): boolean => {
return (haystack & needle) === needle; return (haystack & needle) === needle;
}; };
export const hasPermission = (roles: Role[], permission: number): boolean => { export const hasPermission = (roles: Role[], permission: bigint): boolean => {
const aggregateRoles = roles.reduce((acc, role) => acc | role.permissions, 0); const aggregateRoles = roles.reduce(
(acc, role) => acc | BigInt(role.permissions),
BigInt(0)
);
return evaluatePermission(aggregateRoles, permission); return evaluatePermission(aggregateRoles, permission);
}; };
export const hasPermissionOrAdmin = (roles: Role[], permission: number): boolean => export const hasPermissionOrAdmin = (roles: Role[], permission: bigint): boolean =>
hasPermission(roles, permission | permissions.ADMINISTRATOR); hasPermission(roles, permission | permissions.ADMINISTRATOR);
export const permissions = { export const permissions = {
CREATE_INSTANT_INVITE: 0x1, CREATE_INSTANT_INVITE: BigInt(0x1),
KICK_MEMBERS: 0x2, KICK_MEMBERS: BigInt(0x2),
BAN_MEMBERS: 0x4, BAN_MEMBERS: BigInt(0x4),
ADMINISTRATOR: 0x8, ADMINISTRATOR: BigInt(0x8),
MANAGE_CHANNELS: 0x10, MANAGE_CHANNELS: BigInt(0x10),
MANAGE_GUILD: 0x20, MANAGE_GUILD: BigInt(0x20),
ADD_REACTIONS: 0x40, ADD_REACTIONS: BigInt(0x40),
VIEW_AUDIT_LOG: 0x80, VIEW_AUDIT_LOG: BigInt(0x80),
VIEW_CHANNEL: 0x400, VIEW_CHANNEL: BigInt(0x400),
SEND_MESSAGES: 0x800, SEND_MESSAGES: BigInt(0x800),
SEND_TTS_MESSAGES: 0x1000, SEND_TTS_MESSAGES: BigInt(0x1000),
MANAGE_MESSAGES: 0x2000, MANAGE_MESSAGES: BigInt(0x2000),
EMBED_LINKS: 0x4000, EMBED_LINKS: BigInt(0x4000),
ATTACH_FILES: 0x8000, ATTACH_FILES: BigInt(0x8000),
READ_MESSAGE_HISTORY: 0x10000, READ_MESSAGE_HISTORY: BigInt(0x10000),
MENTION_EVERYONE: 0x20000, MENTION_EVERYONE: BigInt(0x20000),
USE_EXTERNAL_EMOJIS: 0x40000, USE_EXTERNAL_EMOJIS: BigInt(0x40000),
VIEW_GUILD_INSIGHTS: 0x80000, VIEW_GUILD_INSIGHTS: BigInt(0x80000),
CONNECT: 0x100000, CONNECT: BigInt(0x100000),
SPEAK: 0x200000, SPEAK: BigInt(0x200000),
MUTE_MEMBERS: 0x400000, MUTE_MEMBERS: BigInt(0x400000),
DEAFEN_MEMBERS: 0x800000, DEAFEN_MEMBERS: BigInt(0x800000),
MOVE_MEMBERS: 0x1000000, MOVE_MEMBERS: BigInt(0x1000000),
USE_VAD: 0x2000000, USE_VAD: BigInt(0x2000000),
PRIORITY_SPEAKER: 0x100, PRIORITY_SPEAKER: BigInt(0x100),
STREAM: 0x200, STREAM: BigInt(0x200),
CHANGE_NICKNAME: 0x4000000, CHANGE_NICKNAME: BigInt(0x4000000),
MANAGE_NICKNAMES: 0x8000000, MANAGE_NICKNAMES: BigInt(0x8000000),
MANAGE_ROLES: 0x10000000, MANAGE_ROLES: BigInt(0x10000000),
MANAGE_WEBHOOKS: 0x20000000, MANAGE_WEBHOOKS: BigInt(0x20000000),
MANAGE_EMOJIS: 0x40000000, MANAGE_EMOJIS: BigInt(0x40000000),
}; };

View file

@ -2,6 +2,7 @@ import chroma from 'chroma-js';
import * as React from 'react'; import * as React from 'react';
import { FaCheck, FaTimes } from 'react-icons/fa'; import { FaCheck, FaTimes } from 'react-icons/fa';
import { Role as RPCRole, RoleSafety } from 'roleypoly/common/types'; import { Role as RPCRole, RoleSafety } from 'roleypoly/common/types';
import { evaluatePermission, permissions } from 'roleypoly/common/utils/hasPermission';
import { numberToChroma } from 'roleypoly/design-system/atoms/colors'; import { numberToChroma } from 'roleypoly/design-system/atoms/colors';
import * as styled from './Role.styled'; import * as styled from './Role.styled';
@ -67,12 +68,12 @@ const disabledReason = (role: RPCRole) => {
case RoleSafety.HIGHERTHANBOT: case RoleSafety.HIGHERTHANBOT:
return `This role is above Roleypoly's own role.`; return `This role is above Roleypoly's own role.`;
case RoleSafety.DANGEROUSPERMISSIONS: case RoleSafety.DANGEROUSPERMISSIONS:
const { permissions } = role; const rolePermissions = BigInt(role.permissions);
let permissionHits: string[] = []; let permissionHits: string[] = [];
(permissions & 0x00000008) === 0x00000008 && evaluatePermission(rolePermissions, permissions.ADMINISTRATOR) &&
permissionHits.push('Administrator'); permissionHits.push('Administrator');
(permissions & 0x10000000) === 0x10000000 && evaluatePermission(rolePermissions, permissions.MANAGE_ROLES) &&
permissionHits.push('Manage Roles'); permissionHits.push('Manage Roles');
return `This role has unsafe permissions: ${permissionHits.join(', ')}`; return `This role has unsafe permissions: ${permissionHits.join(', ')}`;

View file

@ -12,7 +12,7 @@ const adminRoles: Role[] = [
{ {
id: 'roley2', id: 'roley2',
name: 'Admin', name: 'Admin',
permissions: 0, permissions: '0',
color: chroma('hotpink').num(), color: chroma('hotpink').num(),
position: -1, position: -1,
managed: true, managed: true,
@ -21,7 +21,7 @@ const adminRoles: Role[] = [
{ {
id: 'roley3', id: 'roley3',
name: 'Moderator', name: 'Moderator',
permissions: 0, permissions: '0',
color: chroma('lime').num(), color: chroma('lime').num(),
position: -1, position: -1,
managed: true, managed: true,
@ -32,7 +32,7 @@ const adminRoles: Role[] = [
const roleypolyRole: Role = { const roleypolyRole: Role = {
id: 'roley', id: 'roley',
name: 'Roleypoly', name: 'Roleypoly',
permissions: 0, permissions: '0',
color: chroma(palette.taupe500).num(), color: chroma(palette.taupe500).num(),
position: -1, position: -1,
managed: true, managed: true,

View file

@ -1,6 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es2019", "target": "es2020",
"module": "commonjs", "module": "commonjs",
"lib": [ "lib": [
"dom", "dom",