【问题标题】:Structure role-management in meteor-app with alanning:roles使用 alanning:roles 构建流星应用程序中的角色管理
【发布时间】:2015-12-13 01:04:41
【问题描述】:

我需要一些建议来在我的流星应用程序中构建正确的角色架构和管理。

结构

  • 我正在使用 alanning:roles@1.2.13 为应用添加角色管理功能。
  • 有四种不同的用户类型:管理员、编辑、专家和用户。
  • 此外,还有几个模块具有不同的内容,即汽车、数学和图像。每个模块都组织在一个自己的流星包中。
  • 在每个模块中都有几个类别,可以由编辑器动态添加。

模块中的类别

模块结构如下:

elementSchema = new SimpleSchema({ 
    element:    {type: String, optional: true}
});

Cars.attachSchema(new SimpleSchema({
    title:      { type: String },
    content:    { type: String },
    category:   { type: [elementSchema], optional: true },
});

如您所见,所有可用的类别都在模块的 Collection 中。

权利

  • 管理员:完整权限
  • Editor:可以编辑所选模块中的元素(即 editor_1 可以编辑 Cars 和 Images 中的元素,但不能编辑 Maths 中的元素)
  • 专家:可以获得完整模块的权限或仅对模块的某些类别(即)专家_1 可以编辑图像,但只能编辑汽车中“本田”和“梅赛德斯”类别中的元素;不编辑数学)
  • 用户:没有编辑

这就是我在技术上进行身份验证的方式:

router.js

var filters = {
    authenticate: function () {
        var user;
        if (Meteor.loggingIn()) {
            this.layout('login');
            this.render('loading');
        } else {
            user = Meteor.user();
            if (!user) {
                this.layout('login');
                this.render('signin');
                return;
            }
            this.layout('Standard');
            this.next();
        }
    }
}
Router.route('/car/:_id', {
    name: 'car',
    before: filters.authenticate,
    data: function () {
        return { 
            cars: Cars.findOne({ _id: this.params._id }) 
        };
    }
});

模板

<template name="car">
    {{#if isInRole 'cars'}}
        Some form for editing
    {{else}}
        <h1>Restricted area</h1>
    {{/if}}
</template>

我把这个 router.js 放到每个包中。唯一的变化是使用每个包(汽车、数学、图像)的集合的数据函数。

更新:正如“Eliezer Steinbock”所说,有必要限制对 mongoDB 本身的访问。但到目前为止,我只在路线上这样做。

permissions.js

Cars.allow({
    insert: function(userId) {
        var loggedInUser = Meteor.user()
        if (loggedInUser && Roles.userIsInRole(loggedInUser, ['admin','editor'])) return true;
    },
    update: function(userId) {
        var loggedInUser = Meteor.user()
        if (loggedInUser && Roles.userIsInRole(loggedInUser, ['admin','editor'])) return true;
    }
});

我的问题

1) 我的第一个问题是如何使用角色和组。使用组的最佳方式是什么?第二个问题是,模块中没有固定的类别。现在我不知道有用的角色/组架构。

2) 我如何检查角色?因为有不同的角色可以获得访问权限:管理员、编辑和专家。我也遇到了这些专家的问题,他们只能访问该模块的定义类别。

3) 让permission.js 更通用不是更好吗?我的意思是,是否可以创建一个动态函数,所以我不必在任何地方都放置相同的代码?如何以有用的方式实现 permission.js 中的角色?

【问题讨论】:

  • 角色最重要的地方是编辑集合(插入/更新/删除)和订阅数据。限制对特定路线的访问不会阻止用户获取数据。使用角色在适当的地方限制访问(即 Meteor 方法、允许/拒绝和发布)
  • 感谢您的评论。我添加了到目前为止我写的部分。但这远非完美。
  • 您的第一个和第三个问题不是 Stack Overflow 上的主题,因为它们是基于意见的或过于宽泛。您的第二个问题缺乏精确的 MCVE。

标签: javascript authentication meteor roles simple-schema


【解决方案1】:

如果权限的逻辑相同,您可以在 permissions.js 中定义一次

App = App || {}; // We are using Namespaces, so you don't have to.. but it's good
App.Permissions = {
    insert: function(userId) {
        var loggedInUser = Meteor.user()
        if (loggedInUser && Roles.userIsInRole(loggedInUser, ['admin','editor'])) return true;
    },
    update: function(userId) {
        var loggedInUser = Meteor.user()
        if (loggedInUser && Roles.userIsInRole(loggedInUser, ['admin','editor'])) return true;
    }
}

然后您可以将它用于您的收藏:

Cars.allow(App.Permissions); // Or
Cars.allow(App.Permissions.getPermissionsForGroup('cars'))

在某处定义角色..

角色

// Give user the role "editor" in "cars" group
Roles.addUsersToRoles(someUserId, ['editor'], 'cars');
Roles.addUsersToRoles(someOtherId, ['admin'], 'cars');

你可以像这样在 permissions.js 中准备:

权限

App = App || {}; 
App.Permissions = {
 insert: function(userId) {...},
 update: function(userId) {...},
 getPermissionsForGroup: function(group) {
    return {
       insert: function(userId, doc) {
          // Only admin can insert
          return Roles.userIsInRole(userId, "admin", group);
       },
       update: function(userId, doc, fields, modifier) {
          // Editor & Admin can edit
          return Roles.userIsInRole(userId, ["editor","admin"], group);
       },
       remove: function(userId, doc) {
          // Only admin can remove
          return Roles.userIsInRole(userId, "admin", group);
       }
    }    
}

在此示例中,admins 可以插入和更新。editors 只能更新,但可以插入。

关于 alanning:roles 的文档,您可以像这样定义和使用角色:

// Super Admin definition..
Roles.addUsersToRoles(superAdminId, ['admin'], Roles.GLOBAL_GROUP);

Roles.addUsersToRoles(joesUserId, ['manage-team','schedule-game'], 'manchester-united.com')
Roles.addUsersToRoles(joesUserId, ['player','goalie'], 'real-madrid.com')

Roles.userIsInRole(joesUserId, 'manage-team', 'manchester-united.com')  // => true
Roles.userIsInRole(joesUserId, 'manage-team', 'real-madrid.com')  // => false

是的,请确保权限逻辑将包含在您的 Collection 定义之前.. 显然 :)

【讨论】:

  • 在你的例子中MyApp.Permision不应该是App.Permission吗?
  • 对于getSpecialPermission(group) {,我收到错误Unexpected token (。最后一件事:您是否有将getSpecialPermission 连接到现有类别(Cars.attachSchema)的想法?
  • @user3848987 刚刚修复了代码.. 您必须想象,这只是一个示例,您可以如何做到这一点.. 让您了解如何构建它,而您不必这样做重写每个允许对象并将其保存在一个地方,对吗?
  • @user3848987 您不需要将架构附加到权限,这太不同了..您的架构应该与您的集合定义一起在您的 /both/cars_collection.js 中..架构是架构,权限就是权限
  • 感谢您展示如何将所有内容放在一个地方。这很有帮助。我知道架构和权限是不同的。我的问题只是如何定义角色,这取决于类别。编辑器可以编辑所有汽车,但专家只能编辑某些类别的汽车。这是我必须做的最后一件棘手的事情。但类别不是固定的。它们可以由编辑器扩展。
猜你喜欢
  • 2011-02-19
  • 2013-04-18
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2017-11-19
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多