chore: some naming changes

This commit is contained in:
TheClashFruit 2024-08-30 18:27:10 +02:00
parent 4af5a5e4b5
commit a65bb1bb5d
Signed by: TheClashFruit
GPG key ID: 09BB24C34C2F3204
4 changed files with 10 additions and 158 deletions

View file

@ -29,8 +29,8 @@ import Dropdown from './Dropdown';
import { useUser } from '@/context/UserContext';
import {
Roles,
hasRole
Permission,
hasPermission
} from '@/utils/permissions';
export default function NavBar({ currentPage }: { currentPage: string }) {
@ -135,7 +135,7 @@ export default function NavBar({ currentPage }: { currentPage: string }) {
<ul>
{(isLoggedIn && user) && (
<li>
{hasRole(user.permissions, Roles.Admin) && (
{hasPermission(user.permissions, Permission.Admin) && (
<Dropdown items={[
{
icon: User,

View file

@ -3,144 +3,13 @@ import { useUser } from '@/context/UserContext';
import { getCookieFromContext } from '@/utils/cookies';
import { useRef } from 'react';
interface Param {
name: string;
required: boolean;
}
interface Command {
description: string;
params?: Param[];
commands?: Commands;
action: () => void;
}
interface Commands {
[key: string]: Command;
}
export default function Admin() {
const { user, isLoggedIn } = useUser();
const inputRef = useRef<HTMLInputElement>(null);
const terminalRef = useRef<HTMLDivElement>(null);
if (!isLoggedIn) {
return null;
}
const commands: Commands = {
help: {
description: 'Displays a list of available commands.',
action: () => {
terminalRef.current!.insertAdjacentHTML('beforeend', '<code>Available commands:</code>');
Object.keys(commands).forEach((command) => {
terminalRef.current!.insertAdjacentHTML('beforeend', `<code>${command} - ${commands[command].description}</code>`);
});
}
},
whoami: {
description: 'Who am I?',
action: () => {
terminalRef.current!.insertAdjacentHTML('beforeend', `<code>${user.names.global_name}</code>`);
}
},
clear: {
description: 'Clear the terminal.',
action: () => {
terminalRef.current!.innerHTML = '';
}
},
sudo: {
description: 'Execute a command as another user.',
commands: {
rm: {
description: 'Remove a file.',
commands: {
'-rf': {
description: 'Remove a file forcefully.',
commands: {
'/': {
description: 'Remove the root directory.',
action: () => {
terminalRef.current!.insertAdjacentHTML('beforeend', '<code style="color: red;">...</code>');
const interval = setInterval(() => {
terminalRef.current!.insertAdjacentHTML('beforeend', '<code style="color: red;">...</code>');
}, 1000);
setTimeout(() => {
clearInterval(interval);
document.body.innerHTML = '';
}, 5000);
}
}
},
action: () => {}
}
},
action: () => {}
}
},
action: () => {
terminalRef.current!.insertAdjacentHTML('beforeend', '<code>doas</code>');
}
},
user: {
description: 'Display user information.',
commands: {
list: {
description: 'List users.',
action: () => {
alert('List users');
}
}
},
action: () => {}
},
};
const executeCommand = (commandTree: any, args: string[]) => {
if (args.length === 0) {
if (commandTree.action) {
commandTree.action();
} else {
terminalRef.current!.insertAdjacentHTML('beforeend', '<code>Command executed but no action defined.</code>');
}
return;
}
const nextCommand = args[0];
const remainingArgs = args.slice(1);
if (commandTree.commands && commandTree.commands[nextCommand]) {
executeCommand(commandTree.commands[nextCommand], remainingArgs);
} else {
terminalRef.current!.insertAdjacentHTML('beforeend', `<code>\`${nextCommand}\`: Command Not Found</code>`);
}
};
const parseCommands = (e: any) => {
e.preventDefault();
inputRef.current?.focus();
const input = inputRef.current?.value.trim();
if (!input) return;
const [mainCommand, ...subCommands] = input.split(' ');
inputRef.current!.value = '';
terminalRef.current!.insertAdjacentHTML('beforeend', `<code>theclashfruit@crss.cc ~$ <span style="color: white">${input}</span></code>`);
if (commands[mainCommand]) {
executeCommand(commands[mainCommand], subCommands);
} else {
terminalRef.current!.insertAdjacentHTML('beforeend', `<code>\`${mainCommand}\`: Command Not Found</code>`);
}
};
return (
<>
<PageContent>
@ -149,21 +18,6 @@ export default function Admin() {
<p>What are we doin&apos; today?</p>
<h2>Terminal</h2>
<div style={{ background: 'black', color: 'green', border: '1px solid grey', borderRadius: '.5rem', marginBottom: '1rem' }}>
<div style={{ minHeight: '48ch', maxHeight: '48ch', overflowY: 'scroll', padding: '.5rem', borderTopRightRadius: '.5rem', borderTopLeftRadius: '.5rem', display: 'flex', flexDirection: 'column' }} ref={terminalRef}>
<code>
<span style={{ color: 'white' }}>CRSS AdminShell</span> <br />
<span style={{ color: 'white' }}>Copyright (C) 2024 CRSS. All rights reserved.</span> <br /><br />
<span style={{ color: 'white' }}>Run `help` for commands.</span> <br />
</code>
</div>
<form style={{ display: 'flex', gap: '.5rem', padding: '.5rem' }} onSubmit={parseCommands}>
<label htmlFor="prompt">{user.names.username}@crss.cc ~$</label> <input id="prompt" type="text" style={{ flex: '1', background: 'transparent', color: 'white', fontSize: '1em', border: 'none' }} ref={inputRef} />
</form>
</div>
<h2>Your User in JSON Format</h2>
<p>In case you need it for some reason.</p>

View file

@ -5,8 +5,8 @@ import PageContent from '@/components/PageContent';
import Database from '@/lib/Database';
import {
Roles,
hasRole
Permission,
hasPermission
} from '@/utils/permissions';
import Image from 'next/image';
@ -24,8 +24,7 @@ export default function User({ user }: { user: any }) {
<Image src={`https://cdn.discordapp.com/avatars/${user.did}/a_${user.avatar}.png`} alt={user.global_name} width={128} height={128} />
<ul>
<li>Admin: {hasRole(user.permissions, Roles.Admin) ? 'Yes' : 'No'}</li>
<li>Plus: {hasRole(user.permissions, Roles.Plus) ? 'Yes' : 'No'}</li>
<li>Admin: {hasPermission(user.permissions, Permission.Admin) ? 'Yes' : 'No'}</li>
</ul>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Deserunt perferendis exercitationem aliquid? Corrupti, veniam nam quis, quas, reprehenderit similique perspiciatis veritatis consectetur quidem omnis iste placeat quod! Dolore, labore est.</p>

View file

@ -1,6 +1,5 @@
export enum Roles {
Admin = 1 << 0,
Plus = 1 << 1
export enum Permission {
Admin = 1 << 0
}
export enum Role {
@ -8,6 +7,6 @@ export enum Role {
Admin = 'admin',
}
export function hasRole(permissions: number, role: Roles): boolean {
return (permissions & role) === role;
export function hasPermission(permissions: number, permission: Permission): boolean {
return (permissions & permission) === permission;
}