Skip to content

Commit ceb3252

Browse files
BC-10975 - user admin-api for proper user Deletion (#337)
1 parent f8cbcdc commit ceb3252

File tree

5 files changed

+22
-46
lines changed

5 files changed

+22
-46
lines changed

controllers/accounts.js

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,8 @@ const getDeleteHandler = (service) => {
8888
return async function (req, res, next) {
8989
try {
9090
const { id } = req.params;
91-
const { userId } = await api(req, { useCallback: false, json: true, version: 'v3' })
91+
await api(req, { useCallback: false, json: true, version: 'v3' })
9292
.delete(`/${service}/${id}`);
93-
const user = await api(req)
94-
.get('/users/' + userId, { qs: { $populate: ['roles'] } });
95-
const roles = user.roles.map((role) => {
96-
return role.name;
97-
});
98-
const pathRole = getMostSignificantRole(roles);
99-
100-
if (pathRole === undefined) {
101-
const error = new Error('Deletion is supported only for users with role student, teacher or administrator.');
102-
error.status = 403;
103-
throw error;
104-
}
105-
106-
const data = await api(req)
107-
.delete(`/users/v2/admin/${pathRole}/${userId}`);
10893

10994
res.redirect(req.header('Referer'));
11095
} catch (err) {

controllers/users.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -257,35 +257,18 @@ const getMostSignificantRole = (roles) => {
257257
};
258258

259259
const getDeleteHandler = (service) => {
260-
let roles;
261-
return function (req, res, next) {
262-
api(req)
263-
.get('/users/' + req.params.id, { qs: { $populate: ['roles'] } })
264-
.then(async (user) => {
265-
roles = user.roles.map((role) => {
266-
return role.name;
267-
});
268-
return roles;
269-
})
270-
.then((roles) => {
271-
const pathRole = getMostSignificantRole(roles);
272-
if (pathRole === undefined) {
273-
const error = new Error('Deletion is supported only for users with role student, teacher or administrator.');
274-
error.status = 403;
275-
throw error;
276-
}
277-
api(req)
278-
.delete(`/users/v2/admin/${pathRole}/${req.params.id}`)
279-
.then((data) => {
280-
res.redirect(req.header('Referer'));
281-
})
282-
.catch((err) => {
283-
next(err);
284-
});
285-
})
286-
.catch((err) => {
287-
next(err);
260+
return async function (req, res, next) {
261+
try {
262+
const userId = req.params.id;
263+
264+
await api(req, { adminApi: true }).post(`/deletionRequests`, {
265+
json: { targetRef: { domain: 'user', id: userId }, deleteAfterMinutes: 0 },
288266
});
267+
268+
res.redirect(req.header('Referer'));
269+
} catch (err) {
270+
next(err);
271+
}
289272
};
290273
};
291274
// secure routes

views/accounts/accounts.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
{{/embed}}
2929
{{#embed "lib/components/modal-form" roles=roles method="delete" class="delete-modal"}}
3030
{{#content "fields"}}
31-
{{> "lib/components/delete-form-user"}}
31+
{{> "lib/components/delete-form-account"}}
3232
{{/content}}
3333
{{/embed}}
3434
{{#embed "lib/components/modal-form" class="reglink-modal"}}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<div class="form-group">
2+
<label class="control-label" for="name">Sind Sie sich sicher, dass Sie das folgende Element löschen wollen? <br /> <br />
3+
Dadurch wird nur der Account-Eintrag selbst gelöscht.<br />
4+
Wenn Sie den Benutzer und alle seine Daten löschen möchten, tun Sie dies über die Benutzerverwaltungsseite.</label>
5+
<input type="text" name="displayName" id="name" class="form-control" disabled>
6+
</div>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<div class="form-group">
2-
<label class="control-label" for="name">Sind Sie sich sicher, dass Sie das folgende Element löschen wollen?</label>
2+
<label class="control-label" for="name">Sind Sie sich sicher, dass Sie das folgende Element löschen wollen? <br />
3+
Dadurch werden <b>der Benutzer und alle seine Daten</b> dauerhaft gelöscht.
4+
</label>
35
<input type="text" name="displayName" id="name" class="form-control" disabled>
46
</div>

0 commit comments

Comments
 (0)