Comments (6)
I have a workaround to use store instead of the user state inside the store as child:
// in app.js
Vue.use(VueKindergarten, {
child: (store) => store,
});
and
// in perimeters/BasePerimeter.js
export default class BasePerimeter extends Perimeter {
isAdmin() {
return allowed = !!this.child
&& this.child.getters['auth/isLoggedIn']
&& this.child.getters['auth/isAdmin'];
}
...
Inside my store's auth module, I encapsulates the logic to check admin access in the getter isAdmin. And it works!
I would be glad to hear any improvements or suggestions.
Thanks!
Zhu Ming
from vue-kindergarten.
Hey @blackpuppy,
I usually put the getter of a current user to separate file for example src/child.js
, because this way I can also use it wherever I want. The best way to assure that reactivity will work is to make child a function:
// src/child.js
import store from '@/store';
export default () => () => store.state.users.current;
import Vue from 'vue';
import VueKindergarten from 'vue-kindergarten';
import child from '@/child';
Vue.use(VueKindergarten, {
child,
});
And you can use it like this:
import { Perimeter } from 'vue-kindergarten';
export default class BasePerimeter extends Perimeter {
isLoggedIn() {
return !!this.child() || false;
}
isAdmin() {
return this.child().role === 'admin';
}
}
Please let me know if it works for you.
from vue-kindergarten.
@blackpuppy @wdews I have fixed the issue with child reactivity, so it will work even if child references a property in the store. You can upgrade π
from vue-kindergarten.
The fix works as expected.
I changed the child to return my user directly as below:
export default (store) => store ? store.state.auth.user : null;
Now I use Vuex store modules, and user
is in the module auth
's state.
And I changed my BasePerimeter as below:
import { Perimeter } from 'vue-kindergarten';
export default class BasePerimeter extends Perimeter {
isAdmin() {
return this.child && this.child.role &&
this.child.role.name === 'Administrator';
}
isAccountManager() {
return this.child && this.child.role &&
this.child.role.name === 'Account Manager';
}
}
Then the previous problem does not happen. From UI, I see that the menu items shown are always the ones for the user I log in as.
Thanks!
Zhu Ming
from vue-kindergarten.
What if I need to override the constructor in my BasePerimeter
class, what are the parameters I have to supply to super()
then, for everything to continue working as expected? For example, I use constructor() to generate class methods dynamically for isAdmin
, isWhatEver
, based on roles array.
from vue-kindergarten.
I think I got it to work, by looking at Perimeter.js
class, then passing in purpose, opts = {}
.
from vue-kindergarten.
Related Issues (20)
- Rule names collision HOT 6
- any upgrade instructions to 0.2.0 and 0.3.0? HOT 5
- SandBox HOT 10
- Redirecting before child resolve HOT 3
- Vue-kindergarten compatibility with vue-tables-2 HOT 3
- Importing Perimeters Error HOT 4
- Guarding Routes HOT 1
- Infinite update loop HOT 3
- Error in nuxt example? HOT 3
- this returns null inside can HOT 2
- Cannot read property βfromβ of undefined at new RouteGoverness HOT 1
- Is it possible to create generic perimeter HOT 2
- Is this project still maintained? HOT 4
- Passing variables to routePerimeterAction in nuxt HOT 1
- govern vs can / cannot HOT 1
- $isAllowed() method does not work in template for nuxt HOT 1
- `$isAllowed('action', object)` it performs twice in `created`, `mounted` hook.
- using routePerimeter in layouts nuxt.js
- Is there a way to create custom perimeter for custom roles
- Disable Perimeter Injection
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vue-kindergarten.