Let's create our access rights matrix

Have a look at this : http://demo.flexqueries.org/useraccess

As I told previously, we're going to write a user access management interface. We have now users and groups tables, a module table (a module is a part of our virtual application, that a user may access or not), and an access table for groups and users. We'll now focus on the group access and produce a matrix that will look like this :

This will be basically done with a TileGroup.

The first row is retrieved with the following query : Select * from groups order by group_name

The first column is obtained with Select * from modules order by module_key

Every checkbox is checked if Select count(*) from groupaccess where gid = ? and mid = ? returns something greater than 0.

Keep your calm : the following code can be optimized to minimize the number of queries executed !

 

 

            import mx.controls.CheckBox;
            import mx.controls.Label;
            import mx.events.FlexEvent;
           
            import org.flexqueries2.utils.logging.Logger;

            /**
             * Initializations
             */
            protected function group1_creationCompleteHandler(event:FlexEvent):void
            {
                //Initialize the events we'll wait for, before building our matrix:
                waiter.waitFor(qGroups, 'selectComplete');
                waiter.waitFor(qModules, 'selectComplete');
               
                //Send the queries asynchronously
                qGroups.selectAsync();
                qModules.selectAsync();
            }

            /**
             * Builds the matrix
             */
            protected function waiter_everythingReadyHandler(event:Event):void
            {
                var lbl:Label;
               
                // +1 to add the header column and row
                this.requestedColumnCount = qGroups.rowCount + 1;
                this.requestedRowCount = qModules.rowCount + 1;
               
                //We create a first row with all the users names
               
                //Add an empty label in the top-left corner
                addLabel('');
               
                //We read a first time the qGroups SqlQuery (we'll rewind it later in the checkbox creation loop)
                while(qGroups.read())
                    addLabel(qGroups.fieldByName('GROUP_NAME').asString, "titleLabel");
               
                //For each row in our matrix...
                while(qModules.read())
                {
                    //Add the module name in the header column
                    addLabel(qModules.fieldByName('MODULE_NAME').asString, "titleLabel");
                   
                    qGroups.rewind();
                    while(qGroups.read())
                    {
                        var chk:CheckBox = new CheckBox();
                       
                        chk.data = qGroups.fieldByName('GROUP_NAME').asString + ' - ' +
                                   qModules.fieldByName('MODULE_KEY').asString;
                       
                        //Check the checkbox if necessary
                        chk.selected = isChecked(qGroups.fieldByName('GID').asNumber, qModules.fieldByName('MID').asNumber);
                       
                        this.addElement(chk);
                    }
                }
            }
           
            /**
             * Returns true if the checkbox must be checked, ie. if there is
             * a row in groupaccess for the group id and module id specified.
             */
            private function isChecked(gid:Number, mid:Number):Boolean
            {
                qModulesByGroup.sql = "Select count(*) CNT from groupaccess where gid = ? and mid = ?";
                qModulesByGroup.sqlParams = [gid, mid];
               
                //It's practical to send this query synchronously, isn't it?
                qModulesByGroup.selectSync();
               
                //read the first (and unique) row
                qModulesByGroup.read();
                var count:Number = qModulesByGroup.fieldByName('CNT').asNumber;
               
                return count > 0;
            }
           
            /**
             * Adds a label in the TileGroup
             */
            private function addLabel(text:String, cssClass: String = ""):Label
            {
                var lbl:Label = new Label();
                lbl.text = text;
               
                if(cssClass != "")
                    lbl.styleName = cssClass;
               
                this.addElement(lbl);
                return lbl;
            }

 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <pre>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.