(inst)
| 307 | // ownership check induced the whole isOwner() to resolve as false. |
| 308 | // This behaviour will be pruned at next LoopBack major release. |
| 309 | function legacyOwnershipCheck(inst) { |
| 310 | const ownerId = inst.userId || inst.owner; |
| 311 | if (principalType === Principal.USER && ownerId && 'function' !== typeof ownerId) { |
| 312 | return callback(null, matches(ownerId, userId)); |
| 313 | } |
| 314 | |
| 315 | // Try to follow belongsTo |
| 316 | for (const r in modelClass.relations) { |
| 317 | const rel = modelClass.relations[r]; |
| 318 | // relation should be belongsTo and target a User based class |
| 319 | const belongsToUser = rel.type === 'belongsTo' && isUserClass(rel.modelTo); |
| 320 | if (!belongsToUser) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | // checking related user |
| 325 | const relatedUser = rel.modelTo; |
| 326 | const userModelName = relatedUser.modelName; |
| 327 | const isMultipleUsers = _isMultipleUsers(relatedUser); |
| 328 | // a relation can be considered for isOwner resolution if: |
| 329 | // 1. the app has a single user model and principalType is 'USER' |
| 330 | // 2. the app has multiple user models and principalType is the related user model name |
| 331 | if ((!isMultipleUsers && principalType === Principal.USER) || |
| 332 | (isMultipleUsers && principalType === userModelName)) { |
| 333 | debug('Checking relation %s to %s: %j', r, userModelName, rel); |
| 334 | inst[r](processRelatedUser); |
| 335 | return; |
| 336 | } |
| 337 | } |
| 338 | debug('No matching belongsTo relation found for model %j - user %j principalType %j', |
| 339 | modelId, userId, principalType); |
| 340 | callback(null, false); |
| 341 | |
| 342 | function processRelatedUser(err, user) { |
| 343 | if (err || !user) return callback(err, false); |
| 344 | |
| 345 | debug('User found: %j', user.id); |
| 346 | callback(null, matches(user.id, userId)); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | function checkOwnership(inst) { |
| 351 | const ownerRelations = inst.constructor.settings.ownerRelations; |
no test coverage detected
searching dependent graphs…