From d6d7c15bac0f5e84437e4ef740d70f4c30502f38 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Mon, 20 Jan 2020 01:50:12 +0900 Subject: [PATCH] Add mark-admin command (#5705) * mark-admin command * no cli --- .github/CODEOWNERS | 1 - docs/docker.en.md | 2 +- docs/docker.fr.md | 2 +- docs/docker.ja.md | 2 +- docs/manage.en.md | 8 ++------ docs/manage.fr.md | 8 ++------ docs/manage.ja.md | 8 ++------ src/tools/mark-admin.ts | 32 ++++++++++++++++++++++++++++++++ 8 files changed, 41 insertions(+), 22 deletions(-) create mode 100644 src/tools/mark-admin.ts diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 5dd72cc46..2a41c12c7 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -6,7 +6,6 @@ /.github/ @syuilo @AyaMorisawa @acid-chicken /.vscode/ @acid-chicken /assets/ @syuilo # @tamaina -/cli/ @syuilo /docs/ @syuilo /docs/*.en.md @AyaMorisawa # @skid9000 # /docs/*.fr.md @BoFFire diff --git a/docs/docker.en.md b/docs/docker.en.md index a1ddecf62..8920feb60 100644 --- a/docs/docker.en.md +++ b/docs/docker.en.md @@ -88,7 +88,7 @@ Just `docker-compose up -d`. GLHF! 7. `docker-compose stop && docker-compose up -d` ### How to execute [cli commands](manage.en.md): -`docker-compose run --rm web node cli/mark-admin @example` +`docker-compose run --rm web node built/tools/mark-admin @example` ---------------------------------------------------------------- diff --git a/docs/docker.fr.md b/docs/docker.fr.md index c43ab4f65..d2795a590 100644 --- a/docs/docker.fr.md +++ b/docs/docker.fr.md @@ -55,7 +55,7 @@ Utilisez la commande `docker-compose up -d`. GLHF! 7. `docker-compose stop && docker-compose up -d` ### Comment exécuter des [commandes](manage.fr.md) -`docker-compose run --rm web node cli/mark-admin @example` +`docker-compose run --rm web node built/tools/mark-admin @example` ### Configuration d'ElasticSearch (pour la fonction de recherche) *1.* Préparation de l'environnement diff --git a/docs/docker.ja.md b/docs/docker.ja.md index cc26caadf..2e2abfbed 100644 --- a/docs/docker.ja.md +++ b/docs/docker.ja.md @@ -89,7 +89,7 @@ docker-compose run --rm web yarn run init ### cliコマンドを実行する方法: -`docker-compose run --rm web node cli/mark-admin @example` +`docker-compose run --rm web node built/tools/mark-admin @example` ---------------------------------------------------------------- diff --git a/docs/manage.en.md b/docs/manage.en.md index 85c965a16..d310e9531 100644 --- a/docs/manage.en.md +++ b/docs/manage.en.md @@ -5,14 +5,10 @@ coming soon ## Mark as 'admin' user ``` shell -node cli/mark-admin (User-ID or Username) +node built/tools/mark-admin (Username) ``` e.g. ``` shell -# By id -node cli/mark-admin 57d01a501fdf2d07be417afe - -# By username -node cli/suspend @syuilo +node built/tools/mark-admin @syuilo ``` diff --git a/docs/manage.fr.md b/docs/manage.fr.md index bf38e5ed9..0b2b7ffc1 100644 --- a/docs/manage.fr.md +++ b/docs/manage.fr.md @@ -5,14 +5,10 @@ coming soon ## Marquer un utilisateur en tant que 'admin' ``` shell -node cli/mark-admin (ID utilisateur ou nom d'utilisateur) +node built/tools/mark-admin (nom d'utilisateur) ``` Exemple : ``` shell -# Par id -node cli/mark-admin 57d01a501fdf2d07be417afe - -# Par nom d'utilisateur -node cli/suspend @syuilo +node built/tools/mark-admin @syuilo ``` diff --git a/docs/manage.ja.md b/docs/manage.ja.md index 4a9a3e261..55596add1 100644 --- a/docs/manage.ja.md +++ b/docs/manage.ja.md @@ -5,14 +5,10 @@ coming soon ## 管理者ユーザーを設定する ``` shell -node cli/mark-admin (ユーザーID または ユーザー名) +node built/tools/mark-admin (ユーザー名) ``` 例: ``` shell -# ユーザーID -node cli/mark-admin 57d01a501fdf2d07be417afe - -# ユーザー名 -node cli/mark-admin @syuilo +node built/tools/mark-admin @syuilo ``` diff --git a/src/tools/mark-admin.ts b/src/tools/mark-admin.ts new file mode 100644 index 000000000..5844bb464 --- /dev/null +++ b/src/tools/mark-admin.ts @@ -0,0 +1,32 @@ +import { initDb } from '../db/postgre'; +import { getRepository } from 'typeorm'; +import { User } from '../models/entities/user'; + +async function main(username: string) { + if (!username) throw `username required`; + username = username.replace(/^@/, ''); + + await initDb(); + const Users = getRepository(User); + + const res = await Users.update({ + usernameLower: username.toLowerCase(), + host: null + }, { + isAdmin: true + }); + + if (res.affected !== 1) { + throw 'Failed'; + } +} + +const args = process.argv.slice(2); + +main(args[0]).then(() => { + console.log('Success'); + process.exit(0); +}).catch(e => { + console.error(`Error: ${e.message || e}`); + process.exit(1); +});