Index: trunk/prototype/services/PostgreSQL.php
===================================================================
--- trunk/prototype/services/PostgreSQL.php	(revision 7673)
+++ trunk/prototype/services/PostgreSQL.php	(revision 7795)
@@ -47,23 +47,70 @@
     private $config; //Configuração
     public  $error = false; //Armazena um erro caso ocorra
-    
-    public function find ( $uri, $justthese = false, $criteria = false ){
-                   
-        $map =  Config::get($uri['concept'], 'PostgreSQL.mapping');
-        
-    $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map) : '';
-
-    $justthese = self::parseJustthese($justthese, $map);
-
-    return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .' '.$criteria );
-    }
-
-   public function read ( $uri, $justthese = false , $criteria = false){
-   
-      $map =  Config::get($uri['concept'], 'PostgreSQL.mapping');   
-      $justthese = self::parseJustthese($justthese, $map);
-      $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'';
-   
-      return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. (Config::get($uri['concept'],'PostgreSQL.concept')) .$criteria , true );
+    private $maps; //Cache de maps
+    private $tables;
+
+    public function find ( $uri, $justthese = false, $criteria = false )
+    {
+        $condition = '';
+
+        if(!isset($this->maps[$uri['concept']]) || !isset($this->tables[$uri['concept']]))
+        {
+            $this->maps[$uri['concept']] = Config::get($uri['concept'], 'PostgreSQL.mapping');
+            $this->tables[$uri['concept']] =  Config::get($uri['concept'],'PostgreSQL.concept');
+        }
+
+        $tables = $this->tables[$uri['concept']];
+
+        $justthese = self::parseJustthese($justthese, $this->maps[$uri['concept']] , $this->tables[$uri['concept']]);
+
+        if(isset($criteria['condition']))
+        {
+            $pc = $this->parseCondition($criteria['condition']);
+
+            if(is_array($pc))
+            {
+                if(!in_array($this->tables[$uri['concept']], $pc['tables']))
+                    $pc['tables'][] = $this->tables[$uri['concept']];
+
+                $tables = implode(',', $pc['tables'] );
+                $condition .= ' WHERE ' . $pc['conditions'];
+            }
+
+        }
+
+        $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $this->maps[$uri['concept']] , $condition , $this->tables[$uri['concept']]) : $condition;
+
+        return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. $tables .' '.$criteria );
+    }
+
+   public function read ( $uri, $justthese = false , $criteria = false)
+   {
+       if(!isset($this->maps[$uri['concept']]) || !isset($this->tables[$uri['concept']]))
+       {
+           $this->maps[$uri['concept']] = Config::get($uri['concept'], 'PostgreSQL.mapping');
+           $this->tables[$uri['concept']] =  Config::get($uri['concept'],'PostgreSQL.concept');
+       }
+
+       $condition = ' WHERE '.$this->tables[$uri['concept']].'.'.$this->maps[$uri['concept']]['id'].' = \''.addslashes( $uri['id'] ).'\'';
+       $justthese = self::parseJustthese($justthese, $this->maps[$uri['concept']] , $this->tables[$uri['concept']]);
+       $tables = $this->tables[$uri['concept']];
+
+       if(isset($criteria['condition']))
+       {
+           $pc = $this->parseCondition($criteria['condition']);
+
+           if(is_array($pc))
+           {
+               if(!in_array($this->tables[$uri['concept']], $pc['tables']))
+                   $pc['tables'][] = $this->tables[$uri['concept']];
+
+               $tables = implode(',', $pc['tables'] );
+               $condition .= ' AND ' .  $pc['conditions'];
+           }
+       }
+
+       $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $this->maps[$uri['concept']] , $condition , $this->tables[$uri['concept']]) : $condition;
+
+       return $this->execSql( 'SELECT '.$justthese['select'].' FROM '. $tables . ' ' . $criteria , true );
     }
     
@@ -85,8 +132,9 @@
         return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).' '.self::parseCriteria($criteria , $map));
     }
