Skip to content

Integrate multiple roles instead of having only 1 role at the same time #26

@paduvi

Description

@paduvi

I had implemented it in a project, just modify features/users/actions/user.js action named 'findWithRole' and a little bit in config/passport.js:

action.findWithRole = function (conditions) {
        return Promise.coroutine(function*() {
            let user = yield app.models.user.find(conditions);
            let permissions = {
                feature: {},
                plugin: {}
            };
            if (!user)
                return null;
            yield Promise.map(user.role_ids, function (role_id) {
                return app.models.role.findById(role_id).then(function (role) {
                    let role_permissions = JSON.parse(role.permissions);
                    if (role_permissions.feature) {
                        Object.keys(role_permissions.feature).forEach(function (key) {
                            permissions.feature[key] = _.union(permissions.feature[key], role_permissions.feature[key]);
                        });
                    }
                    if (role_permissions.plugin) {
                        Object.keys(role_permissions.plugin).forEach(function (key) {
                            permissions.plugin[key] = _.union(permissions.plugin[key], role_permissions.plugin[key]);
                        });
                    }
                });
            });
            user = user.toJSON();
            user.role = {
                permissions: JSON.stringify(permissions)
            };
            return user;
        })();
    };

But it will return req.user as a raw JSON object instead of Sequelize model as before. You guys can consider to refer it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions