2016-09-16 2 views
0

は、私は以下のようにMongoDBとループバックのアプリを持っています。私は認証に必要なエラーを取得します。 私は料理の役割を誰にも許可するように変更した場合にのみ可能になります。ループバックエラー:認証が必要です

どのようにして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": {} 
} 

アプリ/サーバー/ブーツ/ 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()  

とノードでループバックを実行します。再び

関連する問題