stormpath / stormpath-sdk-angularjs

User Management for AngularJS (1.x) applications
http://docs.stormpath.com/angularjs/sdk/
Apache License 2.0
165 stars 58 forks source link

if-user-in-group directive expects groups.items to be returned from /me endpoint #178

Closed mraible closed 7 years ago

mraible commented 7 years ago

In the Java SDK, /me (with groups expansion) returns groups data as follows:

    "groups" : [ {
      "href" : "https://api.stormpath.com/v1/groups/2HWD2PtXm3WwAsHtthnF5M",
      "createdAt" : "2016-06-30T19:53:19.232Z",
      "modifiedAt" : "2016-10-11T04:48:59.951Z",
      "name" : "ADMIN",
      "description" : null,
      "status" : "ENABLED"
    } ]

When I use if-user-in-group="ADMIN", I see the following error in my console:

angular.js:13920 TypeError: Cannot read property 'filter' of undefined
    at User.inGroup (stormpath-sdk-angularjs.js:2895)
    at User.groupTest (stormpath-sdk-angularjs.js:2915)
    at evalElement (stormpath-sdk-angularjs.js:1016)
    at stormpath-sdk-angularjs.js:1029
    at Scope.$digest (angular.js:17524)
    at Scope.$apply (angular.js:17790)
    at done (angular.js:11831)
    at completeRequest (angular.js:12033)
    at XMLHttpRequest.requestLoaded (angular.js:11966)

If I look at line 2895 in stormpath-sdk-angularjs.js, I see the following function being called:

  /**
  * This method may change in the future, do not use.
  * Please use the `ifUserInGroup` directive instead
  */
  User.prototype.inGroup = function inGroup(groupName) {
    return this.groups.items.filter(function(group){
      return group.name === groupName;
    }).length >0;
  };

The logic still seems to work, and I am using the if-user-in-group directive, but there seems to be a bug here. If I debug, this.groups is defined, but this.groups.items is not.

robertjd commented 7 years ago

@mraible It looks like the JSDK is returning a list of groups, rather than a collection map (which has an items property, which is a list). I'm not seeing this explicitly defined in our framework spec, but in practice I'm pretty sure everyone is doing the later. Ping @bretterer and @nbarbettini

nbarbettini commented 7 years ago

Yes @robertjd, the .NET middleware returns a faux-collection that includes size and items property. Specifically so it will work with this Angular code. :)

mraible commented 7 years ago

@dogeared and @lhazlewood It looks like we need to change the Java SDK to return groups.items instead of just groups.

mraible commented 7 years ago

Created an issue for this in the Java SDK: https://github.com/stormpath/stormpath-sdk-java/issues/1044

mraible commented 7 years ago

Re-opening because the fix on the Java SDK will break semver. We'll be able to release a fix in 2.0, but in the meantime, I'll create a PR to workaround this issue in the AngularJS SDK.

mraible commented 7 years ago

Closing because fixed in Java SDK: https://github.com/stormpath/stormpath-sdk-java/issues/1044