-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3296580
commit eaff0ee
Showing
7 changed files
with
145 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
lib/webserver/routers/dashboard/forbid-if-not-super-user.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const MODULE_NAME = 'WEBSERVER.ROUTERS.DASHBOARD.FORBID-IF-NOT-SUPER-USER'; | ||
|
||
const logger = require('../../../logger'); | ||
|
||
/** | ||
* | ||
* @param {object} options | ||
* @param {string} [options.redirectUrl] - redirect to this url on forbidden | ||
* @returns {import('express').RequestHandler} | ||
*/ | ||
module.exports = (options) => ( | ||
/** | ||
* | ||
* @param {import("express").Request} req | ||
* @param {import("express").Response} res | ||
* @param {import("express").NextFunction} next | ||
*/ | ||
(req, res, next) => { | ||
const { xid, currentUser } = res.locals; | ||
if (!currentUser?.super) { | ||
logger.warn(`${MODULE_NAME} C3415A21: Forbid non super user`, { | ||
xid, | ||
currentUser, | ||
ip: req.ip, | ||
url: req.originalUrl, | ||
}); | ||
|
||
if (options?.redirectUrl) { | ||
res.redirect(options.redirectUrl); | ||
} else { | ||
res.status(403).json({ | ||
status: 403, | ||
message: 'Forbidden access', | ||
'trace-id': xid, | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
next(); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// const MODULE_NAME = 'WEBSERVER.ROUTERS.DASHBOARD.ROUTERS.USERS'; | ||
|
||
const express = require('express'); | ||
const forbidIfNotSuper = require('../../forbid-if-not-super-user'); | ||
const getUserList = require('../../../../../get-user-list'); | ||
|
||
const router = express.Router(); | ||
module.exports = router; | ||
|
||
/** | ||
* | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
* @param {import('express').NextFunction} next | ||
*/ | ||
const init = (req, res, next) => { | ||
res.locals.baseUrl = req.baseUrl; | ||
next(); | ||
}; | ||
|
||
/** | ||
* | ||
* @param {import('express').Request} req | ||
* @param {import('express').Response} res | ||
*/ | ||
const pageMain = async (req, res) => { | ||
const { xid } = res.locals; | ||
|
||
try { | ||
const users = await getUserList(xid, true, true); | ||
res.render('users.index.html.njk', { | ||
pageTitle: 'User List', | ||
users, | ||
}); | ||
} catch (e) { | ||
// | ||
} | ||
}; | ||
|
||
router.use(init); | ||
router.get('/', forbidIfNotSuper({ redirectUrl: '/dashboard/users/view' }), pageMain); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{% extends template %} | ||
|
||
{% block content %} | ||
|
||
<ul> | ||
{% for user in users %} | ||
<li> | ||
<a href="{{ baseUrl }}/views/{{ user.email }}"> | ||
<strong>{{ user.email }}</strong> | ||
</a> | ||
|
||
| ||
<span class="badge text-bg-{% if user.disabled %}danger{% else %}success{% endif %}"> | ||
{% if user.disabled %}DISABLED{% else %}ENABLED{% endif %} | ||
</span> | ||
|
||
<ul> | ||
<li>member since {{ user.created | simpleDateTime }}</li> | ||
|
||
{% if user.super %} | ||
<li>is a super user</li> | ||
{% endif %} | ||
|
||
<li>has {{ user.target_count }} links</li> | ||
</ul> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
|
||
{% endblock %} |