start of flag system
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 25 Apr 2010 08:45:36 +0000 (08:45 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sun, 25 Apr 2010 08:45:36 +0000 (08:45 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@436 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

wob/basics.wolf
wob/user.wolf
www/inc/machine/session.php

index d686019..e7a78e3 100644 (file)
@@ -17,8 +17,8 @@
                <Preset><V col="ckey" val="ReserveStop"/><V col="cval" val="48"/></Preset>
                <Preset><V col="ckey" val="TicketIDChars"/><V col="cval" val="10"/></Preset>
                <Preset><V col="ckey" val="VoucherIDChars"/><V col="cval" val="10"/></Preset>
-               <Preset><V col="ckey" val="Flag admin"/><V col="cval" val="User is Admin"/></Preset>
-               <Preset><V col="ckey" val="Flag web"/><V col="cval" val="User is Customer"/></Preset>
+               <Preset><V col="ckey" val="Flag _admin"/><V col="cval" val="User is Admin"/></Preset>
+               <Preset><V col="ckey" val="Flag _web"/><V col="cval" val="User is Customer"/></Preset>
        </Table>
        
        
@@ -41,4 +41,7 @@
                        <Var name="file" type="blob"/>
                </Output>
        </Transaction>
+       
+       <Transaction name="GetValidFlags"/>
+               <!-- Call lang="php" method=" -->
 </Wolf>
index 08de0f3..d44b6d4 100644 (file)
                <Preset>
                        <V col="rolename" val="_admin"/>
                        <V col="description" val="Admin Users"/>
-                       <V col="flags" val="admin"/>
+                       <V col="flags" val="_admin"/>
                </Preset>
                <Preset>
                        <V col="rolename" val="_web"/>
                        <V col="description" val="Web Interface, Customers"/>
-                       <V col="flags" val="web"/>
+                       <V col="flags" val="_web"/>
                </Preset>
        </Table>
        
index a61cebf..41e51a9 100644 (file)
@@ -23,6 +23,7 @@ class Session
        protected $user="";
        protected $roles;
        protected $rights;
+       protected $flags;
        
        /**construct the session object, check validity*/
        public function __construct($trans)
@@ -30,12 +31,14 @@ class Session
                global $db,$session;
                $this->roles=array();
                $this->rights=array();
+               $this->flags=array();
                $sid=$trans->getHeader("Wob-SessionId");
                $res=$db->select("session","sessionid,user","sessionid=".$db->escapeString($sid));
                if(count($res)>0){
                        $this->sessid=$sid;
                        $this->user=$res[0]["user"];
                        $this->initRights();
+                       $this->initFlags($res[0]["flags"]);
                }
                $session=$this;
        }
@@ -76,12 +79,34 @@ class Session
                }
        }
        
+       /**internal: retrieve and remember the flags of this user*/
+       protected function initFlags($flg)
+       {
+               global $db;
+               $this->flags=explode(" ",$flg);
+               $res=$db->select("userrole","role","uname=".$db->escapeString($this->user));
+               for($i=0;$i<count($res);$i++){
+                       $res2=$db->select("role","flags","rolename=".$db->escapeString($res[$i][0]));
+                       if(count($res2)>0){
+                               foreach(explode(" ",$res2[0][0]) as $f)
+                                       if(!in_array($f,$this->flags))
+                                               $this->flags[]=$f;
+                       }
+               }
+       }
+       
        /**returns all rights of this user*/
        public function getRights(){return $this->rights;}
        
        /**returns all roles of this user*/
        public function getRoles(){return $this->roles;}
        
+       /**returns the flags of this user*/
+       public function getFlags(){return $this->flags;}
+       
+       /**returns whether the user has a specific flag*/
+       public function hasFlag($f){return in_array($f,$this->flags);}
+       
        /**creates a new session, called from the Login transaction*/
        static public function login($trans)
        {
@@ -206,8 +231,30 @@ class Session
        {
                global $db;
                if(in_array("_admin",$this->roles))return true;
+               if(in_array("_admin",$this->flags))return true;
                return in_array($transaction,$this->rights);
        }
+       
+       /**checks the given flags item pattern (string or array of strings) and returns true if they match*/
+       public function checkFlags($iflg)
+       {
+               //admin shortcut
+               if($this->hasFlag("_admin"))return true;
+               if(in_array("_admin",$this-roles))return true;
+               //actual check
+               if(is_array($iflg))$fp=$iflg;
+               else $fp=explode(" ",$iflg);
+               foreach($fp as $f){
+                       if($f[0]=="+"){
+                               if(!$this->hasFlag(substr($f,1)))return false;
+                       }else
+                       if($f[0]=="-"){
+                               if($this->hasFlag(substr($f,1)))return false;
+                       }
+                       else return false;
+               }
+               return true;
+       }
 };
 
 /**dummy class used by browsed pages to represent the virtual web user*/
@@ -220,11 +267,19 @@ class DummyWebSession extends Session
                $this->user="(web)";
                //fake web role, web pages check for it
                $this->roles=array("_web");
+               $this->flags=array("_web");
                //get rights
                $this->rights=array();
-               $res2=$db->select("roleright","rightname","rolename=".$db->escapeString($res[$i][0]));
+               $res2=$db->select("roleright","rightname","rolename='_web'");
                for($j=0;$j<count($res2);$j++)
                        $this->rights[]=$res2[$j][0];
+               //get flags
+               $res2=$db->select("role","flags","rolename='_web'");
+               if(count($res2)>0){
+                       foreach(explode(" ",$res2[0][0]) as $f)
+                               if(!in_array($f,$this->flags))
+                                       $this->flags[]=$f;
+               }
        }
 }