From 79cb4d18f301b933ac71aa60437c1d26a66d81f6 Mon Sep 17 00:00:00 2001 From: "Shaurya Singhal(jugshaurya)" Date: Thu, 11 Jun 2020 11:00:17 +0530 Subject: [PATCH] Progress : User Enable issue #135 --- api/graphql/mutation/userEnable/index.js | 17 +++++++++++++ api/graphql/mutation/userEnable/type.graphql | 3 +++ api/services/AuthorizationPolicy/index.js | 11 +++++---- api/services/BaseModelService/Form.js | 25 ++++++++++++++++++++ api/services/User/Form.js | 5 ++++ api/services/User/index.js | 13 ++++++++++ 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 api/graphql/mutation/userEnable/index.js create mode 100644 api/graphql/mutation/userEnable/type.graphql diff --git a/api/graphql/mutation/userEnable/index.js b/api/graphql/mutation/userEnable/index.js new file mode 100644 index 0000000..651f4b8 --- /dev/null +++ b/api/graphql/mutation/userEnable/index.js @@ -0,0 +1,17 @@ +import UserForm from 'Services/User/Form'; +import BaseMutationResolver from 'Graphql/base/MutationResolver'; + +class UserEnable extends BaseMutationResolver { + resolve = () => { + const { viewer } = this.ctx; + const { id } = this.args; + const form = new UserForm({ + viewer, + id, + }); + + return form.restore(); + }; +} + +export default UserEnable.resolver(); diff --git a/api/graphql/mutation/userEnable/type.graphql b/api/graphql/mutation/userEnable/type.graphql new file mode 100644 index 0000000..9a28995 --- /dev/null +++ b/api/graphql/mutation/userEnable/type.graphql @@ -0,0 +1,3 @@ +extend type Mutation { + userEnable(id: ID!): User! +} diff --git a/api/services/AuthorizationPolicy/index.js b/api/services/AuthorizationPolicy/index.js index 559472e..0a6014a 100644 --- a/api/services/AuthorizationPolicy/index.js +++ b/api/services/AuthorizationPolicy/index.js @@ -32,7 +32,7 @@ policy.include('features', (p) => { }); policy.include('user', (p) => { - p.register('impersonate', ({ viewer }) => isAdmin(viewer)); + p.register(['impersonate', 'restore'], ({ viewer }) => isAdmin(viewer)); p.register('read', ({ viewer }) => isUser(viewer)); p.register( 'update', @@ -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', diff --git a/api/services/BaseModelService/Form.js b/api/services/BaseModelService/Form.js index 24ff6c6..f02c640 100644 --- a/api/services/BaseModelService/Form.js +++ b/api/services/BaseModelService/Form.js @@ -211,6 +211,17 @@ class ModelForm extends BaseModelService { ); } + async preRestore() { + if ( + !(await AuthPolicy.can(this.viewer) + .perform(`${this.name}:restore`) + .on(this.instance)) + ) + throw new ForbiddenError( + `User is not authorized to perform this action.`, + ); + } + @saveInstance async create() { await this.beforeAll?.call(this); @@ -250,6 +261,20 @@ class ModelForm extends BaseModelService { return this.instance; } + + @saveInstance + async restore() { + await this.beforeAll?.call(this); + await this.beforeRestore?.call(this); + await this.preRestore?.call(this); + const success = + (await this._handleErrors(() => this.onRestore?.call(this))) === null; + if (!success) + throw new UserInputError(`No ${this.text || this.name} found to restore`); + this.afterRestore?.call(this); + + return this.instance; + } } export default ModelForm; diff --git a/api/services/User/Form.js b/api/services/User/Form.js index 5581afd..1c9ce39 100644 --- a/api/services/User/Form.js +++ b/api/services/User/Form.js @@ -23,6 +23,11 @@ class UserForm extends BaseModelForm { onUpdate() { return new User(this.instance).update(this.body); } + + @requireInstance + onRestore() { + return new User(this.instance).restore(); + } } export default UserForm; diff --git a/api/services/User/index.js b/api/services/User/index.js index abd394f..878b965 100644 --- a/api/services/User/index.js +++ b/api/services/User/index.js @@ -48,6 +48,7 @@ export default class User extends BaseModelService { dateTimeRange, excludeEvents, }, + viewer, ]) )[0]; } @@ -143,4 +144,16 @@ export default class User extends BaseModelService { config.app.secret, ); } + + @saveInstance + @requireInstance + async restore() { + const count = await Models.User.restore({ + where: { id: this.instance.id }, + }); + + if (count) return null; + + return this.instance; + } }