[OpenLayers-Users] event listener for protocol.read() -- trying to set a login flag ..

gismgr gismgr at sec.state.ma.us
Tue Jun 12 12:44:57 PDT 2012


Hello all,

I need to show different layers in my map depending on whether the user is public/anonymous or has login credentials.  I'm using geoserver.

I can log in and out using forms with j_spring_security_check.  The difficulty is in trying to sense whether the user is logged in or not.

I've come up with a way to do it by attempting to query one of the secure layers, and setting the login flag to yes if it succeeds and no if it fails.  The problem is that javascript just blows right on past my protocol.read() command without setting the flag, unless I force it to wait with an alert, which is pretty ugly and not ideal.

Does OpenLayers have an event listener I can call for the script protocol so that the rest of the code won't fire until the login flag is set?  From googling around, it looks like there may once have been plans for this, but no one has been discussing it much recently...

Alternately, does anyone have a creative suggestion for nesting callback functions so this will work smoothly without recourse to alert()?  I've tried a few different ways but nothing really works.

Any suggestions greatly appreciated!

Code:

    Ext.onReady(function init() {

//Start with login = no
            login = "no";

            //function to parse and test the response - are there any features?
            var test = function(resp) {
                if (resp.features) {
                    login = "yes";  //change login to yes
                    } else {
                    login = "no";
                        //Do nothing.  It never actually gets here -
                        //if not logged in, it can't get a json response
                        //and errors out trying to parse the xml fail response
                        //But that's ok, that's why login defaults to no
                    }
                }

                var filter = new OpenLayers.Filter.Comparison({
                    type: OpenLayers.Filter.Comparison.EQUAL_TO,
                    property: 'ID',
                    value: 'TESTAREA'
                });

                protocol = new OpenLayers.Protocol.Script({
                    url:geoserver_url + '/wfs',
                    callbackKey: 'format_options',
                    callbackPrefix: 'callback:',
                    params: {
                        service: 'WFS',
                        version: '1.1.0',
                        srsName: 'EPSG:3857',
                        request: 'GetFeature',
                        typeName: 'Areas',
                        outputFormat: 'json',
                        cql_filter: filter
                    },
                    callback: test,
                });
                protocol.read();

                //have to do an alert here so login=yes has a chance to catch up
                alert ("Checking login credentials ...");

              //construct a logout button or a login form - reload the page after logging in or out, which will rerun the test protocol above and reset the login flag
                if (login == 'yes') {  //only gets here if we wait with the alert
                    document.getElementById('login').innerHTML='<center><font style="font-size:8pt; font-family:sans-serif;"><form name="logout" method="post" action="'+ geoserver_url+ '/j_spring_security_logout"     target="_blank"><input type="submit" value="Logout" onclick="location.reload();"></form></font></center>';
                    } else { //always goes here without the alert, even if protocol succeeds
                        document.getElementById('login').innerHTML='<font style="font-size:8pt; font-family:sans-serif;"><form name="login" method="post" action="' + geoserver_url + '/j_spring_security_check" target="_blank"><label for="username">Username: </label><input id="username" type="text" name="username" value=""><br/><label for="password">Password: </label><input  id="password" type="password" name="password"><input type="submit" value="Login" onclick="location.reload();"></form></font>';
                    }

//..... all the rest of the map code, do it slightly differently if login = yes or login = no

}); //end init



Kathryn Catlin
GIS Supervisor
Massachusetts Historical Commission
220 Morrissey Blvd
Boston, MA  02125
(617) 727-8470

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120612/1e5d1cb5/attachment-0001.html>


More information about the Users mailing list