Managing user roles in emberjs?

I have a nodejs api as below

route.post("/token",function(req,res){
    authLib
        .checkForm(req.body)
        .then(authLib.findUser)
        .then(authLib.isValidUser)
        .then(authLib.authenticate)
        .then(authLib.genToken)
        .then((token)=>{
            res
                .status(200)
                .json({'access_token': token});
        })
        .catch((err)=>{
            res
                .status(400)
                .json({'error': err.message});
        });
});

modelfor the user contains a field with the user role. Each user role has a different panel. I applied ember-simple-auth with oauth2-password-grantand the toolbar template is shown below

{{#if session.isAuthenticated}}
    {{#app-dashboard}}
    {{/app-dashboard}}
{{else}}
    {{#landing-app}}
    {{/landing-app}}
{{/if}}

The problem is how I can distinguish between user roles. One method might be to use ajax requests to retrieve the role, but that would mean an additional XHR request for all views. Another problem with using XHR Ember.$is that the authorization token is not tied to the request. What is the best way to solve this problem?

+4
source share
2

ember-simple-auth, , , , / "". Ember , role/permission , . . XHR.

, "" , , .

, . API Node, .

Ember , :

export default Ember.Service.extend({
    permissions: [], // Sample permissions: "seeAdminPanel", "deleteUsers"

    // You can create a computed property to check permissions (good for templates)
    canDeleteUsers: Ember.computed('permissions', function() {
        //Check that the permissions object contains the deleteUsers permission
        let permissions = this.get('permissions');
        let permissionToCheck = 'deleteUsers';
        let userHasPermission = permissions.indexOf(permissionToCheck) > -1; 
        return (userHasPermission);
    }),

    // Or create a generic function to check any permission (good for checking in a function)
    canCurrentUser(permissionToCheck) {
        let permissions = this.get('permissions');
        return (permissions.indexOf(permissionToCheck) > -1);
    }
});

Ember Node api, . , , ( ):

let userPermissionsService = this.get('userPermissionsService');
userPermissionsService.set('permissions', ["deleteUsers"]);

:

{{#if userPermissionsService.canDeleteUsers}}
    <button>Delete User</button>
{{/if}}

:

let userPermissionsService = this.get('userPermissionsService');
if (userPermissionsService.canCurrentUser("deleteUsers")) {
    this.deleteUser()
}

XHR, , jQuery ajax ( http://api.jquery.com/jQuery.ajax/), Ember, REST : https://guides.emberjs.com/v2.13.0/models/customizing-adapters/#toc_headers-customization

+2

Source: https://habr.com/ru/post/1677985/


All Articles