using sfGuard for my own user model

Questions relating to sfGuardPlugin or other user management plugins

using sfGuard for my own user model

Postby leonidas79 » Tue Aug 03, 2010 11:27 pm

hello everyone
i want know is there is a way to use sfGuard for my own user model and benefit from all sfguard options . i used it for the backend application and it works fine , but i want use it for the frontend application with my own login form.
i tied it but the problem is that when a user is authenticated in the frontend application he can accesss to the backened cos in my login action i used $this->getUser()->setAuthenticated(true);

i hope i'm clear enough , thx for help
leonidas79
Junior Member
 
Posts: 20
Joined: Mon May 31, 2010 4:39 pm
Location: Morocco

Re: using sfGuard for my own user model

Postby claudia_k » Wed Aug 04, 2010 10:03 pm

Shouldn't it be enough to change the cookie names for frontend and backend? Then the frontend and backend apps should require separate logins. I have not tried that (yet) though so I would appreciate if you could post the results.

Good luck

Claudia
claudia_k
Faithful Member
 
Posts: 265
Joined: Mon Jun 21, 2010 8:12 pm
Location: Luxembourg

Re: using sfGuard for my own user model

Postby halfer » Thu Aug 05, 2010 6:43 pm

You can also override the sfGuardAuth module in your application, and use your own form. I do this in 1.0, and would be surprised if it were not possible in 1.3/1.4.
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: using sfGuard for my own user model

Postby leonidas79 » Tue Aug 17, 2010 2:33 am

thx for ur answer , but it didn't work , i don't why
if i'm logged in the frontend app and switch to the backend app i have the "Credentials Required,This page is in a restricted area." page , it's cos when i'm logged in the frontend app i set setAuthenticated to true , but it shouldn't mix both sessions i guess.

let me know plz if there is another solution for this case.

thank you
leonidas79
Junior Member
 
Posts: 20
Joined: Mon May 31, 2010 4:39 pm
Location: Morocco

Re: using sfGuard for my own user model

Postby lucasaba » Tue Aug 17, 2010 10:47 am

Didi you try to change the cookie name in the backend application as suggested by claudia_k ?

To me it works.

Code: Select all
//project/backend/config/factories.yml
all:
  [...]
  storage:
    class: sfSessionStorage
    param:
      session_name: backend_cookie_name
It is easier to move a problem around (for example, by moving the problem to a different part of the overall network architecture) than it is to solve it.
6th truth - rfc1925
User avatar
lucasaba
Senior Member
 
Posts: 106
Joined: Tue Jun 12, 2007 5:33 pm

Re: using sfGuard for my own user model

Postby leonidas79 » Tue Aug 17, 2010 1:38 pm

thank you so much
it's working , when i tried claudia_k's solution i did a mistake in factories.yml file , now with ur sample code it's working fine.
i have another question : how to prevent users in frontend app to access some actions unless they are authenticated ? ( ps: i'm not using sfguard in the frontend app , i have a custom form for authentication )

thank you all for helping
leonidas79
Junior Member
 
Posts: 20
Joined: Mon May 31, 2010 4:39 pm
Location: Morocco

Re: using sfGuard for my own user model

Postby lucasaba » Tue Aug 17, 2010 1:46 pm

If you're not using sfGuard you can add credential during login.
I always use sfGuard but, if I recall well, that's how it should work: in your myUser.class.php, based on your rules, you can call the function:

Code: Select all
$this->addCredential('permit_some_action');


Then, again in the module you want to protect, add a config folder and create e security.yml file like this:

Code: Select all
//project/apps/frontend/module_to_protect/config/security.yml
name_of_the_action:
  credentials: permit_some_action


This should work....
It is easier to move a problem around (for example, by moving the problem to a different part of the overall network architecture) than it is to solve it.
6th truth - rfc1925
User avatar
lucasaba
Senior Member
 
Posts: 106
Joined: Tue Jun 12, 2007 5:33 pm

Re: using sfGuard for my own user model

Postby leonidas79 » Tue Aug 17, 2010 2:06 pm

i tried to add credentials during login but it's not working
Code: Select all
public function executeIndex(sfWebRequest $request) {
        $this->form = new LoginForm();
        if ($request->isMethod('post')) {
            $this->form->bind($request->getParameter('login'));
            if ($this->form->isValid()) {
                $login  =  $this->form->getValue('login');
                $password  =  $this->form->getValue('password');

                $q = Doctrine_Query::create()
                        ->from('User u')
                        ->where('u.login = ? AND u.password = ?',array($login,$password));

                $users = $q->execute();

                if ($users->count() == 1) {
                    $this->getUser()->setAuthenticated(true);
                    $this->getUser()->addCredential('user');
                    $this->getRequest()->getParameterHolder()->set('user',$users[0]);
                    ....

                }else {
                    $this->getUser()->setFlash('notice', 'Login ou Mot de passe invalid !');
                }
            }

        }
    }


Code: Select all
 public function executeLogout(sfWebRequest $request) {
        $this->user = null;
        $this->clearCredentials();
        $this->setAuthenticated(false);
        $this->redirect('connexion/index');
    }


it seems that the logout is not working , i still have the user in the session and authenticated , is there something wrong in my code ?

thx
leonidas79
Junior Member
 
Posts: 20
Joined: Mon May 31, 2010 4:39 pm
Location: Morocco

Re: using sfGuard for my own user model

Postby lucasaba » Tue Aug 17, 2010 5:52 pm

I think there's an error here:

Code: Select all
public function executeLogout(sfWebRequest $request) {
   //$this->user = null;
   $this->getUser()->clearCredentials()
   //$this->clearCredentials();
   //$this->setAuthenticated(false);
   $this->getUser()->setAuthenticated(false);
   $this->redirect('connexion/index');
}


You call the user object from the action within your module and then un-authenticate it.
It is easier to move a problem around (for example, by moving the problem to a different part of the overall network architecture) than it is to solve it.
6th truth - rfc1925
User avatar
lucasaba
Senior Member
 
Posts: 106
Joined: Tue Jun 12, 2007 5:33 pm

Re: using sfGuard for my own user model

Postby leonidas79 » Tue Aug 17, 2010 9:50 pm

it's not working , the user is still authenticated after Logout Action.
i don't know why .
leonidas79
Junior Member
 
Posts: 20
Joined: Mon May 31, 2010 4:39 pm
Location: Morocco

Re: using sfGuard for my own user model

Postby halfer » Thu Aug 19, 2010 1:19 pm

I am not clear whether you want your two apps to be accessible on the same cookie. Set them to the same name if so, and you will find that logging into one app will permit access on both.

As per @lucasaba's post, your logout method won't work as it is. You need to execute clearCredentials and setAuthenticated on the user object, not on the action. I wonder though whether this is executed at all, since if you tried it, PHP should halt with a fatal error. So... make sure this action is actually being called!
halfer
Faithful Member
 
Posts: 10148
Joined: Mon Jan 30, 2006 1:16 pm
Location: West Midlands, UK

Re: using sfGuard for my own user model

Postby leonidas79 » Thu Aug 19, 2010 4:15 pm

thank you , it' works fine now.
i used clearCredentials and setAuthenticated on the user object.
leonidas79
Junior Member
 
Posts: 20
Joined: Mon May 31, 2010 4:39 pm
Location: Morocco


Return to User management plugins

Who is online

Users browsing this forum: No registered users and 1 guest