2016-09-16 95 views
0

我有一个回环应用程序与mongoDB如下:环回错误:需要授权

当我登录为管理员我不能使用post方法的菜肴。并且我获得授权所需的错误。 只有当我改变菜肴的角色才能让每个人都变得可能。

我该如何实现想要的结果,让每个人都在DENY上,只允许某些用户进行某些操作? 谢谢。这里是我的代码..

应用/服务器/模型config.json:

{ 
    "_meta": { 
    "sources": [ 
     "loopback/common/models", 
     "loopback/server/models", 
     "../common/models", 
     "./models" 
    ], 
    "mixins": [ 
     "loopback/common/mixins", 
     "loopback/server/mixins", 
     "../node_modules/loopback-ds-timestamp-mixin", 
     "../common/mixins", 
     "./mixins" 
    ] 
    }, 
    "User": { 
    "dataSource": "db" 
    }, 
    "AccessToken": { 
    "dataSource": "db", 
    "public": false 
    }, 
    "ACL": { 
    "dataSource": "MongoDB", 
    "public": false 
    }, 
    "RoleMapping": { 
    "dataSource": "MongoDB", 
    "public": false 
    }, 
    "Role": { 
    "dataSource": "MongoDB", 
    "public": false 
    }, 
    "dishes": { 
    "dataSource": "MongoDB", 
    "public": true 
    }, 
    "Customer": { 
    "dataSource": "MongoDB", 
    "public": true 
    }, 
    "Comments": { 
    "dataSource": "MongoDB", 
    "public": true 
    } 
} 

应用程序/通用/ modles/dishes.json:

{ 
    "name": "dishes", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "name": { 
     "type": "string", 
     "required": true 
    }, 
    "description": { 
     "type": "string", 
     "required": true 
    }, 
    "category": { 
     "type": "string", 
     "required": true 
    }, 
    "image": { 
     "type": "string", 
     "required": true 
    }, 
    "label": { 
     "type": "string", 
     "required": true, 
     "default": "''" 
    }, 
    "price": { 
     "type": "string", 
     "required": true, 
     "default": "0" 
    } 
    }, 
    "mixins": { 
    "TimeStamp": true 
    }, 
    "validations": [], 
    "relations": { 
    "comments": { 
     "type": "hasMany", 
     "model": "Comments", 
     "foreignKey": "" 
    }, 
    "customers": { 
     "type": "hasMany", 
     "model": "Customer", 
     "foreignKey": "" 
    } 
    }, 
    "acls": [ 
    { 
     "accessType": "*", 
     "principalType": "ROLE", 
     "principalId": "$everyone", 
     "permission": "DENY" 
    }, 
    { 
     "accessType": "READ", 
     "principalType": "ROLE", 
     "principalId": "$authenticated", 
     "permission": "ALLOW" 
    }, 
    { 
     "accessType": "EXECUTE", 
     "principalType": "ROLE", 
     "principalId": "admin", 
     "permission": "ALLOW", 
     "property": "create" 
    }, 
    { 
     "accessType": "WRITE", 
     "principalType": "ROLE", 
     "principalId": "admin", 
     "permission": "ALLOW" 
    } 
    ], 
    "methods": {} 
} 

应用程序/通用/ modles/comments.json:

{ 
    "name": "Comments", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "Rating": { 
     "type": "number", 
     "required": true, 
     "default": 5 
    }, 
    "comment": { 
     "type": "string", 
     "required": true 
    } 
    }, 
    "mixins": { 
    "TimeStamp": true 
    }, 
    "validations": [], 
    "relations": { 
    "dishes": { 
     "type": "belongsTo", 
     "model": "dishes", 
     "foreignKey": "" 
    }, 
    "customer": { 
     "type": "belongsTo", 
     "model": "Customer", 
     "foreignKey": "customerId" 
    } 
    }, 
    "acls": [ 
    { 
     "accessType": "*", 
     "principalType": "ROLE", 
     "principalId": "$everyone", 
     "permission": "DENY" 
    }, 
    { 
     "accessType": "READ", 
     "principalType": "ROLE", 
     "principalId": "$authenticated", 
     "permission": "ALLOW" 
    }, 
    { 
     "accessType": "EXECUTE", 
     "principalType": "ROLE", 
     "principalId": "$authenticated", 
     "permission": "ALLOW", 
     "property": "create" 
    }, 
    { 
     "accessType": "WRITE", 
     "principalType": "ROLE", 
     "principalId": "$owner", 
     "permission": "ALLOW" 
    } 
    ], 
    "methods": {} 
} 

应用程序/公共/ modles/customer.json:

{ 
    "name": "Customer", 
    "base": "User", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": {}, 
    "validations": [], 
    "relations": { 
    "comments": { 
     "type": "hasMany", 
     "model": "Comments", 
     "foreignKey": "customerId" 
    } 
    }, 
    "acls": [ 
    { 
     "accessType": "*", 
     "principalType": "ROLE", 
     "principalId": "$everyone", 
     "permission": "DENY" 
    } 
    ], 
    "methods": {} 
} 

和应用/服务器的/ boot /的script.js:

module.exports = function(app) { 
var MongoDB = app.dataSources.MongoDB; 

MongoDB.automigrate('Customer', function(err) { 
    if (err) throw (err); 
    var Customer = app.models.Customer; 

    Customer.create([ 
    {username: 'Admin', email: '[email protected]', password: 'abcdef'}, 
    {username: 'muppala', email: '[email protected]', password: 'abcdef'} 
    ], function(err, users) { 
     if (err) throw (err); 
     var Role = app.models.Role; 
     var RoleMapping = app.models.RoleMapping; 

     Role.find({ name: 'admin' }, function(err, results) { 
      if (err) { throw err; } 

      if (results.length < 1) { 
       // now we know the DB doesn't have it already, so do the Role creation... 
       //create the admin role 
       Role.create({ 
        name: 'admin' 
       }, function(err, role) { 
        if (err) throw (err); 
        //make admin 
        role.principals.create({ 
        principalType: RoleMapping.USER, 
        principalId: users[0].id 
        }, function(err, principal) { 
        if (err) throw (err); 
        }); 
       }); 
      } 
     }); 
    }); 
}); 

}; 

回答

0

看到你last question我想象发生了什么。

不知何故集合Role已创建但未映射到User

我建议你改变:

Role.find({ name: 'admin' }, function(err, results) { 
      if (err) { throw err; } 

      if (results.length < 1) { 
       // now we know the DB doesn't have it already, so do the Role creation... 
       //create the admin role 
       Role.create({ 
        name: 'admin' 
       }, function(err, role) { 
        if (err) throw (err); 
        //make admin 
        role.principals.create({ 
        principalType: RoleMapping.USER, 
        principalId: users[0].id 
        }, function(err, principal) { 
        if (err) throw (err); 
        }); 
       }); 
      } 
     }); 

通过:

Role.create({ 
     name: 'admin' 
    }, function(err, role) { 
     if (err) throw (err); 
     //make admin 
     role.principals.create({ 
     principalType: RoleMapping.USER, 
     principalId: users[0].id 
     }, function(err, principal) { 
     if (err) throw (err); 
     }); 
    }); 

删除该角色集合: db.Role.drop() 并再次执行环回。

注:我在做同样的工作,并为我工作。

0

我也遇到同样的麻烦,因为我通过相同的任务。基克先生的答案适用于我的。 首先,从cmd键入

>mongo 
>use conFusion (Note: conFusion is the name of database of this assignment) 
>show collections (Note: to see all collections in database collections) 
>db.Role.drop()  

然后用节点运行环回。再次