-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(auth): type ability templates
refs #247
- Loading branch information
1 parent
45656d7
commit 2cdfafd
Showing
5 changed files
with
33 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
import { Ability } from "@casl/ability"; | ||
import { Ability, RawRule } from "@casl/ability"; | ||
import type { TemplateExecutor } from "lodash"; | ||
import template from "lodash/template"; | ||
import { singleton } from "tsyringe"; | ||
|
||
type Role = "REGULAR_USER" | "REGULAR_ANONYMOUS_USER" | "SUPER_USER"; | ||
|
||
@singleton() | ||
export class AbilityService { | ||
private readonly createRegularUserRules = template( | ||
JSON.stringify([ | ||
readonly EMPTY_ABILITY = new Ability([]); | ||
|
||
private readonly RULES: Record<Role, RawRule[]> = { | ||
REGULAR_USER: [ | ||
{ action: ["create", "read", "sign"], subject: "UserWallet", conditions: { userId: "${user.id}" } }, | ||
{ action: "read", subject: "User", conditions: { id: "${user.id}" } } | ||
]) | ||
); | ||
private readonly createRegularAnonymousUserRules = template( | ||
JSON.stringify([ | ||
], | ||
REGULAR_ANONYMOUS_USER: [ | ||
{ action: ["create", "read", "sign"], subject: "UserWallet", conditions: { userId: "${user.id}" } }, | ||
{ action: "read", subject: "User", conditions: { id: "${user.id}", userId: null } } | ||
]) | ||
); | ||
|
||
getAbilityForUser(user: { userId: string }) { | ||
const rules = this.createRegularUserRules({ user }); | ||
return new Ability(JSON.parse(rules)); | ||
} | ||
{ action: "read", subject: "User", conditions: { id: "${user.id}" } } | ||
], | ||
SUPER_USER: [{ action: "manage", subject: "all" }] | ||
}; | ||
|
||
getAbilityForAnonymousUser(user: { id: string }) { | ||
const rules = this.createRegularAnonymousUserRules({ user }); | ||
return new Ability(JSON.parse(rules)); | ||
} | ||
private readonly templates = (Object.keys(this.RULES) as Role[]).reduce( | ||
(acc, role) => { | ||
acc[role] = template(JSON.stringify(this.RULES[role])); | ||
return acc; | ||
}, | ||
{} as Record<Role, TemplateExecutor> | ||
); | ||
|
||
getEmptyAbility() { | ||
return new Ability([]); | ||
getAbilityFor(role: Role, user: { userId: string }) { | ||
return this.toAbility(this.templates[role]({ user })); | ||
} | ||
|
||
getSuperUserAbility() { | ||
return new Ability([{ action: "manage", subject: "all" }]); | ||
private toAbility(raw: string) { | ||
return new Ability(JSON.parse(raw)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters