我是Loopback的新手,我无法正确扩展用户群模型。虽然在资源管理器中它显示它已被扩展,但所有API都会产生401错误。恩。在正常的/用户呼叫中我得到..
{
"error": {
"name": "Error",
"status": 401,
"message": "Authorization Required",
"statusCode": 401,
"code": "AUTHORIZATION_REQUIRED",
"stack": "Error: Authorization Required"
}
}
我通过所有链接和问题,但他们都没有为我工作。我已经正确地将public设置为:在模型配置中为用户模型扩展模型和编写的acls等,但它们都不起作用。我还在git for strongloop上提出了一个问题:https://github.com/strongloop/loopback/issues/1809。任何线索都会很棒。感谢。
User.json如下:
{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "User"
},
"properties": {
"name": {
"type": "string",
"required": true
},
"email": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"phone": {
"type": "string"
}
},
"validations": [],
"relations": {
"question": {
"type": "hasMany",
"model": "question",
"foreignKey": ""
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": {}
}
答案 0 :(得分:2)
但有些注意事项值得考虑:
1)您正在定义电子邮件,密码,...属性,尽管它们在父用户模型中的定义方式完全相同;请参阅:https://github.com/strongloop/loopback/blob/master/common/models/user.json;
2)对于ACL,您缺少访问类型,它们不正确,但它们不会破坏任何内容...有关ACL的详细信息,请参阅:https://docs.strongloop.com/display/public/LB/Define+access+controls
3)同样,当您登录时,请确保使用您已创建的用户(POST
请求)并且它已在数据库中。
谢谢!
答案 1 :(得分:0)
好像您还没有登录该应用程序。 无论如何,默认情况下,用户父类中的设置无法访问大多数功能。 (这完全是一个关闭)
运行登录部分中的代码
{ "用户名":" ABC&#34 ;, "密码":" XYZ" }
此操作将返回令牌ID。
创建继承用户的模型
:~/nodejs/lab/user-api$ slc loopback:model
? Enter the model name: customer
? Select the data-source to attach customer to: db (memory)
? Select model's base class: User
? Expose customer via the REST API? Yes
? Custom plural form (used to build REST URL): customers
Let's add some customer properties now.
Enter an empty property name when done.
? Property name: phone
invoke loopback:property
? Property type: string
? Required? No
Let's add another customer property.
Enter an empty property name when done.
? Property name:
授予ACL访问权限:
slc loopback:acl
? Select the model to apply the ACL entry to: customer
? Select the ACL scope: All methods and properties
? Select the access type: All (match all types)
? Select the role: All users
? Select the permission to apply: Explicitly grant access
再次授予ACL访问权限:
slc loopback:acl
? Select the model to apply the ACL entry to: customer
? Select the ACL scope: All methods and properties
? Select the access type: All (match all types)
? Select the role: All users
? Select the permission to apply: Explicitly grant access
当我们两次授予访问权限时,它优先于基类中的DENY。你下次会得到一个结果。
包含ACL的示例类。您可以在环回项目中尝试它,它将工作:)
{
"name": "customer",
"plural": "customers",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"phone": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "ALLOW"
}
],
"methods": []
}
如果有效,请接受答复。它会。干杯!