Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit d5edb1d

Browse files
added account deletion
1 parent 34e885f commit d5edb1d

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed

server/services/comments/comments.hooks.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const nullDeletedData = require('../../hooks/null-deleted-data');
1212
const hideDeletedData = require('../../hooks/hide-deleted-data');
1313
const createNotifications = require('./hooks/create-notifications');
1414
const createMentionNotifications = require('./hooks/create-mention-notifications');
15+
const isModerator = require('../../hooks/is-moderator-boolean');
1516
const _ = require('lodash');
1617
const xss = require('../../hooks/xss');
1718

@@ -58,8 +59,8 @@ module.exports = {
5859
],
5960
patch: [
6061
authenticate('jwt'),
61-
isVerified(),
6262
unless(isProvider('server'),
63+
isVerified(),
6364
unless((hook) => {
6465
// TODO: change that to a more sane method by going through the server with an constum service
6566
// only allow upvoteCount increment for non owners
@@ -79,8 +80,7 @@ module.exports = {
7980
],
8081
remove: [
8182
authenticate('jwt'),
82-
isVerified(),
83-
unless(isProvider('server'),
83+
unless(isModerator(),
8484
restrictToOwner()
8585
),
8686
softDelete()

server/services/contributions/contributions.hooks.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,7 @@ module.exports = {
137137
],
138138
remove: [
139139
authenticate('jwt'),
140-
isVerified(),
141140
unless(isModerator(),
142-
excludeDisabled(),
143141
restrictToOwner()
144142
)
145143
]

server/services/follows/follows.hooks.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ module.exports = {
1414
])
1515
],
1616
update: [
17-
authenticate('jwt')
17+
authenticate('jwt'),
18+
hooks.restrictToOwner()
1819
],
1920
patch: [
20-
authenticate('jwt')
21+
authenticate('jwt'),
22+
hooks.restrictToOwner()
2123
],
2224
remove: [
23-
authenticate('jwt')
25+
authenticate('jwt'),
26+
hooks.restrictToOwner()
2427
]
2528
},
2629

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
const errors = require('feathers-errors');
3+
4+
module.exports = () => {
5+
return async (hook) => {
6+
async function deleteData (service, query) {
7+
try {
8+
return await hook.app.service(service).remove(null, {query});
9+
} catch (err) {
10+
console.log('ERROR ON SERVICE', service);
11+
throw new errors.GeneralError(err.message);
12+
}
13+
}
14+
15+
if (hook.method !== 'remove') {
16+
hook.app.error('removeAllRelatedUserData hook works only on remove');
17+
return hook;
18+
}
19+
20+
const user = hook.params.user;
21+
if (!user || !user._id) {
22+
throw new errors.Forbidden('Forbidden');
23+
}
24+
25+
const query = hook.params.query;
26+
27+
let res;
28+
29+
if (query.deleteContributions === true) {
30+
await deleteData('contributions', {
31+
userId: user._id,
32+
type: 'post'
33+
});
34+
}
35+
if (query.deleteCandos === true) {
36+
await deleteData('contributions', {
37+
userId: user._id,
38+
type: 'cando'
39+
});
40+
}
41+
if (query.deleteComments === true) {
42+
await deleteData('comments', {
43+
userId: user._id
44+
});
45+
}
46+
// TODO: find a way to remove shouts without error
47+
// await deleteData('shouts', {
48+
// userId: user._id
49+
// });
50+
await deleteData('users-candos', {
51+
userId: user._id
52+
});
53+
await deleteData('notifications', {
54+
userId: user._id
55+
});
56+
await deleteData('usersettings', {
57+
userId: user._id
58+
});
59+
await deleteData('invites', {
60+
email: user.email
61+
});
62+
63+
// throw new errors.FeathersError('BOOOM');
64+
return hook;
65+
};
66+
};

server/services/users/users.hooks.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const thumbnails = require('../../hooks/thumbnails');
1111
const inviteCode = require('./hooks/invite-code')();
1212
const search = require('feathers-mongodb-fuzzy-search');
1313
const isOwnEntry = require('./hooks/is-own-entry');
14+
const removeAllRelatedUserData = require('./hooks/remove-all-related-user-data');
1415

1516
const { hashPassword } = require('feathers-authentication-local').hooks;
1617

@@ -145,7 +146,11 @@ module.exports = {
145146
),
146147
saveRemoteImages(['avatar', 'coverImg'])
147148
],
148-
remove: [ ...restrict, disableMultiItemChange() ]
149+
remove: [
150+
...restrict,
151+
disableMultiItemChange(),
152+
removeAllRelatedUserData()
153+
]
149154
},
150155

151156
after: {

0 commit comments

Comments
 (0)