feat: list-users & set-user script

This commit is contained in:
diced 2022-11-10 19:27:23 -08:00
parent 51cfb9062a
commit 67bb9cd4a5
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
4 changed files with 119 additions and 1 deletions

View file

@ -53,6 +53,7 @@ COPY --from=builder /build/.next ./.next
COPY --from=builder /build/node_modules ./node_modules
COPY --from=builder /build/next.config.js ./next.config.js
COPY --from=builder /build/esbuild.config.js ./esbuild.config.js
COPY --from=builder /build/src ./src
COPY --from=builder /build/dist ./dist
COPY --from=builder /build/prisma ./prisma

View file

@ -18,7 +18,9 @@
"docker:down": "docker-compose down",
"docker:build-dev": "docker-compose --file docker-compose.dev.yml up --build",
"scripts:read-config": "npm-run-all build:server && node dist/scripts/read-config",
"scripts:import-dir": "npm-run-all build:server && node dist/scripts/import-dir"
"scripts:import-dir": "npm-run-all build:server && node dist/scripts/import-dir",
"scripts:list-users": "npm-run-all build:server && node dist/scripts/list-users",
"scripts:set-user": "npm-run-all build:server && node dist/scripts/set-user"
},
"dependencies": {
"@dicedtomato/mantine-data-grid": "0.0.23",

36
src/scripts/list-users.ts Normal file
View file

@ -0,0 +1,36 @@
import config from '../lib/config';
import { PrismaClient } from '@prisma/client';
import { migrations } from '../server/util';
async function main() {
const extras = (process.argv[2] ?? '').split(',');
const specificId = process.argv[3] ?? null;
const where = {
id: specificId ? Number(specificId) : null,
};
const select = {
username: true,
administrator: true,
id: true,
};
for (let i = 0; i !== extras.length; ++i) {
const e = extras[i];
if (e) select[e] = true;
}
process.env.DATABASE_URL = config.core.database_url;
await migrations();
const prisma = new PrismaClient();
const users = await prisma.user.findMany({
where: specificId ? where : undefined,
select,
});
console.log(JSON.stringify(users, null, 2));
}
main();

79
src/scripts/set-user.ts Normal file
View file

@ -0,0 +1,79 @@
import config from '../lib/config';
import { PrismaClient } from '@prisma/client';
import { migrations } from '../server/util';
import { hash } from 'argon2';
const SUPPORTED_FIELDS = [
'username',
'password',
'administrator',
'avatar',
'token',
'administrator',
'superAdmin',
'systemTheme',
'embedTitle',
'embedColor',
'embedSiteName',
'ratelimit',
'domains',
];
async function main() {
const args = process.argv.slice(2);
if (!args[0]) return console.error('no user id specified (run list-users script to see all user ids)');
if (!args[1]) return console.error('no property specified');
if (!args[2]) return console.error('no value specified');
if (!SUPPORTED_FIELDS.includes(args[1])) return console.error(`property ${args[1]} is not supported`);
process.env.DATABASE_URL = config.core.database_url;
await migrations();
const prisma = new PrismaClient();
const user = await prisma.user.findFirst({
where: {
id: Number(args[0]),
},
});
if (!user) return console.error('user not found');
let parsed;
if (args[2] === 'true') parsed = true;
if (args[2] === 'false') parsed = false;
if (args[1] === 'password') {
parsed = await hash(args[2]);
}
if (args[1] === 'domains') {
parsed = args[2].split(',');
}
if (args[1] === 'ratelimit') {
const try_date = new Date(args[2]);
if (isNaN(try_date.getTime())) return console.error('invalid date');
parsed = try_date;
}
const data = {
[args[1]]: parsed,
};
await prisma.user.update({
where: {
id: user.id,
},
data,
});
if (args[1] === 'password') {
parsed = '***';
}
console.log(`Updated user ${user.id} with ${args[1]} = ${parsed}`);
}
main();