Setting a variable for beforeFilter to catch?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Setting a variable for beforeFilter to catch?

cypher543

I have a one-user blog, so I don't have a big fancy authentication
scheme. What I want to do is "lock" certain actions so that they are
only visible if my session is set. Here's what I did... (without the
session checks done)

In app_controller.php, I defined the beforeFilter() function:

<?php
class AppController extends Controller {
        function beforeFilter() {
                // if action is locked
                if ($this->locked) {
                        $this->flash("This page is for admins only!", "/");
                        $this->locked = false;
                        exit();
                }
        }
}
?>

In entries_controller.php, under the "write" action, I did the
following:

$this->locked = true;

When I visit /entries/write, I get:

Notice: Undefined property: EntriesController::$locked in
/home/---/cakeapps/---/app_controller.php on line 5

Anyone have any ideas? Sorry if there are some dumb mistakes that that
code. I haven't used PHP for awhile and I'm still new to CakePHP.

Thanks,
David


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Setting a variable for beforeFilter to catch?

francky06l

You should store your variable "lock" into the Session Object (ei :
Session->write(), Session->check or read ). When reading just check the
existence of the variable and it's value (in beforeFilter)...

On Nov 8, 11:14 pm, "cypher543" <[hidden email]> wrote:

> I have a one-user blog, so I don't have a big fancy authentication
> scheme. What I want to do is "lock" certain actions so that they are
> only visible if my session is set. Here's what I did... (without the
> session checks done)
>
> In app_controller.php, I defined the beforeFilter() function:
>
> <?php
> class AppController extends Controller {
>         function beforeFilter() {
>                 // if action is locked
>                 if ($this->locked) {
>                         $this->flash("This page is for admins only!", "/");
>                         $this->locked = false;
>                         exit();
>                 }
>         }}?>
>
> In entries_controller.php, under the "write" action, I did the
> following:
>
> $this->locked = true;
>
> When I visit /entries/write, I get:
>
> Notice: Undefined property: EntriesController::$locked in
> /home/---/cakeapps/---/app_controller.php on line 5
>
> Anyone have any ideas? Sorry if there are some dumb mistakes that that
> code. I haven't used PHP for awhile and I'm still new to CakePHP.
>
> Thanks,
> David


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Setting a variable for beforeFilter to catch?

cypher543

Sorry if I wasn't clear on what I wanted...

$this->locked is defined so that I can use it in beforeFilter to check
if an action is supposed to be accessible by guests or admins. If
beforeFilter sees that $this->locked is true, then it will later check
if the admin session is created (which I haven't implemented yet). I'm
not trying to use "locked" in a session, I want to use it to initiate a
Session->read.

On Nov 8, 4:35 pm, "francky06l" <[hidden email]> wrote:

> You should store your variable "lock" into the Session Object (ei :
> Session->write(), Session->check or read ). When reading just check the
> existence of the variable and it's value (in beforeFilter)...
>
> On Nov 8, 11:14 pm, "cypher543" <[hidden email]> wrote:
>
> > I have a one-user blog, so I don't have a big fancy authentication
> > scheme. What I want to do is "lock" certain actions so that they are
> > only visible if my session is set. Here's what I did... (without the
> > session checks done)
>
> > In app_controller.php, I defined the beforeFilter() function:
>
> > <?php
> > class AppController extends Controller {
> >         function beforeFilter() {
> >                 // if action is locked
> >                 if ($this->locked) {
> >                         $this->flash("This page is for admins only!", "/");
> >                         $this->locked = false;
> >                         exit();
> >                 }
> >         }}?>
>
> > In entries_controller.php, under the "write" action, I did the
> > following:
>
> > $this->locked = true;
>
> > When I visit /entries/write, I get:
>
> > Notice: Undefined property: EntriesController::$locked in
> > /home/---/cakeapps/---/app_controller.php on line 5
>
> > Anyone have any ideas? Sorry if there are some dumb mistakes that that
> > code. I haven't used PHP for awhile and I'm still new to CakePHP.
>
> > Thanks,
> > David


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Setting a variable for beforeFilter to catch?

Shalanga

Cypher, i think you are right. This should work.

I guess the problem is that you are *extends* the wrong class.
Did you alter the /libs/controllers/app_control.php file? Or you
created a new one in another place?


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Setting a variable for beforeFilter to catch?

Bert Van den Brande

I think defining locked as a class instance var inside AppController
should solve the warning ?

On 11/9/06, Ismael S. Kafeltz <[hidden email]> wrote:

>
> Cypher, i think you are right. This should work.
>
> I guess the problem is that you are *extends* the wrong class.
> Did you alter the /libs/controllers/app_control.php file? Or you
> created a new one in another place?
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Setting a variable for beforeFilter to catch?

themanfrombucharest

If I understood your explanation right then this is what happens:
the beforeFilter() method gets called before your action method so
$this->locked isn't set yet!
you could try to have a special class var for your controller that
stores the locked actions:

class EntriesController extends AppController {
    var $locked = array('write', 'etc');
}

and then:

class AppController extends Controller {
    function beforeFilter() {
        if (isset($this->locked[$this->action])) {
            //do your thing
        }
    }
}

On Nov 9, 1:39 pm, "Bert Van den Brande" <[hidden email]> wrote:

> I think defining locked as a class instance var inside AppController
> should solve the warning ?
>
> On 11/9/06, Ismael S. Kafeltz <[hidden email]> wrote:
>
>
>
> > Cypher, i think you are right. This should work.
>
> > I guess the problem is that you are *extends* the wrong class.
> > Did you alter the /libs/controllers/app_control.php file? Or you
> > created a new one in another place?


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---