Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Users] Add delete user mutation fixes(#134) #138

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions api/graphql/mutation/userDisable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import UserForm from 'Services/User/Form';
import BaseMutationResolver from 'Graphql/base/MutationResolver';

class UserDisable extends BaseMutationResolver {
resolve = () => {
const { viewer } = this.ctx;
const { id } = this.args;
const form = new UserForm({
viewer,
id,
});

return form.delete();
};
}

export default UserDisable.resolver();
3 changes: 3 additions & 0 deletions api/graphql/mutation/userDisable/type.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend type Mutation {
userDisable(id: ID!): User!
}
3 changes: 2 additions & 1 deletion api/routes/login/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class LoginCallback extends BaseController {
const profile = await auth.getProfile();

const user = new User();
await user.loadByUsername(profile.username);
await user.loadByUsername(profile.username, { paranoid: false });
if (user.deleted_at) return App.redirectToHome(res);
if (user.exists) {
await user.syncProfile(profile);
} else {
Expand Down
11 changes: 6 additions & 5 deletions api/services/AuthorizationPolicy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ policy.include('features', (p) => {
});

policy.include('user', (p) => {
p.register('impersonate', ({ viewer }) => isAdmin(viewer));
p.register(['impersonate', 'delete'], ({ viewer }) => isAdmin(viewer));
p.register('read', ({ viewer }) => isUser(viewer));
p.register(
'update',
Expand Down Expand Up @@ -126,10 +126,11 @@ policy.include('calendarEventInvite', (p) => {
({ viewer, entity: { event, status }, action }) =>
isOrganiser(event, viewer) ||
isAdmin(viewer) ||
isUser(viewer)
&& action === ':create'
&& event?.is_requestable
&& (status === 'Requested' || status === 'Accepted' && event?.auto_accept_requests)
(isUser(viewer) &&
action === ':create' &&
event?.is_requestable &&
(status === 'Requested' ||
(status === 'Accepted' && event?.auto_accept_requests))),
);
p.register(
'update',
Expand Down
13 changes: 13 additions & 0 deletions api/services/User/Form.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Models from 'Models';
import User from './';
import { isEmail, isPhoneNumber } from 'Utils/validators';
import BaseModelForm from 'Services/BaseModelService/Form';
import { saveInstance, requireInstance } from 'Services/BaseModelService';

class UserForm extends BaseModelForm {
name = 'user';
Expand All @@ -23,6 +25,17 @@ class UserForm extends BaseModelForm {
onUpdate() {
return new User(this.instance).update(this.body);
}

@saveInstance
@requireInstance
async beforeDelete() {
return Models.User.findByPk(this.id);
}

@requireInstance
onDelete() {
return new User(this.instance).delete();
}
}

export default UserForm;
16 changes: 14 additions & 2 deletions api/services/User/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import BaseModelService, {
import Availability from './Loaders/Availability';

export default class User extends BaseModelService {
static findByUsername(username) {
return Models.User.findOne({ where: { username } });
static findByUsername(username, options) {
return Models.User.findOne({ where: { username }, ...options });
}

static findById(id) {
Expand Down Expand Up @@ -143,4 +143,16 @@ export default class User extends BaseModelService {
config.app.secret,
);
}

@saveInstance
@requireInstance
async delete() {
const count = await Models.User.destroy({
where: { id: this.instance.id },
});

if (count) return null;

return this.instance;
}
}
5 changes: 5 additions & 0 deletions app/mutations/userDisable.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation UserDisable($id: ID!) {
disableUser: userDisable(id: $id) {
id
}
}