也許這可以幫助。我在我需要每個記錄(在session.user.company.id中找到)登錄用戶的companyId相同。所以這是我的解決方案。這是一種策略,用於檢查模型是否有任何關聯,然後通過在req.body對象中添加companyId的關聯進行循環。
module.exports = function(req, res, next) {
req.options.where = _.assign({companyId : req.session.user.company.id}, req.options.where);
var attachCompanyIdToBody = function(){
if(_.isArray(req.body)){
} else if (_.isObject(req.body)){
req.body.companyId = req.session.user.company.id;
attachCompanyIdToAssociations();
}
};
var attachCompanyIdToAssociations = function(){
// Get assocations with current model
var Model = actionUtil.parseModel(req);
if(Model.associations){
_.forEach(Model.associations, function(item,index,object){
// if has company attached, remove company
if(req.body[item.alias] && item.alias.toLowerCase() !== 'company'){
if(_.isArray(req.body[item.alias])){
// ToDo if assocation is a collection then loop through collection adding companyId
} else if(_.isObject(req.body[item.alias])){
// if assocation is object then add companyId to object
req.body[item.alias].companyId = req.session.user.company.id;
}
}
});
}
};
if(req.body){
attachCompanyIdToBody();
}
return next();
};
您是如何實施多租戶的? –
related_account字段始終包含租戶的帳戶。 – toovy