Code Monkey home page Code Monkey logo

Comments (6)

blackpuppy avatar blackpuppy commented on August 21, 2024

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.

JiriChara avatar JiriChara commented on August 21, 2024

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.

JiriChara avatar JiriChara commented on August 21, 2024

@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.

blackpuppy avatar blackpuppy commented on August 21, 2024

@JiriChara

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.

branquito avatar branquito commented on August 21, 2024

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.

branquito avatar branquito commented on August 21, 2024

I think I got it to work, by looking at Perimeter.js class, then passing in purpose, opts = {}.

from vue-kindergarten.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.