Skip to content

Commit e5ea22b

Browse files
author
Tom Kirkpatrick
committed
Fix multiple lint issues
1 parent e7a9a4e commit e5ea22b

File tree

3 files changed

+44
-53
lines changed

3 files changed

+44
-53
lines changed

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ module.exports = function loopbackComponentAccess(app, options) {
3232
accessUtils.setupModels();
3333
}
3434

35-
// TODO: Create Group Access model automatically if one hasn't been specified
35+
// TODO: Create Group Access model automatically if one hasn't been specified
3636
};

lib/middleware/user-context.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module.exports = function userContextMiddleware() {
2323
loopbackContext.set('accessToken', req.accessToken.id);
2424
const app = req.app;
2525
const UserModel = app.accessUtils.options.userModel || 'User';
26-
const GroupAccessModel = app.accessUtils.options.groupAccessModel || 'GroupAccess';
2726

2827
return Promise.join(
2928
app.models[UserModel].findById(req.accessToken.userId),

lib/utils.js

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ module.exports = class AccessUtils {
111111
*/
112112
buildFilter(userId, Model) {
113113
const filter = { };
114-
const key = this.isGroupModel(Model)? Model.getIdName() : this.options.foreignKey;
114+
const key = this.isGroupModel(Model) ? Model.getIdName() : this.options.foreignKey;
115115
// TODO: Support key determination based on the belongsTo relationship.
116116

117117
return this.getUserGroups(userId)
@@ -177,7 +177,7 @@ module.exports = class AccessUtils {
177177
rel = modelClass.relations[rel];
178178
// debug('Checking relation %s to %s: %j', r, rel.modelTo.modelName, rel);
179179
if (rel.type === 'belongsTo' && this.isGroupModel(rel.modelTo)) {
180-
return models.push(modelName);
180+
models.push(modelName);
181181
}
182182
}
183183
});
@@ -230,6 +230,7 @@ module.exports = class AccessUtils {
230230
getCurrentUser() {
231231
const ctx = this.app.loopback.getCurrentContext();
232232
const currentUser = ctx && ctx.get('currentUser') || null;
233+
233234
return currentUser;
234235
}
235236

@@ -241,6 +242,7 @@ module.exports = class AccessUtils {
241242
getCurrentUserGroups() {
242243
const ctx = this.app.loopback.getCurrentContext();
243244
const currentUserGroups = ctx && ctx.get('currentUserGroups') || [];
245+
244246
return currentUserGroups;
245247
}
246248

@@ -274,6 +276,7 @@ module.exports = class AccessUtils {
274276
const Role = this.app.models[this.options.roleModel];
275277

276278
Role.registerResolver(accessGroup, (role, context, cb) => {
279+
cb = cb || createPromiseCallback();
277280
const modelClass = context.model;
278281
const modelId = context.modelId;
279282
const userId = context.getUserId();
@@ -287,9 +290,9 @@ module.exports = class AccessUtils {
287290
if (!userId) {
288291
process.nextTick(() => {
289292
debug('Deny access for anonymous user');
290-
if (cb) cb(null, false);
293+
cb(null, false);
291294
});
292-
return;
295+
return cb.promise;
293296
}
294297

295298
this.app.loopback.getCurrentContext().set('groupAccessApplied', true);
@@ -302,23 +305,23 @@ module.exports = class AccessUtils {
302305
if (!context || !context.model || !context.modelId) {
303306
process.nextTick(() => {
304307
debug('Deny access (context: %s, context.model: %s, context.modelId: %s)',
305-
!!context, !!context.model, !!context.modelId);
306-
if (cb) cb(null, false);
308+
Boolean(context), Boolean(context.model), Boolean(context.modelId));
309+
cb(null, false);
307310
});
308-
return;
311+
return cb.promise;
309312
}
310313

311-
return this.isGroupMemberWithRole(modelClass, modelId, userId, roleName)
312-
.then(res => {
313-
cb(null, res);
314-
})
314+
this.isGroupMemberWithRole(modelClass, modelId, userId, roleName)
315+
.then(res => cb(null, res))
315316
.catch(cb);
317+
318+
return cb.promise;
316319
}
317320

318321
/**
319322
* More complex application that also covers static methods.
320323
*/
321-
return Promise.join(this.getCurrentGroupId(context), this.getTargetGroupId(context),
324+
Promise.join(this.getCurrentGroupId(context), this.getTargetGroupId(context),
322325
(currentGroupId, targetGroupId) => {
323326
if (!currentGroupId) {
324327
// TODO: Use promise cancellation to abort the chain early.
@@ -329,10 +332,7 @@ module.exports = class AccessUtils {
329332
scope.currentGroupId = currentGroupId;
330333
scope.targetGroupId = targetGroupId;
331334
const actions = [ ];
332-
const conditions = {
333-
userId: userId,
334-
role: roleName
335-
};
335+
const conditions = { userId, role: roleName };
336336

337337
conditions[this.options.foreignKey] = currentGroupId;
338338
actions.push(GroupAccess.count(conditions));
@@ -375,9 +375,9 @@ module.exports = class AccessUtils {
375375
return cb(null, res);
376376
})
377377
.catch(cb);
378-
379-
});
380-
};
378+
return cb.promise;
379+
});
380+
}
381381

382382
/**
383383
* Check if a given user ID has a given role in the model instances group.
@@ -410,59 +410,51 @@ module.exports = class AccessUtils {
410410
modelClass.findById(modelId, (err, inst) => {
411411
if (err || !inst) {
412412
debug('Model not found for id %j', modelId);
413-
if (cb) cb(err, false);
414-
return;
413+
return cb(err, false);
415414
}
416415
debug('Model found: %j', inst);
417-
var groupId = inst[this.options.foreignKey];
416+
const groupId = inst[this.options.foreignKey];
417+
418418
// Ensure groupId exists and is not a function/relation
419-
if (groupId && 'function' !== typeof groupId) {
420-
if (cb) {
421-
return this.hasRoleInGroup(userId, roleId, groupId)
422-
.then(res => cb(null, res));
423-
}
424-
} else {
425-
// Try to follow belongsTo
426-
for (var r in modelClass.relations) {
427-
var rel = modelClass.relations[r];
428-
if (rel.type === 'belongsTo' && isGroupModel(rel.modelTo)) {
429-
debug('Checking relation %s to %s: %j', r, rel.modelTo.modelName, rel);
430-
inst[r](processRelatedGroup);
431-
return;
432-
}
433-
}
434-
debug('No matching belongsTo relation found for model %j and group: %j', modelId, groupId);
435-
if (cb) cb(null, false);
419+
if (groupId && typeof groupId !== 'function') {
420+
return this.hasRoleInGroup(userId, roleId, groupId)
421+
.then(res => cb(null, res));
436422
}
423+
// Try to follow belongsTo
424+
for (const relName in modelClass.relations) {
425+
const rel = modelClass.relations[relName];
437426

438-
function processRelatedGroup(err, group) {
439-
if (!err && group) {
440-
debug('Group found: %j', group.getId());
441-
if (cb) cb(null, this.hasRoleInGroup(userId, roleId, group.getId()));
442-
} else {
443-
if (cb) cb(err, false);
427+
if (rel.type === 'belongsTo' && this.isGroupModel(rel.modelTo)) {
428+
debug('Checking relation %s to %s: %j', relName, rel.modelTo.modelName, rel);
429+
return inst[relName](function processRelatedGroup(error, group) {
430+
if (!error && group) {
431+
debug('Group found: %j', group.getId());
432+
return cb(null, this.hasRoleInGroup(userId, roleId, group.getId()));
433+
}
434+
return cb(error, false);
435+
});
444436
}
445437
}
438+
debug('No matching belongsTo relation found for model %j and group: %j', modelId, groupId);
439+
return cb(null, false);
446440
});
447441
return cb.promise;
448-
};
442+
}
449443

450444
hasRoleInGroup(userId, role, group, cb) {
451445
debug('hasRoleInGroup: role: %o, group: %o, userId: %o', role, group, userId);
452446
cb = cb || createPromiseCallback();
453447
const GroupAccess = this.app.models[this.options.groupAccessModel];
454-
const conditions = {
455-
userId,
456-
role,
457-
}
448+
const conditions = { userId, role };
449+
458450
conditions[this.options.foreignKey] = group;
459451
GroupAccess.count(conditions)
460452
.then(count => {
461453
const res = count > 0;
462454

463455
debug(`user ${userId} has role ${role} in group ${group}: ${res}`);
464456
cb(null, res);
465-
})
457+
});
466458
return cb.promise;
467459
}
468460

0 commit comments

Comments
 (0)