Titan Movil
Bienvenido a titan movil, te invitamos a registrate para acceder a todo el contenido del foro



Unirse al foro, es rápido y fácil

Titan Movil
Bienvenido a titan movil, te invitamos a registrate para acceder a todo el contenido del foro

Titan Movil
¿Quieres reaccionar a este mensaje? Regístrate en el foro con unos pocos clics o inicia sesión para continuar.

No estás conectado. Conéctate o registrate

Aprende mas Cita textual cacheo de acl usando session

+9
botlammer
omymasterx
barriobravo1987
cosanada3250
duarte
CuervoMB1
estarmantrax
HACKER1986
Krhono
13 participantes

Ir abajo  Mensaje [Página 1 de 1.]

Krhono

Krhono
TITAN MASTER
TITAN MASTER

Código:
ACL Caching using Session
by macduy  on January 05, 2010
ACL checks can be costly and would benefit from caching. One approach would be to use Cake's Caching system, as implemented in http://bakery.cakephp.org/articles/view/caching-acl-permissions-with-cachedaclcomponent. Presented here is a different approach, where the cache is stored using Session.
How it works
Results of ACL checks are stored in the Session variable 'Acl'. Every time an ACL check is performed, the component looks into this variable to see if the result has been stored, if not, it performs the usual check using the original AclComponent and then stores the result in the variable.

The limitations of this method is its reliance on the Session component. Also, the cache remains active until the user destroys the session, either by logging out or manually. This will cause problems if ACL rights tend to change often while the Session remains active.

Getting the code
To use the component, download the code on the next page and store it in /app/controllers/components/ as session_acl.php.

How to use

To use the component, include it in the $component variable, along with Session. Do not include the 'Acl' component.
<?php

var $components = array('Session','SessionAcl');?>

The component stores itself as $this->Acl and so you can continue using e.g.
<?php
$this->Acl->check($user_alias, "Posts", "read");
?>
as usual. The only difference is that now SessionAcl is working for you. All standard functions deny, allow etc. work as well.

If you need to destroy the cache, call

<?php
$this->Acl->flushCache();
?>

or delete the 'Acl' variable from the Session.

Extra functions
all()
SessionAcl comes with extra functions to simplify certain checks. Say you would like to allow a user to access something only if he has read access to A and create access to B. Normally, you would write:

<?php
if ($this->Acl->check($user_alias, "A", "read") && $this->Acl->check($user_alias, "B", "create"))
?>

Using SessionAcl, you can do:

<?php
if ($this->Acl->all($user_alias, array("A" => "read", "B" => "create")))
?>

one()
Similarly, if you would like to allow the user only if he has read access to at least one of A, B, C or D, instead of writing

<?php
if ($this->Acl->check($user_alias, "A", "read") || $this->Acl->check($user_alias, "B", "read") || $this->Acl->check($user_alias, "C", "read") || $this->Acl->check($user_alias, "D", "read"))
?>

you now can write

<?php
if ($this->Acl->one($user_alias, array("A" => "read", "B" => "read", "C" => "read", "D" => "read")))
?>

can()
Sometimes you want to perform several checks at once. Say you'd like to know if a user has read,create and delete access to A,B,C respectively, at once. You can do

<?php
$result = $this->Acl->can($user_alias, array("A" => "read", "B" => "create", "C" => "delete"))
?>

$result now contains, say:
<?php
[true, false, true]
?>

Use it with array_combine to simplify work:

<?php
$can = array_combine(array("read_A","create_B","delete_C"), $this->Acl->can($user_alias, array("A" => "read", "B" => "create", "C" => "delete"));

if ($can["create_B"]) {...


Component Class:
<?php  
/**
* ACL Caching.
*
* Yet another take at Caching ACL queries, now using Session.
* Adapted from http://www.nabble.com/ACL-Auth-Speed-Issues-td21386047.html
* and bits and pieces taken from cached_acl.php
*
* It also extends ACL with some nifty functions for easier and simpler code.
*
* Cake's ACL doesn't cache anything. For better performance, we
* put results of check into session. Only ::check() is wrapped,
* other functions are simply piped to the parent Acl object,
* though it can be handy to wrap these too in future.
*
* @author macduy
*/
App::import('Component', 'Acl');
App::import('component', 'Session');
class SessionAclComponent extends AclComponent
{

   function initialize(&$controller)
   {
       $this->master =& $controller;
       $controller->Acl =& $this;
       $this->Session = new SessionComponent();
   }
   
   function check($aro, $aco, $action = "*")
   {
       $path = $this->__cachePath($aro, $aco, $action);
       if ($this->Session->check($path))
       {
           return $this->Session->read($path);
       } else
       {
           $check = parent::check($aro, $aco, $action);
           $this->Session->write($path, $check);
           return $check;
       }
   }

   /**
    * Allow
    */
   function allow($aro, $aco, $action = "*")
   {
       parent::allow($aro, $aco, $action);
       $this->__delete($aro, $aco, $action);
   }

   /**
    * Deny method.
    */
   function deny($aro, $aco, $action = "*")
   {
       parent::deny($aro, $aco, $action);
       $this->__delete($aro, $aco, $action);
   }

   /**
    * Inherit method.
    *
    * This method overrides and uses the original
    * method. It only adds cache to it.
    *
    * @param string $aro ARO
    * @param string $aco ACO
    * @param string $action Action (defaults to *)
    * @access public
    */
   function inherit($aro, $aco, $action = "*")
   {
       parent::inherit($aro, $aco, $action);
       $this->__delete($aro, $aco, $action);
   }

   /**
    * Grant method.
    *
    * This method overrides and uses the original
    * method. It only adds cache to it.
    *
    * @param string $aro ARO
    * @param string $aco ACO
    * @param string $action Action (defaults to *)
    * @access public
    */
   function grant($aro, $aco, $action = "*")
   {
       parent::grant($aro, $aco, $action);
       $this->__delete($aro, $aco, $action);
   }

   /**
    * Revoke method.
    *
    * This method overrides and uses the original
    * method. It only adds cache to it.
    *
    * @param string $aro ARO
    * @param string $aco ACO
    * @param string $action Action (defaults to *)
    * @access public
    */
   function revoke($aro, $aco, $action = "*")
   {
       parent::revoke($aro, $aco, $action);
       $this->__delete($aro, $aco, $action);
   }

   /**
    * Returns a unique, dot separated path to use as the cache key. Copied from CachedAcl.
    *
    * @param string $aro ARO
    * @param string $aco ACO
    * @param boolean $acoPath Boolean to return only the path to the ACO or the full path to the permission.
    * @access private
    */
   function __cachePath($aro, $aco, $action, $acoPath = false)
   {
       if ($action != "*")
       {
           $aco .= '/' . $action;
       }
       $path = Inflector::slug($aco);

       if (!$acoPath)
       {
           if (!is_array($aro))
           {
               $_aro = explode(':', $aro);
           } elseif (Set::countDim($aro) > 1)
           {
               $_aro = array(key($aro), current(current($aro)));
           } else
           {
               $_aro = array_values($aro);
           }
           $path .= '.' . Inflector::slug(implode('.', $_aro));
       }

       return "Acl.".$path;
   }

   /**
    * Deletes the cache reference in Session, if found
    */
    function __delete($aro, $aco, $action) {
        $key = $this->__cachePath($aro, $aco, $action, true);
        if ($this->Session->check($key))
        {
            $this->Session->delete($key);
        }
    }

    /**
     * Deletes the whole cache from the Session variable
     */
    function flushCache() {
        $this->Session->delete('Acl');
    }

    /**
     * Checks that ALL of given pairs of aco-action are satisfied
     */
    function all($aro, $pairs) {
        foreach ($pairs as $aco => $action)
        {
            if (!$this->check($aro,$aco,$action))
            {
                return false;
            }
        }
        return true;
    }


    /**
     * Checks that AT LEAST ONE of given pairs of aco-action is satisfied
     */
    function one($aro, $pairs) {
        foreach ($pairs as $aco => $action)
        {
            if ($this->check($aro,$aco,$action))
            {
                return true;
            }
        }
        return false;
    }
     
    /**
     * Returns an array of booleans for each $aco-$aro pair
     */
    function can($aro, $pairs) {
        $can = array();
        $i = 0;
        foreach ($pairs as $aco => $action)
        {
            $can[$i] = $this->check($aro,$aco,$action);
            $i++;
        }
        return $can;
    }
}
?>http://bakery.cakephp.org/articles/macduy/2010/01/05/acl-caching-using-session

HACKER1986

avatar
Experto
Experto

Shocked 

estarmantrax

estarmantrax
Master Symbian
Master Symbian

Aveces me encantaria poder aprender todo lo que saves krhono y poder ayudar descubriendo tricks y todo lo que posteas pero siempre pones tus garavatos sin siquiera explicar que ondas u solo entienden los que ya estan mas avanzados y tienen mayores conocimientos u.u y los que no nos jodimos a seguir como estamos espero mi comentario no lo tomes a mal :'(

CuervoMB1

CuervoMB1
Avanzado
Avanzado

Sin comentarios... Bueno creo que si fue comentario el no comentar.
Aprende mas Cita textual  cacheo de acl usando session Images?q=tbn:ANd9GcQOGyMxD-Mnr4z_eAqWinWt658aRwNXfBAbdn95c8RwoqNWuRJ4

http://crowptc.blogspot.mx/

duarte

duarte
Game Master
Game Master

nesecito estudiar mas

http://instagram.com/raaaaziel#

cosanada3250

cosanada3250
Gran Titan
Gran Titan

No tengo ni idea de qué es eso. :/

barriobravo1987

barriobravo1987
Gran Titan
Gran Titan

e_e esto es un post más que nos deja con más dudas

omymasterx

omymasterx
Gran Experto
Gran Experto

muy interesante Master.
algunas veces t excedes

botlammer

botlammer
Master Android
Master Android

krhono e encontrado enlaces a servidores de telcel como lo son piwik y cake php que esta en tu log. encontre una forma de usar un trick ideas en perfll internet.

jackblack

jackblack
Master Picture
Master Picture

El "localismo" sólo debería ser un problema si se tiene varios servidores web que distribuyan la carga

Si estoy entendiendo el tema, creo que el 'localismo' se puede evitar mediante el uso de las sesiones que utilizan la base de datos o métodos de almacenamiento mem cached distribuidos.

Después de que se implementa, como dice el artículo, los cambios de ACL no se contabilizan hasta que el usuario inicie sesión de nuevo.


La desventaja, como se señala en el artículo, es por supuesto el "localismo" y por lo tanto no puede ser adecuada en todas las situaciones.

franksanchez

franksanchez
Gran Titan
Gran Titan

Mediante uso de sesiones Smile
muy bueno...

javix777

javix777
Gran Titan
Gran Titan

chale krhono todo en parabola no entiendo... ojala algun dia pases algo que podamos entender...

martingaxi

martingaxi
Gran Titan
Gran Titan

me isiste bolas men

https://www.facebook.com/martin.gaxiolaespinoza

Contenido patrocinado



Volver arriba  Mensaje [Página 1 de 1.]

Permisos de este foro:
No puedes responder a temas en este foro.