-        
+
     public function update ( $uri,  $data, $criteria = false ){
             $map = Config::get($uri['concept'], 'PostgreSQL.mapping');
-        $criteria = ($criteria !== false) ? $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'';
+        $criteria = ($criteria !== false) ?
+            $this->parseCriteria ( $criteria , $map , ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'') : ' WHERE '.$map['id'].' = \''.pg_escape_string( $uri['id'] ).'\'';
 
         return $this->execSql('UPDATE '.(Config::get($uri['concept'],'PostgreSQL.concept')).' '. self::parseUpdateData( $data ,$map).$criteria);
@@ -204,5 +252,5 @@
     
     private static function parseUpdateData( $data , &$map){
-                                        
+
         $d = array();
         foreach ($data as $i => $v)
@@ -216,6 +264,6 @@
     }
 
-    private static function parseCriteria( $criteria  , &$map , $query = '' ){               
-    
+    private static function parseCriteria( $criteria  , &$map , $query = '' ){
+
         if( isset($criteria["filter"]) && $criteria["filter"] !== NULL )
         {
@@ -230,5 +278,6 @@
           *     array( '=' , 'campo' , 'valor' )
         */
-                $query .= ($query === '') ?  'WHERE ('.self::parseFilter( $criteria['filter'] , $map).')' : ' AND ('.self::parseFilter( $criteria['filter'] , $map).')';
+               if($fc = self::parseFilter( $criteria['filter'] , $map))
+                    $query .= ($query === '') ?  'WHERE ('.$fc.')' : ' AND ('.$fc.')';
         }
         /*
@@ -323,8 +372,8 @@
     }
     }
-    
-    private static function parseJustthese($justthese , &$map)
-    {
-                  
+
+    static function parseJustthese($justthese , &$map , $table = '')
+    {
+
         if(!is_array($justthese)) //Caso seja um full select pegar todas as keys
             $justthese = array_keys($map);
@@ -332,5 +381,8 @@
         $return = array();
 
-        foreach ($justthese as &$value) 
+        if($table)
+            $table .= '.';
+
+        foreach ($justthese as &$value)
         {
             if(!isset($map[$value])) continue; //Escapa itens não existentes no mapa
@@ -339,9 +391,67 @@
                 $return['deepness'][$value] = $map[$value];
             else
-                $return['select'][] = $map[$value] .' as "'. $value. '"';
-        }
-        
+                $return['select'][] = $table . $map[$value] .' as "'. $value. '"';
+        }
+
         $return['select'] = implode(', ', $return['select']);
-        return $return;  
+        return $return;
+    }
+
+    private function parseCondition( $condition )
+    {
+        $tables = array();
+        $conditions = '';
+
+            $matches = array();
+            if(preg_match_all('/\s*(AND|^)\s*([a-z]+)\.([a-z]+)\s+\=\s+([a-z]+)\.([a-z]+)(\s|$)+/i', $condition ,$matches,PREG_SET_ORDER))
+            {
+               foreach ($matches as $i => $v)
+               {
+                   if(!isset($this->maps[$v[2]]) || !isset($this->tables[$v[2]]))
+                   {
+                       $this->maps[$v[2]] = Config::get($v[2], 'PostgreSQL.mapping');
+                       $this->tables[$v[2]] =  Config::get($v[2],'PostgreSQL.concept');
+                   }
+                   if(!isset($this->maps[$v[4]]) || !isset($this->tables[$v[4]]))
+                   {
+                       $this->maps[$v[4]] = Config::get($v[4], 'PostgreSQL.mapping');
+                       $this->tables[$v[4]] =  Config::get($v[4],'PostgreSQL.concept');
+                   }
+
+                   if(isset($this->maps[$v[2]][$v[3]]) && isset($this->maps[$v[4]][$v[5]]))
+                       $conditions .= ' '. $v[1] .' '. $this->tables[$v[2]] . '.' . $this->maps[$v[2]][$v[3]] .' = '. $this->tables[$v[4]] . '.' . $this->maps[$v[4]][$v[5]];
+                   else
+                       continue;
+
+                   if(!in_array( $this->tables[$v[2]], $tables))
+                       $tables[] = $this->tables[$v[2]];
+
+                   if(!in_array( $this->tables[$v[4]], $tables))
+                       $tables[] = $this->tables[$v[4]];
+               }
+
+            }
+
+            if(preg_match_all('/\s*(AND|OR|^)\s*([a-z]+)\.([a-z]+)\s+([\=\>\<\!]+|like)+\s+([a-z0-9\/\+\=]+)(\s|$)+/i', $condition , $matches ,PREG_SET_ORDER))
+            {
+                foreach ($matches as $i => $v)
+                {
+                    if(!isset($this->maps[$v[2]]) || !isset($this->tables[$v[2]]))
+                    {
+                        $this->maps[$v[2]] = Config::get($v[2], 'PostgreSQL.mapping');
+                        $this->tables[$v[2]] =  Config::get($v[2],'PostgreSQL.concept');
+                    }
+
+                    if(isset($this->maps[$v[2]][$v[3]]))
+                        $conditions .= ' '. $v[1] .' '. $this->tables[$v[2]] . '.' . $this->maps[$v[2]][$v[3]] .' '.$v[4].' \''. pg_escape_string(base64_decode($v[5])) .'\'';
+                    else
+                        continue;
+
+                    if(!in_array( $this->tables[$v[2]], $tables))
+                        $tables[] = $this->tables[$v[2]];
+                }
+            }
+
+        return (count($tables) > 0 && count($conditions ) > 0) ? array('tables' => $tables , 'conditions' => $conditions ) : '' ;
     }
 
Index: trunk/prototype/config/schedulable.ini
===================================================================
--- trunk/prototype/config/schedulable.ini	(revision 7702)
+++ trunk/prototype/config/schedulable.ini	(revision 7795)
@@ -17,4 +17,5 @@
 
 [before.find]
+schedulableSecurity = modules/calendar/interceptors/DBMapping.php
 findDeepnessOne = modules/calendar/interceptors/DBMapping.php
 findSchedulable = modules/calendar/interceptors/DBMapping.php
@@ -34,4 +35,7 @@
 createHistoric = modules/calendar/interceptors/DBMapping.php
 
+[before.read]
+schedulableSecurity = modules/calendar/interceptors/DBMapping.php
+
 [after.read]
 deepnessReadEvent = modules/calendar/interceptors/DBMapping.php
@@ -40,4 +44,5 @@
 deleteSchedulable = modules/calendar/interceptors/DBMapping.php
 deleteEvent = modules/calendar/interceptors/Notifications.php
+
 
 [PostgreSQL.mapping]
Index: trunk/prototype/config/calendarSignature.ini
===================================================================
--- trunk/prototype/config/calendarSignature.ini	(revision 6996)
+++ trunk/prototype/config/calendarSignature.ini	(revision 7795)
@@ -14,4 +14,10 @@
 deepnessFindCalendarSignature = modules/calendar/interceptors/DBMapping.php
 createDefaultGroup = modules/calendar/interceptors/DBMapping.php
+
+[before.read]
+calendarSignatureSecurity = modules/calendar/interceptors/DBMapping.php
+
+[before.find]
+calendarSignatureSecurity = modules/calendar/interceptors/DBMapping.php
 
 [after.read]
Index: trunk/prototype/modules/calendar/interceptors/DBMapping.php
===================================================================
--- trunk/prototype/modules/calendar/interceptors/DBMapping.php	(revision 7747)
+++ trunk/prototype/modules/calendar/interceptors/DBMapping.php	(revision 7795)
@@ -164,5 +164,30 @@
 
     public function encodeSignatureAlarmType(&$uri, &$params, &$criteria, $original) {
-	$params['type'] = self::codeAlarmType($params['type']);
+	    $params['type'] = self::codeAlarmType($params['type']);
+    }
+
+    public function schedulableSecurity(&$uri, &$params, &$criteria, $original)
+    {
+        $security = 'schedulable.id = calendarToSchedulable.schedulable';
+        $security .= ' AND calendar.id = calendarToSchedulable.calendar';
+        $security .= ' AND calendar.id = calendarSignature.calendar';
+        $security .= ' AND calendarSignature.user = ' . base64_encode(Config::me('uidNumber'));
+
+        $criteria['condition'] = $security;
+    }
+
+    public function calendarSecurity(&$uri, &$params, &$criteria, $original)
+    {
+        $security = 'calendar.id = calendarSignature.calendar';
+        $security .= ' AND calendarSignature.user = ' . base64_encode(Config::me('uidNumber'));
+
+        $criteria['condition'] = $security;
+    }
+
+    public function calendarSignatureSecurity(&$uri, &$params, &$criteria, $original)
+    {
+        $security = 'calendarSignature.user = ' . base64_encode(Config::me('uidNumber'));
+
+        $criteria['condition'] = $security;
     }
 
