From 826a2466b7a72cb2f28a0e36057d0bb924157123 Mon Sep 17 00:00:00 2001
From: MeiMei <30769358+mei23@users.noreply.github.com>
Date: Fri, 11 Jan 2019 08:07:09 +0900
Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E8=80=85=E3=81=8C=E3=82=B5?=
 =?UTF-8?q?=E3=82=A4=E3=83=B3=E3=82=A4=E3=83=B3=E5=B1=A5=E6=AD=B4=E3=82=92?=
 =?UTF-8?q?=E5=8F=82=E7=85=A7=E3=81=A7=E3=81=8D=E3=82=8B=E3=83=84=E3=83=BC?=
 =?UTF-8?q?=E3=83=AB=20(#3870)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 管理者がサインイン履歴を参照できるツール

* remove debug code
---
 src/tools/show-signin-history.ts | 57 ++++++++++++++++++++++++++++++++
 1 file changed, 57 insertions(+)
 create mode 100644 src/tools/show-signin-history.ts

diff --git a/src/tools/show-signin-history.ts b/src/tools/show-signin-history.ts
new file mode 100644
index 0000000000..e770710322
--- /dev/null
+++ b/src/tools/show-signin-history.ts
@@ -0,0 +1,57 @@
+// node built/tools/show-signin-history username
+//  => {Success} {Date} {IPAddrsss}
+
+// node built/tools/show-signin-history username user-agent,x-forwarded-for
+//  with user-agent and x-forwarded-for
+
+// node built/tools/show-signin-history username all
+//  with full request headers
+
+import User from '../models/user';
+import Signin from '../models/signin';
+
+async function main(username: string, headers: string[]) {
+	const user = await User.findOne({
+		host: null,
+		usernameLower: username.toLowerCase(),
+	});
+
+	if (user === null) throw 'User not found';
+
+	const history = await Signin.find({
+		userId: user._id
+	});
+
+	for (const signin of history) {
+		console.log(`${signin.success ? 'OK' : 'NG'} ${signin.createdAt ? signin.createdAt.toISOString() : 'Unknown'} ${signin.ip}`);
+
+		// headers
+		if (headers != null) {
+			for (const key of Object.keys(signin.headers)) {
+				if (headers.includes('all') || headers.includes(key)) {
+					console.log(`   ${key}: ${signin.headers[key]}`);
+				}
+			}
+		}
+	}
+}
+
+// get args
+const args = process.argv.slice(2);
+
+let username = args[0];
+let headers: string[];
+
+if (args[1] != null) {
+	headers = args[1].split(/,/).map(header => header.toLowerCase());
+}
+
+// normalize args
+username = username.replace(/^@/, '');
+
+main(username, headers).then(() => {
+	process.exit(0);
+}).catch(e => {
+	console.warn(e);
+	process.exit(1);
+});