I was going crazy always setting the current $user_id, $owner_id in my
controller and then testing again for empty() in my views. So I put
all my commonly used context variables into Session and wrote a
controller function to keep it initialized and in sync with the
controllers and views. (This might also be a good place to put random
things like the current page # from paginate).

Now in my view, I can just do something like:

if($cur['authenticated']) {
        $menubar[]="Welcome <b>{$othAuth->user('username')}</b>";
}

It makes my code a little more readable, but is it good practice, or
am I opening myself up to future problems or security risks?


file app_controller.php:

        function getCurrent() {
                // init $cur with defaults
                $keys = array('owner_id', 'bundle_id', 'asset_id', 'user_id',
'user_group_level');
                $this->cur = array_fill_keys($keys, NULL);
                $this->cur['authenticated']=false;
                if ($this->Session->valid()) {
                        // defaults to NULL if Session key not set
                        $this->cur['owner_id'] = 
$this->Session->read('Current.owner_id');
                        $this->cur['bundle_id'] = $this->Session-
>read('Current.bundle_id');
                        $this->cur['asset_id'] = 
$this->Session->read('Current.asset_id');

                        if ($this->Session->check('othAuth.' . 
$this->othAuth->hashkey)) {
                                $this->cur['authenticated']=true;
                                $this->cur['user_id'] = 
$this->othAuth->user('id');
                                $this->cur['user_group_level']= 
$this->othAuth->group('level');
                        }
                } else {
                        debug("************************ Error: session not 
valid, deleting
Current.");
                        $this->Session->del('Current');
                }
                // set $cur in Views
                $this->set('cur', $this->cur);
        }

        function setCurrent($key, $value) {
                $this->Session->write("Current.$key",$value);
                $this->cur[$key]=$value;
                $this->set('cur', $this->cur);  // why do I have to set again if
        }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to