Only load layers from map services user has access to

3824
6
05-26-2014 12:41 PM
MS
by
New Contributor III
Hi

I have a set of map services that I load in my JSAPI app (v3.9), but different users have access to different map services through the app (using windows auth).  The app will throw an error (as expected) if the user doesn't have access to a particular map service when I try to add the layers to the map, but when there's an error loading any of the map services, the app won't fire the layers-add-result event.

My code essentially boils down to this:

this.map.on('layers-add-result', lang.hitch(this, 'doStuff'));

// Here I add some ArcGISDynamicMapServiceLayer layers here to the 'layers' object.

this.map.addLayers(this.layers);


What I'd like to be able to do is to only add layers to the map that the user has access to.  I think this would be similar to doing a check to see if a map service is up before trying to add a layer to the map from it, ie 'check if map service is available - if yes, add layer to map' kind of thing.

Is this possible to do?

Thanks.
0 Kudos
6 Replies
GauriDeshmukh
New Contributor III

What I'd like to be able to do is to only add layers to the map that the user has access to.  I think this would be similar to doing a check to see if a map service is up before trying to add a layer to the map from it, ie 'check if map service is available - if yes, add layer to map' kind of thing.

Is this possible to do?


  Yes its possible. You can set the permissions on server for different users.  or if you want to check for permission in your code only then you can do it by following way-
-if user is going to add the layer then show(populate) only that layers for which user has access,instead of throwing an error.
-depending upon login type(user login,admin login..)  populate the list of layers.
-For this you will have to apply filter for all layers in your layer list(in which you will have all the layers from server).
-You will filter this depending upon login type
- use this filtered list to populate the available layers  to user.
"but when there's an error loading any of the map services, the app won't fire the layers-add-result event."

-didnt get this.
0 Kudos
MS
by
New Contributor III
Yes its possible. You can set the permissions on server for different users.  or if you want to check for permission in your code only then you can do it by following way-
-if user is going to add the layer then show(populate) only that layers for which user has access,instead of throwing an error.
-depending upon login type(user login,admin login..)  populate the list of layers.
-For this you will have to apply filter for all layers in your layer list(in which you will have all the layers from server).
-You will filter this depending upon login type
- use this filtered list to populate the available layers  to user.

-didnt get this.


I think I understand what you're saying, as in to have code that decides what layers the user has access to and only request those layers.  But I don't want to have to put security code into my app before requesting layers - I want to have the security controlled only at the map service level, ie administered through ArcGIS Server Manager.  I would like the app to try to get all the layers, and ignore the ones that the user doesn't have access to.

As far as the event - if I use 'this.map.addLayers(this.layers)' while including a layer that the user doesn't have access to, the addLayers method throws a JavaScript error.  Since I have an event to run more code after the layers finish loading, that code will never run when this error occurs.
0 Kudos
JoanneMcGraw
Occasional Contributor III

I just wanted to comment that I am seeing the same behaviour that M S describes in re: the "layers-add-result" not firing when one of the services fails to load in the map. In my case, I am using an external service in my app and, if it happens to be down (a situation I have no control over), the "layers-add-result" event is not firing.

LauraMiles1
Occasional Contributor III

Hi, I'm wondering if you figured out how to catch this error before adding the layers to the map? I can see the dojo error in my console before the layers are actually added, so there must be some way to catch it. I have the same situation, where I have some secured layers that cause this error to fire, which causes "layers-add-result" not to fire - but only in Internet Explorer, and not consistently - sometimes "layers-add-result" will fire! It always works perfectly in Chrome.

I need to catch this error prior to adding the secured layers, so I can just skip adding them.

0 Kudos
JoanneMcGraw
Occasional Contributor III

It's been a while since I was looking at this myself and from what I remember at the time, I'm not sure that I came up with anything satisfactory. It seems to me that I did figure out a way to trap for this but, because of the lag time in getting the error, couldn't use it the way I wanted...which was to load a different layer on failure. Because the layers in question were acting as basemaps, it meant having to wait too long for the map to populate, etc. etc. I can't really remember the details.

Having said all that, you might want to look into lyr.on("error", handler) functionality to see if it will serve your purposes. Again, I can't remember details re: using it but I think that if you create your secured layer object and then attach a handler for an error before you add it to the map, you might be able to work with that somehow.

Sorry if this just confuses the issue, but hope it helps.

Cheers,

jtm

LauraMiles1
Occasional Contributor III

Thanks for responding - anything is helpful, I will give that a try.

0 Kudos