简体   繁体   English

CASL-Vue Can 组件为授权和未授权用户隐藏组件

[英]CASL-Vue Can component hides components for both authorized and unauthorized user

I have a component that I'll like to show if user has permission but the Can component seem to hide the component regardless of user permission.我有一个组件,如果用户有权限,我想显示它,但 Can 组件似乎隐藏了该组件,而不管用户权限如何。

Following is my ability.js module以下是我的ability.js 模块

 import {Ability, AbilityBuilder} from "@casl/ability"; export default function defineAbilityFor(hasRole) { const { can, cannot, build } = new AbilityBuilder(Ability) if (hasRole.== undefined) { console,log(hasRole) can('manage', 'all') can('create', 'all') } else { can('read', 'all') cannot('create', 'all') } return build() }

I added castle as a my main.js as follows:我将城堡添加为我的 main.js,如下所示:

 const app = createApp(App) app.use(router) app.use(store) app.use(abilitiesPlugin, ability(), { useGlobalProperties: true }) app.component(Can.name, Can) app.mount('#app')

And the following is the component in which i need to do permission check.以下是我需要进行权限检查的组件。

 <template> <div class="d-flex flex-column px-4"> <div class="d-flex justify-content-end"> <Can I="create" an="Institution"> <button class="btn btn-sm btn-outline-success" @click="addInst = true">{{ action }}</button> </Can> </div> <ListInstitutions v-if="addInst === false"/> <Can I="create" an="Institution"> <AddInstitution v-if="addInst === true" @created="closeModal"/> </Can> </div> </template> <script> import AddInstitution from "@/components/institution/AddInstitution"; import ListInstitutions from "@/components/institution/ListInstitutions"; import { Can } from "@casl/vue"; export default { name: 'InstitutionPage', components: {AddInstitution, ListInstitutions, Can}, data() { return { addInst: false, action: this.addInst? 'Institutions': 'Add Institution' } }, methods: { closeModal() { this.addInst = false } } } </script>

Thank you so much @Sergii Stotskyi.非常感谢@Sergii Stotskyi。

I eventually defined ability in a definedAbility.js file as follows:我最终在definedAbility.js 文件中定义了能力,如下所示:

 import { AbilityBuilder, Ability } from "@casl/ability"; export default function defineAbilityFor(userRole) { const { can, cannot, build } = new AbilityBuilder(Ability); if (userRole === 'user') { can ('read', 'Institution') cannot(['create', 'update'], 'all') } if (userRole === 'reg_admin_stroke' || userRole === 'admin') { can('manage', 'all') } return build() }

And then in every component where ability is required, inside the setup() method I create the ability as follows:然后在需要能力的每个组件中,在 setup() 方法中,我按如下方式创建能力:

 ... import definedAbility from '@/services/definedAbility.js'... const ability = definedAbility(userRole.value) return { ability }

And In my template did something like:在我的模板中做了类似的事情:

 <div v-if="ability.can('create', 'Institution')"> <button class="btn btn-sm btn-outline-success" @click="addInst = true">{{ action }}</button> </div>

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

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