Skip to content

Node.js based forum software built for the modern web

feat: recent logins sessions table in dashbaord subpage

Julian Lam2f89b0d791fa
diff --git a/public/language/en-GB/admin/dashboard.json b/public/language/en-GB/admin/dashboard.json
index 6406c8c985..b5ff928dd4 100644
--- a/public/language/en-GB/admin/dashboard.json
+++ b/public/language/en-GB/admin/dashboard.json
@@ -81,5 +81,7 @@
"back-to-dashboard": "Back to Dashboard",
"details.no-users": "No users have joined within the selected timeframe",
"details.no-topics": "No topics have been posted within the selected timeframe",
- "details.no-logins": "No logins have been recorded within the selected timeframe"
+ "details.no-logins": "No logins have been recorded within the selected timeframe",
+ "details.logins-static": "NodeBB only saves session data for %1 days, and so this table below will only show the most recently active sessions",
+ "details.logins-login-time": "Login Time"
}
diff --git a/src/controllers/admin/dashboard.js b/src/controllers/admin/dashboard.js
index d864706c41..d4a75cfa27 100644
--- a/src/controllers/admin/dashboard.js
+++ b/src/controllers/admin/dashboard.js
@@ -242,11 +242,27 @@ dashboardController.getLogins = async (req, res) => {
month: stats[0].thismonth,
};
+ // List recent sessions
+ const start = Date.now() - (1000 * 60 * 60 * 24 * meta.config.loginDays);
+ const uids = await db.getSortedSetRangeByScore('users:online', 0, 500, start, Date.now());
+ const usersData = await user.getUsersData(uids);
+ let sessions = await Promise.all(uids.map(async (uid) => {
+ const sessions = await user.auth.getSessions(uid);
+ sessions.forEach((session) => {
+ session.user = usersData[uids.indexOf(uid)];
+ });
+
+ return sessions;
+ }));
+ sessions = _.flatten(sessions).sort((a, b) => b.datetime - a.datetime);
+
res.render('admin/dashboard/logins', {
set: 'logins',
query: req.query,
stats,
summary,
+ sessions,
+ loginDays: meta.config.loginDays,
});
};
diff --git a/src/views/admin/dashboard/logins.tpl b/src/views/admin/dashboard/logins.tpl
index ec9f431882..9f3be1d1d6 100644
--- a/src/views/admin/dashboard/logins.tpl
+++ b/src/views/admin/dashboard/logins.tpl
@@ -7,5 +7,29 @@
<!-- IMPORT admin/partials/dashboard/graph.tpl -->
<!-- IMPORT admin/partials/dashboard/stats.tpl -->
+
+ <div class="alert alert-info">[[admin/dashboard:details.logins-static, {loginDays}]]</div>
+ <table class="table table-striped">
+ <thead>
+ <th class="text-muted">[[admin/manage/users:users.username]]</th>
+ <th data-sort="joindate">[[admin/dashboard:details.logins-login-time]]</th>
+ </thead>
+ <tbody>
+ {{{ if !sessions.length}}}
+ <tr>
+ <td colspan=4" class="text-center"><em>[[admin/dashboard:details.no-logins]]</em></td>
+ </tr>
+ {{{ end }}}
+ {{{ each sessions }}}
+ <tr>
+ <td>
+ <a href="{config.relative_path}/uid/{./user.uid}">{buildAvatar(./user, "sm", true)} {../username}</a>
+ {function.userAgentIcons} {../browser} {../version} on {../platform}
+ </td>
+ <td><span class="timeago" title="{./datetimeISO}"></span></td>
+ </tr>
+ {{{ end }}}
+ </tbody>
+ </table>
</div>
</div>
\ No newline at end of file