vue-acl icon indicating copy to clipboard operation
vue-acl copied to clipboard

Dynamically modifying globalRules

Open konrad-ch opened this issue 4 years ago • 4 comments

Hello, I want to do something like this:

  1. make basic globalRules like:
globalRules: {
  editor: new AclRule('editor').generate(),
  accessUsersRoute: new AclRule('admin').generate()
}
  1. fetch permission data from my api
  2. modify/refresh accessUsersRoute rule to add "editor": accessUsersRoute = new AclRule('admin').or('editor').generate()

How can I do that? And if it's not possible then how can I fetch data from my api and build globalRules based on that BEFORE importing router and AclCreate and without making some ugly synchronous ajax call?

Thanks for any help!

konrad-ch avatar May 10 '20 15:05 konrad-ch

Hello @konrad-ch
To achieve this i am storing permission as array in local storage with userInfo. if someone change the permission then i updating $store's UserRole also updating my localstorage. so there is no need to call api on everytime & permission can be return in login api's response.

import Vue from 'vue'
import { AclInstaller, AclCreate, AclRule } from 'vue-acl'
import router from '@/router' 
Vue.use(AclInstaller) 

let initialRole = 'admin'
let permissionarray = null;
const userInfo = JSON.parse(localStorage.getItem('userInfo'))
if (userInfo && userInfo.userRole) initialRole = userInfo.userRole
if (userInfo && userInfo.permissions) permissionarray = userInfo.permissions
const rolePermissions = {}
//roles permission code
export default new AclCreate({
  initial  : initialRole,
  notfound : '/error-403',
  router,
  acceptLocalRules : true,
  globalRules: rolePermissions,
})

just a suggestion.

Thanks

dharmdeep avatar Jul 18 '20 07:07 dharmdeep

you can fetch the permission before vue mounted

ekoeryanto avatar Jul 20 '20 01:07 ekoeryanto

@ekoeryanto can you please share the code & more provide explanation because i am new to vue and i think i am doing wrong implementation with acl. so please explain with code how to implement acl , also api request, store commit, router things.

Thanks in advance !!

dharmdeep avatar Jul 22 '20 07:07 dharmdeep

Hello, can you show more of the code? Where the request is made.

Usually I do it in the middleware method of the API or in the created main.js

leonardovilarinho avatar Jul 22 '20 10:07 leonardovilarinho