Skip to content

Commit 15c348f

Browse files
committed
Fix default inAudienceValidationFunction
The function had a statement that performed no value: _.flatten(userRoles, this.roleToAudienceMapFunction) resulted in the userRoles array being flattened, but it should have been mapping the userRoles with the roleToAudienceMapFunction. Fixed this behavior and also extended the behavior so the roleToAudienceMapFunction can return an array of values, which will then be flattened into the final userAudience list.
1 parent 60eeb6e commit 15c348f

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ app.config(['$navigationProvider', function ($navigationProvider) {
5555
return userRole;
5656
},
5757
inAudienceValidationFunction: function (userRoles, audiences) {
58-
var userAudiences = _.flatten(userRoles, this.roleToAudienceMapFunction);
58+
var userAudiences = _.flatten(_.map(userRoles, this.roleToAudienceMapFunction));
5959
return !_.isEmpty(userAudiences) && !_.isEmpty(audiences) &&
60-
(_.find(audiences, function (audience) { return _.includes(userAudiences, audience); }) !== undefined);
60+
_.some(audiences, function (audience) { return _.includes(userAudiences, audience); });
6161
}
6262
});
6363
}]);
@@ -73,6 +73,9 @@ All properties (own and inherited) of the extensions object will be available as
7373
```JAVASCRIPT
7474
// returns true if the user is in any of the specified audiences
7575
$navigation.inAudience('X', 'Y', 'Z');
76+
77+
// will flatten provided arrays that are one-level deep in the arguments list
78+
$navigation.inAudience('X', ['Y', 'Z'], ['A']) === $navigation.inAudience('X', 'Y', 'Z', 'A')
7679
```
7780

7881
### isActiveLocation

ng-navigation-service.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
return userRole;
2525
},
2626
inAudienceValidationFunction: function (userRoles, audiences) {
27-
var userAudiences = _.flatten(userRoles, this.roleToAudienceMapFunction);
27+
var userAudiences = _.flatten(_.map(userRoles, this.roleToAudienceMapFunction));
2828
return !_.isEmpty(userAudiences) && !_.isEmpty(audiences) &&
29-
(_.find(audiences, function (audience) { return _.includes(userAudiences, audience); }) !== undefined);
29+
_.some(audiences, function (audience) { return _.includes(userAudiences, audience); });
3030
}
3131
};
3232

@@ -40,7 +40,7 @@
4040
this.$get = ['$injector', '$location', '$log', '$window', function ($injector, $location, $log, $window) {
4141
var secService;
4242
var securityService = function () {
43-
if (secService === undefined) {
43+
if (_.isUndefined(secService)) {
4444
if (!_.isString(configuration.securityService)) {
4545
$log.error('No securityService configuration value provided');
4646
return undefined;
@@ -65,7 +65,7 @@
6565
* returns true if the user is in any of the specified audiences.
6666
*/
6767
inAudience: function () {
68-
var args = _.toArray(arguments);
68+
var args = _.flatten(_.toArray(arguments));
6969
var authenticated = securityService().isAuthenticated();
7070
// handle 'all' and 'anonymous' special cases
7171
if (args.length === 1 && _.isString(args[0])) {

tests.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('$navigation', function() {
1616
it('should be exposed as part of the service',
1717
inject(function ($navigation) {
1818
$navigation.links.key.should.equal('value');
19-
$navigation.linker.should.be.a.Function; // jshint ignore:line
19+
$navigation.linker.should.be.a.Function();
2020
})
2121
);
2222
});
@@ -43,8 +43,8 @@ describe('$navigation', function() {
4343
it('should have an expected default configuration',
4444
inject(function ($navigation) {
4545
var configuration = $navigation.getConfiguration();
46-
configuration.inAudienceValidationFunction.should.be.a.Function; // jshint ignore:line
47-
configuration.roleToAudienceMapFunction.should.be.a.Function; // jshint ignore:line
46+
configuration.inAudienceValidationFunction.should.be.a.Function();
47+
configuration.roleToAudienceMapFunction.should.be.a.Function();
4848
configuration.activeLinkDecorator.should.equal('active-decorator');
4949
configuration.inactiveLinkDecorator.should.equal('inactive-decorator');
5050
configuration.securityService.should.equal('$security');
@@ -55,7 +55,7 @@ describe('$navigation', function() {
5555
inject(function ($navigation) {
5656
var functions = ['decorateLink', 'getConfiguration', 'inAudience', 'isActiveLocation', 'tokenizePath'];
5757
for (var i in functions) {
58-
$navigation[functions[i]].should.be.a.Function; // jshint ignore:line
58+
$navigation[functions[i]].should.be.a.Function();
5959
}
6060
})
6161
);
@@ -235,6 +235,12 @@ describe('$navigation', function() {
235235
$navigation.inAudience('a', 'b').should.equal(true);
236236
})
237237
);
238+
239+
it('should handle an array as part of the argument list',
240+
inject(function ($security, $navigation) {
241+
$navigation.inAudience('b', ['a']).should.equal(true);
242+
})
243+
);
238244
});
239245
});
240246

0 commit comments

Comments
 (0)