1043 timebox prepare zapier integration (#1967)
* Add create api-key route * Import module * Remove required mutation parameter * Fix Authentication * Generate random key * Update Read ApiKeyAbility handler * Add findMany apiKey route * Remove useless attribute * Use signed token for apiKeys * Authenticate with api keys * Fix typo * Add a test for apiKey module * Revoke token when api key does not exist * Handler expiresAt parameter * Fix user passport * Code review returns: Add API_TOKEN_SECRET * Code review returns: Rename variable * Code review returns: Update code style * Update apiKey schema * Update create token route * Update delete token route * Filter revoked api keys from listApiKeys * Rename endpoint * Set default expiry to 2 years * Code review returns: Update comment * Generate token after create apiKey * Code review returns: Update env variable * Code review returns: Move method to proper service --------- Co-authored-by: martmull <martmull@hotmail.com>
This commit is contained in:
@ -6,6 +6,7 @@ import {
|
||||
Activity,
|
||||
ActivityTarget,
|
||||
Attachment,
|
||||
ApiKey,
|
||||
Comment,
|
||||
Company,
|
||||
Favorite,
|
||||
@ -30,6 +31,7 @@ type SubjectsAbility = Subjects<{
|
||||
Activity: Activity;
|
||||
ActivityTarget: ActivityTarget;
|
||||
Attachment: Attachment;
|
||||
ApiKey: ApiKey;
|
||||
Comment: Comment;
|
||||
Company: Company;
|
||||
Favorite: Favorite;
|
||||
@ -55,7 +57,7 @@ export type AppAbility = PureAbility<
|
||||
|
||||
@Injectable()
|
||||
export class AbilityFactory {
|
||||
defineAbility(user: User, workspace: Workspace) {
|
||||
defineAbility(workspace: Workspace, user?: User) {
|
||||
const { can, cannot, build } = new AbilityBuilder<AppAbility>(
|
||||
createPrismaAbility,
|
||||
);
|
||||
@ -66,8 +68,18 @@ export class AbilityFactory {
|
||||
workspaceId: workspace.id,
|
||||
},
|
||||
});
|
||||
can(AbilityAction.Update, 'User', { id: user.id });
|
||||
can(AbilityAction.Delete, 'User', { id: user.id });
|
||||
if (user) {
|
||||
can(AbilityAction.Update, 'User', { id: user.id });
|
||||
can(AbilityAction.Delete, 'User', { id: user.id });
|
||||
} else {
|
||||
cannot(AbilityAction.Update, 'User');
|
||||
cannot(AbilityAction.Delete, 'User');
|
||||
}
|
||||
|
||||
// ApiKey
|
||||
can(AbilityAction.Read, 'ApiKey', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'ApiKey');
|
||||
can(AbilityAction.Update, 'ApiKey', { workspaceId: workspace.id });
|
||||
|
||||
// Workspace
|
||||
can(AbilityAction.Read, 'Workspace');
|
||||
@ -76,12 +88,19 @@ export class AbilityFactory {
|
||||
|
||||
// Workspace Member
|
||||
can(AbilityAction.Read, 'WorkspaceMember', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Delete, 'WorkspaceMember', { workspaceId: workspace.id });
|
||||
cannot(AbilityAction.Delete, 'WorkspaceMember', { userId: user.id });
|
||||
can(AbilityAction.Update, 'WorkspaceMember', {
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
if (user) {
|
||||
can(AbilityAction.Delete, 'WorkspaceMember', {
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
cannot(AbilityAction.Delete, 'WorkspaceMember', { userId: user.id });
|
||||
can(AbilityAction.Update, 'WorkspaceMember', {
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
});
|
||||
} else {
|
||||
cannot(AbilityAction.Delete, 'WorkspaceMember');
|
||||
cannot(AbilityAction.Update, 'WorkspaceMember');
|
||||
}
|
||||
|
||||
// Company
|
||||
can(AbilityAction.Read, 'Company', { workspaceId: workspace.id });
|
||||
@ -107,14 +126,19 @@ export class AbilityFactory {
|
||||
// Comment
|
||||
can(AbilityAction.Read, 'Comment', { workspaceId: workspace.id });
|
||||
can(AbilityAction.Create, 'Comment');
|
||||
can(AbilityAction.Update, 'Comment', {
|
||||
workspaceId: workspace.id,
|
||||
authorId: user.id,
|
||||
});
|
||||
can(AbilityAction.Delete, 'Comment', {
|
||||
workspaceId: workspace.id,
|
||||
authorId: user.id,
|
||||
});
|
||||
if (user) {
|
||||
can(AbilityAction.Update, 'Comment', {
|
||||
workspaceId: workspace.id,
|
||||
authorId: user.id,
|
||||
});
|
||||
can(AbilityAction.Delete, 'Comment', {
|
||||
workspaceId: workspace.id,
|
||||
authorId: user.id,
|
||||
});
|
||||
} else {
|
||||
cannot(AbilityAction.Update, 'Comment');
|
||||
cannot(AbilityAction.Delete, 'Comment');
|
||||
}
|
||||
|
||||
// ActivityTarget
|
||||
can(AbilityAction.Read, 'ActivityTarget');
|
||||
|
||||
Reference in New Issue
Block a user