gpt4 book ai didi

angularjs - Strongloop环回如何给新用户分配静态 Angular 色

转载 作者:行者123 更新时间:2023-12-02 07:24:38 26 4
gpt4 key购买 nike

我克隆了一个 https://github.com/beeman/loopback-angular-admin我已经使用环回资源管理器创建了几个新 Angular 色,但是如何为我创建的用户分配 Angular 色

我有一个用户模型,它从环回中的用户模型扩展而来模型文件是这样的 -

{
"name": "user",
"plural": "users",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"accessTokens": {
"type": "hasMany",
"model": "accessToken",
"foreignKey": "userId"
},
"identities": {
"type": "hasMany",
"model": "userIdentity",
"foreignKey": "userId"
},
"credentials": {
"type": "hasMany",
"model": "userCredential",
"foreignKey": "userId"
},
"roles": {
"type": "hasMany",
"model": "Role",
"foreignKey": "principalId",
"through": "RoleMapping"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$owner",
"permission": "ALLOW"
}
],
"methods": {}
}

我的 user.js 就像 -

module.exports = function (user) {

// Set the username to the users email address by default.
user.observe('before save', function setDefaultUsername(ctx, next) {
if (ctx.instance) {
if(ctx.isNewInstance) {
ctx.instance.username = ctx.instance.email;
}
ctx.instance.status = 'created';
ctx.instance.created = Date.now();
}
next();
});

};

现在,我想根据我从客户端传递的属性 ctx.instance.type 为用户分配 Angular 色和委托(delegate)人

最佳答案

假设您已经在 Angular 色表中创建了一组有限的 Angular 色,请使用保存后 Hook 为刚刚创建的用户分配特定 Angular 色:

User.observe('after save', function setRoleMapping(ctx, next) {
if (ctx.instance) {
if(ctx.isNewInstance) {

var RoleMapping = User.app.models.RoleMapping;
// var roleId = based on type lookup or static?

RoleMapping.create({
principalType: "USER",
principalId: ctx.instance.id,
roleId: roleId
}, function(err, roleMapping) {
if (err) {return console.log(err);}

// success stuff

}):

}
}
next();
});

代码未经测试,只是一个大概的想法。您不能使用 before save Hook ,因为您不知道用于 RoleMapping 表中 principalId 的用户 ID。

UPDATE:版本包括按传入的类型查找 Angular 色:

user.observe('after save', function setRoleMapping(ctx, next) {
if (ctx.instance) {
if(ctx.isNewInstance) {

// look up role based on type
//
Role.find({where: {name: ctx.instance.type}}, function(err, role) {
if (err) {return console.log(err);}

RoleMapping.create({
principalType: "USER",
principalId: ctx.instance.id,
roleId: role.id
}, function(err, roleMapping) {

if (err) {return console.log(err);}

console.log('User assigned RoleID ' + role.id + ' (' + ctx.instance.type + ')');

}):

});

}
}
next();
});

查询文档在这里:https://docs.strongloop.com/display/public/LB/Querying+data

关于angularjs - Strongloop环回如何给新用户分配静态 Angular 色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34558200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com