简体   繁体   English

如何在 CASL 中使用 ForcedSubject

[英]How to use ForcedSubject in CASL

I am following CASL's cookbook on implementing authorization for roles with predfined permissions ( see here ) and I am blocked when I try to check abilities for a user.我正在关注 CASL 的食谱,了解如何为具有预定义权限的角色实施授权( 请参阅此处),当我尝试检查用户的能力时,我被阻止了。

I do not understand how ForcedSubject is meant to be used.我不明白ForcedSubject是如何使用的。 I do not which to use classes or interfaces to define my subjects so I therefore rely on strings.我不使用类或接口来定义我的主题,因此我依赖字符串。 However, all my subjects do contain a id property and I want the ability to check permissions with conditionals on that ID.但是,我所有的主题都包含一个id属性,我希望能够使用该 ID 的条件检查权限。 Bellow is a simplified version of what I implemented. Bellow 是我实现的简化版本。

import { Ability, AbilityClass, ForcedSubject } from '@casl/ability';

type OrgActions =
  | 'manage'
  | 'invite-user';

type SubjectWithId = { id: string };
type MyAbilities =
  | ['manage', 'all']
  | [OrgActions, 'organization' | ForcedSubject<SubjectWithId>];

export type MyAbility = Ability<MyAbilities>;
export const MyAbility = Ability as AbilityClass<MyAbility>;


const builder = new AbilityBuilder(PlatformAbility)
builder.can('invite-user', 'organization', { id: 'abcd' })

const ability = builder.build()
ability.can('invite-user', 'organization') // No problem here
ability.can('invite-user', subject('organization', { id: 'abcd' }) // Typing Error!

Here is the typing error I get.这是我得到的打字错误。

Argument of type '["invite-user", { id: string; } & ForcedSubject<"organization">]' is not assignable to parameter of type '[action: "manage", subject: "all", field?: string | undefined] | [action: OrgActions, subject: "organization" | ForcedSubject<SubjectWithId>, field?: string | undefined]'.
  Type '["invite-user", { id: string; } & ForcedSubject<"organization">]' is not assignable to type '[action: OrgActions, subject: "organization" | ForcedSubject<SubjectWithId>, field?: string | undefined]'.
    Type at position 1 in source is not compatible with type at position 1 in target.
      Type '{ id: string; } & ForcedSubject<"organization">' is not assignable to type '"organization" | ForcedSubject<SubjectWithId>'.
        Type '{ id: string; } & ForcedSubject<"organization">' is not assignable to type 'ForcedSubject<SubjectWithId>'.
          Types of property '__caslSubjectType__' are incompatible.
            Type 'string' is not assignable to type 'SubjectWithId'.ts(2345)

I do not understand how the ForceSubject type is supposed to be used if not with the subject helper like I did.我不明白如果不像我那样与subject助手一起使用ForceSubject类型应该如何使用。

The error you encountered is due to the type you passed to ForcedSubject .您遇到的错误是由于您传递给ForcedSubject的类型所致。

You need to provide the type your ability expects and subject is supposed to force your object into.您需要提供您的能力期望的类型,并且subject应该强制您的 object 进入。 In your example, you want CASL to recognise plain objects being forced into an 'organization' subject, so you need to pass 'organization' to ForcedSubject .在您的示例中,您希望 CASL 识别被强制进入'organization'主题的普通对象,因此您需要将'organization'传递给ForcedSubject Thus, your type MyAbilities should look like this:因此,您的类型MyAbilities应该如下所示:

type MyAbilities =
  | ['manage', 'all']
  | [OrgActions, 'organization' | ForcedSubject<'organization'>]

Note that this allows you to have only some subject types be forceable, providing greater type-safety for any subject type that might use classes instead of plain objects.请注意,这允许您只强制某些主题类型,为可能使用类而不是普通对象的任何主题类型提供更高的类型安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM