Skip to content

Commit

Permalink
Progress : User Enable issue coding-blocks#135
Browse files Browse the repository at this point in the history
  • Loading branch information
jugshaurya committed Jun 13, 2020
1 parent 5ebcc30 commit 79cb4d1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
17 changes: 17 additions & 0 deletions api/graphql/mutation/userEnable/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 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();
3 changes: 3 additions & 0 deletions api/graphql/mutation/userEnable/type.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extend type Mutation {
userEnable(id: ID!): User!
}
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', 'restore'], ({ 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
25 changes: 25 additions & 0 deletions api/services/BaseModelService/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
5 changes: 5 additions & 0 deletions api/services/User/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 13 additions & 0 deletions api/services/User/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default class User extends BaseModelService {
dateTimeRange,
excludeEvents,
},
viewer,
])
)[0];
}
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 79cb4d1

Please sign in to comment.