Adding permission mangement to admin panel
This commit is contained in:
parent
774df52736
commit
557d7e186e
@ -5,7 +5,7 @@ name = OpenAuthService
|
|||||||
port = 3000
|
port = 3000
|
||||||
|
|
||||||
[mail]
|
[mail]
|
||||||
server = mail.stamm.me
|
server = mail.example.com
|
||||||
username = test
|
username = test
|
||||||
password = test
|
password = test
|
||||||
port = 595
|
port = 595
|
10
package-lock.json
generated
10
package-lock.json
generated
@ -18,9 +18,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"@hibas123/safe_mongo": {
|
"@hibas123/safe_mongo": {
|
||||||
"version": "1.3.3",
|
"version": "1.3.4",
|
||||||
"resolved": "https://registry.npmjs.org/@hibas123/safe_mongo/-/safe_mongo-1.3.3.tgz",
|
"resolved": "https://registry.npmjs.org/@hibas123/safe_mongo/-/safe_mongo-1.3.4.tgz",
|
||||||
"integrity": "sha512-ekEusgpuFqKBB2rt0FH0m7u4YEq2kRpUXUM7PrJNV9onLxSRmUtuJErA59PVhMWvZPVXnSn/Z69aebScxdH+BQ==",
|
"integrity": "sha512-JX1L5XWamAcmxZ8RB+yMEurKO84rGFm06yRS3php0g7LJUbBZJlWB13yJAlNx254z9zLUfXisRaAJz+EA3E+qw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@hibas123/nodelogging": "^1.3.18",
|
"@hibas123/nodelogging": "^1.3.18",
|
||||||
"mongodb": "^3.1.8"
|
"mongodb": "^3.1.8"
|
||||||
@ -254,7 +254,8 @@
|
|||||||
"@types/node": {
|
"@types/node": {
|
||||||
"version": "10.12.2",
|
"version": "10.12.2",
|
||||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.2.tgz",
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.2.tgz",
|
||||||
"integrity": "sha512-53ElVDSnZeFUUFIYzI8WLQ25IhWzb6vbddNp8UHlXQyU0ET2RhV5zg0NfubzU7iNMh5bBXb0htCzfvrSVNgzaQ=="
|
"integrity": "sha512-53ElVDSnZeFUUFIYzI8WLQ25IhWzb6vbddNp8UHlXQyU0ET2RhV5zg0NfubzU7iNMh5bBXb0htCzfvrSVNgzaQ==",
|
||||||
|
"dev": true
|
||||||
},
|
},
|
||||||
"@types/node-rsa": {
|
"@types/node-rsa": {
|
||||||
"version": "0.4.3",
|
"version": "0.4.3",
|
||||||
@ -293,6 +294,7 @@
|
|||||||
"version": "3.4.4",
|
"version": "3.4.4",
|
||||||
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-3.4.4.tgz",
|
"resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-3.4.4.tgz",
|
||||||
"integrity": "sha512-tPIgT0GUmdJQNSHxp0X2jnpQfBSTfGxUMc/2CXBU2mnyTFVYVa2ojpoQ74w0U2yn2vw3jnC640+77lkFFpdVDw==",
|
"integrity": "sha512-tPIgT0GUmdJQNSHxp0X2jnpQfBSTfGxUMc/2CXBU2mnyTFVYVa2ojpoQ74w0U2yn2vw3jnC640+77lkFFpdVDw==",
|
||||||
|
"dev": true,
|
||||||
"requires": {
|
"requires": {
|
||||||
"@types/node": "*"
|
"@types/node": "*"
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@hibas123/nodelogging": "^1.3.21",
|
"@hibas123/nodelogging": "^1.3.21",
|
||||||
"@hibas123/nodeloggingserver_client": "^1.1.2",
|
"@hibas123/nodeloggingserver_client": "^1.1.2",
|
||||||
"@hibas123/safe_mongo": "^1.3.3",
|
"@hibas123/safe_mongo": "^1.3.4",
|
||||||
"body-parser": "^1.18.3",
|
"body-parser": "^1.18.3",
|
||||||
"compression": "^1.7.3",
|
"compression": "^1.7.3",
|
||||||
"cookie-parser": "^1.4.3",
|
"cookie-parser": "^1.4.3",
|
||||||
|
@ -5,6 +5,7 @@ import promiseMiddleware from "../../helper/promiseMiddleware";
|
|||||||
import Permission from "../../models/permissions";
|
import Permission from "../../models/permissions";
|
||||||
import verify, { Types } from "../middlewares/verify";
|
import verify, { Types } from "../middlewares/verify";
|
||||||
import Client from "../../models/client";
|
import Client from "../../models/client";
|
||||||
|
import { ObjectID } from "bson";
|
||||||
|
|
||||||
const PermissionRoute: Router = Router();
|
const PermissionRoute: Router = Router();
|
||||||
PermissionRoute.use(GetUserMiddleware(true, true), (req: Request, res, next) => {
|
PermissionRoute.use(GetUserMiddleware(true, true), (req: Request, res, next) => {
|
||||||
@ -15,12 +16,16 @@ PermissionRoute.use(GetUserMiddleware(true, true), (req: Request, res, next) =>
|
|||||||
|
|
||||||
PermissionRoute.route("/")
|
PermissionRoute.route("/")
|
||||||
.get(promiseMiddleware(async (req, res) => {
|
.get(promiseMiddleware(async (req, res) => {
|
||||||
let permission = await Permission.find({});
|
let query = {};
|
||||||
|
if (req.query.client) {
|
||||||
|
query = { client: new ObjectID(req.query.client) }
|
||||||
|
}
|
||||||
|
let permission = await Permission.find(query);
|
||||||
res.json(permission);
|
res.json(permission);
|
||||||
}))
|
}))
|
||||||
.post(verify({
|
.post(verify({
|
||||||
clientId: {
|
client: {
|
||||||
type: Types.NUMBER
|
type: Types.STRING
|
||||||
},
|
},
|
||||||
name: {
|
name: {
|
||||||
type: Types.STRING
|
type: Types.STRING
|
||||||
@ -29,7 +34,7 @@ PermissionRoute.route("/")
|
|||||||
type: Types.STRING
|
type: Types.STRING
|
||||||
}
|
}
|
||||||
}, true), promiseMiddleware(async (req, res) => {
|
}, true), promiseMiddleware(async (req, res) => {
|
||||||
let client = await Client.findById(req.body.clientId);
|
let client = await Client.findById(req.body.client);
|
||||||
if (!client) {
|
if (!client) {
|
||||||
throw new RequestError("Client not found", HttpStatusCode.BAD_REQUEST);
|
throw new RequestError("Client not found", HttpStatusCode.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
@ -40,6 +45,10 @@ PermissionRoute.route("/")
|
|||||||
});
|
});
|
||||||
await Permission.save(permission);
|
await Permission.save(permission);
|
||||||
res.json(permission);
|
res.json(permission);
|
||||||
}))
|
})).delete(promiseMiddleware(async (req, res) => {
|
||||||
|
let { id } = req.query;
|
||||||
|
await Permission.delete(id);
|
||||||
|
res.json({ success: true });
|
||||||
|
}));
|
||||||
|
|
||||||
export default PermissionRoute;
|
export default PermissionRoute;
|
@ -164,6 +164,7 @@
|
|||||||
<div></div>
|
<div></div>
|
||||||
</th>
|
</th>
|
||||||
<th scope="col" style="width: 2.5rem"></th>
|
<th scope="col" style="width: 2.5rem"></th>
|
||||||
|
<th scope="col" style="width: 2.5rem"></th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
@ -183,8 +184,11 @@
|
|||||||
<td>
|
<td>
|
||||||
<a href="\{{ website }}">\{{ website }}</a>
|
<a href="\{{ website }}">\{{ website }}</a>
|
||||||
</td>
|
</td>
|
||||||
|
<td style="padding: 0.25em">
|
||||||
<!-- ToDo: Make helper to resolve number to human readaby text-->
|
<button style="border: 0; background-color: rgba(0, 0, 0, 0); padding: 0; text-align: center;" onclick="permissionsClient('\{{_id}}')">
|
||||||
|
perm
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
<td style="padding: 0.25em">
|
<td style="padding: 0.25em">
|
||||||
<button style="border: 0; background-color: rgba(0, 0, 0, 0); padding: 0; text-align: center;" onclick="editClient('\{{_id}}')">
|
<button style="border: 0; background-color: rgba(0, 0, 0, 0); padding: 0; text-align: center;" onclick="editClient('\{{_id}}')">
|
||||||
<i class="material-icons" style="font-size: 2rem; display: inline">
|
<i class="material-icons" style="font-size: 2rem; display: inline">
|
||||||
@ -238,6 +242,53 @@
|
|||||||
</span>
|
</span>
|
||||||
</form>
|
</form>
|
||||||
</script>
|
</script>
|
||||||
|
<script id="template-permission-list" type="text/x-handlebars-template">
|
||||||
|
<h2><button class="btn btn-raised btn-primary" onclick="gotoClients()">back</button> to \{{client_name}} </h2>
|
||||||
|
<button class="btn btn-raised btn-primary" onclick="createPermission('\{{ client_id }}')">Create</button>
|
||||||
|
<table class="table table-bordered" style="margin-bottom: 0">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">ID</th>
|
||||||
|
<th scope="col">Name</th>
|
||||||
|
<th scope="col">Description</th>
|
||||||
|
<th scope="col" style="width: 2.5rem"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
\{{#permissions}}
|
||||||
|
<tr>
|
||||||
|
<td>\{{ _id }}</td>
|
||||||
|
<td>\{{ name }}</td>
|
||||||
|
<td>\{{ description }}</td>
|
||||||
|
<td style="padding: 0.25em">
|
||||||
|
<button style="border: 0; background-color: rgba(0, 0, 0, 0); padding: 0; text-align: center;" onclick="deletePermission('\{{_id}}')">
|
||||||
|
<i class="material-icons" style="font-size: 2rem; display: inline">
|
||||||
|
delete
|
||||||
|
</i>
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
\{{/permissions}}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</script>
|
||||||
|
<script id="template-permission-form" type="text/x-handlebars-template">
|
||||||
|
<form class="form" action="JavaScript:void(null)" onsubmit="createPermissionSubmit(this)" style="margin-bottom: 0">
|
||||||
|
<input type=hidden value="\{{client_id}}" name=client />
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="name_input" class="bmd-label-floating">Name</label>
|
||||||
|
<input type="text" class="form-control" id="name_input" name=name value="">
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for=name class="bmd-label-floating">Description</label>
|
||||||
|
<input type="text" class="form-control" id=name name=description value="">
|
||||||
|
</div>
|
||||||
|
<span class="form-group bmd-form-group">
|
||||||
|
<!-- needed to match padding for floating labels -->
|
||||||
|
<button type="submit" class="btn btn-raised btn-primary">Save</button>
|
||||||
|
</span>
|
||||||
|
</form>
|
||||||
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
@ -72,6 +72,49 @@ Handlebars.registerHelper("formatDate", function (datetime, format) {
|
|||||||
await loadList();
|
await loadList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function renderPermissions(client_id, client_name) {
|
||||||
|
const listt = Handlebars.compile(document.getElementById("template-permission-list").innerText);
|
||||||
|
const formt = Handlebars.compile(document.getElementById("template-permission-form").innerText);
|
||||||
|
setCustomCard();
|
||||||
|
async function loadList() {
|
||||||
|
try {
|
||||||
|
let data = await request("/api/admin/permission", "GET");
|
||||||
|
tableb.innerHTML = listt({
|
||||||
|
client_id: client_id,
|
||||||
|
client_name: client_name,
|
||||||
|
permissions: data
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
catchError(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
window.gotoClients = () => {
|
||||||
|
renderClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
window.deletePermission = (id) => {
|
||||||
|
request("/api/admin/permission?id=" + id, "DELETE").then(() => loadList()).catch(catchError)
|
||||||
|
}
|
||||||
|
|
||||||
|
window.createPermission = () => {
|
||||||
|
try {
|
||||||
|
setCustomCard(formt({ client_id: client_id }));
|
||||||
|
} catch (err) {
|
||||||
|
console.log("Err", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
window.createPermissionSubmit = (elm) => {
|
||||||
|
console.log(elm);
|
||||||
|
let data = getFormData(elm);
|
||||||
|
console.log(data);
|
||||||
|
request("/api/admin/permission", "POST", data).then(() => setCustomCard()).then(() => loadList()).catch(catchError)
|
||||||
|
}
|
||||||
|
await loadList()
|
||||||
|
}
|
||||||
|
|
||||||
async function renderClient() {
|
async function renderClient() {
|
||||||
console.log("Rendering Client")
|
console.log("Rendering Client")
|
||||||
setTitle("Client")
|
setTitle("Client")
|
||||||
@ -88,6 +131,10 @@ Handlebars.registerHelper("formatDate", function (datetime, format) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.permissionsClient = (id) => {
|
||||||
|
renderPermissions(id, clients.find(e => e._id === id).name);
|
||||||
|
}
|
||||||
|
|
||||||
window.deleteClient = (id) => {
|
window.deleteClient = (id) => {
|
||||||
request("/api/admin/client?id=" + id, "DELETE").then(() => loadList()).catch(catchError)
|
request("/api/admin/client?id=" + id, "DELETE").then(() => loadList()).catch(catchError)
|
||||||
}
|
}
|
||||||
@ -96,8 +143,8 @@ Handlebars.registerHelper("formatDate", function (datetime, format) {
|
|||||||
console.log(elm);
|
console.log(elm);
|
||||||
let data = getFormData(elm);
|
let data = getFormData(elm);
|
||||||
console.log(data);
|
console.log(data);
|
||||||
let id = data.id;
|
let id = data._id;
|
||||||
delete data.id;
|
delete data._id;
|
||||||
if (id !== "") {
|
if (id !== "") {
|
||||||
request("/api/admin/client?id=" + id, "PUT", data).then(() => setCustomCard()).then(() => loadList()).catch(catchError)
|
request("/api/admin/client?id=" + id, "PUT", data).then(() => setCustomCard()).then(() => loadList()).catch(catchError)
|
||||||
} else {
|
} else {
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
<html>
|
|
||||||
|
|
||||||
|
|
||||||
<head>
|
|
||||||
<title>{{title}}</title>
|
|
||||||
<meta charset="utf8" />
|
|
||||||
<meta name="viewport" content="width=device-width,initial-scale=1" />
|
|
||||||
</head>
|
|
||||||
|
|
||||||
<body>
|
|
||||||
|
|
||||||
</body>
|
|
||||||
|
|
||||||
</html>
|
|
@ -1 +0,0 @@
|
|||||||
console.log("Hello World")
|
|
Loading…
Reference in New Issue
Block a user