Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
How can I create a user login mechanism in symfony 1.4
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Gaurav Sharma  
View profile  
 More options Jun 23 2011, 6:51 am
From: Gaurav Sharma <gaurav.v.sha...@gmail.com>
Date: Thu, 23 Jun 2011 03:51:37 -0700 (PDT)
Local: Thurs, Jun 23 2011 6:51 am
Subject: How can I create a user login mechanism in symfony 1.4

Hello everyone,

I want to create a login mechanism for users in my current project.

This is what I have done so far.

Created a function named executeLogin in the UsersActions class

class usersActions extends sfActions
{
    public function executeLogin(sfWebRequest $request)
    {
        $this->form = new UsersLoginForm();
        if($request->isMethod('post'))
        {

$this->form->bind($request->getParameter($this->form->getName()));
            if($this->form->isValid())
            {
                $loginSuccess = Doctrine_Query::create()
                ->select('u.email, u.password')
                ->from('users u')
                ->where('u.email = ? and u.password = ?',
array($request->getParameter('email'), $request->getParameter('password')))
                ->fetchArray();

                if($loginSuccess)
                {
                    $this->getUser()->setFlash('success', 'You have logged
in successfully.');
                    $this->redirect('users/index');
                }
                else
                {
                    $this->getUser()->setFlash('notice', 'Error while
logging in.');
                }
            }
        }
    }

}

The code in the form class is as follows:

class UsersLoginForm extends sfForm
{

    public function configure()
    {
        $this->setWidgets(array(
            'email' => new sfWidgetFormInput(),
            'password' => new sfWidgetFormInputPassword()
        ));

        $this->widgetSchema->setNameFormat('login[%s]');

        $this->setValidators(array(
            'email' => new sfValidatorEmail(array(
                'required' => true
            ), array(
                'required' => 'Please enter your email'
            )),
            'password' => new sfValidatorString(array(
                'min_length' => 2
            ), array(
                'min_length' => 'Minimum password length should be 2 chars
long.'
            ))
        ));

        $this->widgetSchema->setNameFormat('users[%s]');
        $this->errorSchema = new
sfValidatorErrorSchema($this->validatorSchema);
        parent::setup();
    }

}

and here is the loginSuccess.php file

<h2>Enter your email and password to login</h2>

<form action="<?php echo url_for('users/login') ?>" method="post">
    <?php
    echo $form;
    ?>
    <input type="submit" value="Save" id="btnSubmit" />
</form>

 When I submit the form nothing happens at all. Please help me correct this.

Thanks.
WebRep
Overall rating


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Manse  
View profile  
 More options Jun 23 2011, 7:25 am
From: Manse <mans...@gmail.com>
Date: Thu, 23 Jun 2011 04:25:56 -0700 (PDT)
Local: Thurs, Jun 23 2011 7:25 am
Subject: Re: How can I create a user login mechanism in symfony 1.4
Why not just use the sfDoctrineGuardUser plugin ... works a treat ...

http://www.symfony-project.org/plugins/sfDoctrineGuardPlugin

Paul

On Jun 23, 11:51 am, Gaurav Sharma <gaurav.v.sha...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Justen Doherty  
View profile  
 More options Jun 23 2011, 7:55 am
From: Justen Doherty <phpc...@gmail.com>
Date: Thu, 23 Jun 2011 12:55:35 +0100
Local: Thurs, Jun 23 2011 7:55 am
Subject: Re: [symfony1] Re: How can I create a user login mechanism in symfony 1.4

i've used the Login form to do the authentication/authorisation logic, if
the form doesnt pass validation - the user isnt logged in..

    public function configure()
    {
       ....

        // call the doLogin function...
        $this->validatorSchema->setPostValidator(new
sfValidatorCallback(array(
          'callback' => array($this, 'doLogin'),
        )));
    }

    // authentication
      public function doLogin(sfValidatorBase $validator, array $values)
    {
        if($values['email']!='') {
            // get the user
            $u = Doctrine_Core::getTable('User')
             ->createQuery('u')
             ->where('u.email = ?', $values['email'])
             ->limit(1);

            $dbUser = $u->execute()->getFirst();

            if(!$dbUser) {

                // invalid username - generic message
                $error = new sfValidatorError($validator, 'Error with
login'); // dont give too much info away
                // throw an error bound to the password field'
                throw new sfValidatorErrorSchema($validator, array('email'
=> $error));

            } else {

                $submittedPassword = $values['password'];
                $userObj = new User();
                $hashedPassword =
$userObj->hashPassword($submittedPassword);

                $found = false;

                if($hashedPassword == $dbUser->getPassword()) {

                    $found = true;
                }

                if(!$found) {
                    // invalid username - generic message
                    $error = new sfValidatorError($validator, 'Error with
login'); // dont give too much info away
                    // throw an error bound to the password field'
                    throw new sfValidatorErrorSchema($validator,
array('password' => $error));
                }
            }
        }
    }

then in my action i do the following:

  public function executeLoginRegister(sfWebRequest $request)
  {

    $this->form = new LoginForm();

    // posted the form?
    if ($request->isMethod('post') && $request->getParameter('user'))
    {
      // set the form values and validate the form
      $this->form->bind($request->getParameter('user'));

      if ($this->form->isValid())
      {
        // user is authenticated
      } else {
        // user failed authentication
      }
   }

hope that helps!

--
-----------------------------------------------------------------
http://www.linkedin.com/in/justendoherty - LinkedIn
http://www.twitter.com/phpchap - Twitter
http://www.anotherwebdeveloper.com - Portfolio

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaurav Sharma  
View profile  
 More options Jun 23 2011, 9:02 am
From: Gaurav Sharma <gaurav.v.sha...@gmail.com>
Date: Thu, 23 Jun 2011 06:02:58 -0700 (PDT)
Local: Thurs, Jun 23 2011 9:02 am
Subject: Re: How can I create a user login mechanism in symfony 1.4
:)
thanks for your help. I appreciate it.

But I did it like this...

in the actions class 'login' function

public function executeLogin(sfWebRequest $request)
    {
        $this->form = new UsersLoginForm();
        if($request->isMethod('post'))
        {
            $this->form->bind($request->getParameter($this->form-

>getName()));

            if($this->form->isValid())
            {
                $userData = $this->form->getValues();
                $loginSuccess = Doctrine_Query::create()
                ->select('u.email, u.password')
                ->from('users u')
                ->where('u.email = ? and u.password = ?',
array($userData['email'], $userData['password']))
                ->fetchArray();

                if($loginSuccess)
                {
                    $this->getUser()->setAuthenticated(true);
                    $this->getUser()->addCredential('user');
                    $this->getUser()->setFlash('success', 'You have
logged in successfully.');
                    $this->redirect('users/index');
                }
                else
                {
                    $this->getUser()->setFlash('notice', 'Invalid
email or password. Please try again.');
                }
            }
        }
    }

and the above worked properly..

On Jun 23, 4:55 pm, Justen Doherty <phpc...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gábor Fási  
View profile  
 More options Jun 23 2011, 9:12 am
From: Gábor Fási <maerl...@gmail.com>
Date: Thu, 23 Jun 2011 15:12:47 +0200
Local: Thurs, Jun 23 2011 9:12 am
Subject: Re: [symfony1] Re: How can I create a user login mechanism in symfony 1.4
Your code says that you store passwords in plaintext. That's a very bad idea.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gaurav Sharma  
View profile  
 More options Jun 24 2011, 1:55 am
From: Gaurav Sharma <gaurav.v.sha...@gmail.com>
Date: Thu, 23 Jun 2011 22:55:37 -0700 (PDT)
Local: Fri, Jun 24 2011 1:55 am
Subject: Re: How can I create a user login mechanism in symfony 1.4
Yes I do, but it is only for now (as I have just started learning
symfony, I will learn that too, the symfony way).

Can you help me with this?
i.e.

What method or standard shall I use to handle password storage in
symfony?

Is it same like encrypting it with sha1() and then checking the whole
string.... sort of logic..?

Thanks

On Jun 23, 6:12 pm, Gábor Fási <maerl...@gmail.com> wrote:

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Re : How can I create a user login mechanism in symfony 1.4" by Serge HARDY
Serge HARDY  
View profile  
 More options Jun 24 2011, 4:07 am
From: Serge HARDY <serge.ha...@gmail.com>
Date: Fri, 24 Jun 2011 01:07:39 -0700 (PDT)
Local: Fri, Jun 24 2011 4:07 am
Subject: Re : How can I create a user login mechanism in symfony 1.4

you shouldn't call   parent::setup(); in configure()


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "How can I create a user login mechanism in symfony 1.4" by Gábor Fási
Gábor Fási  
View profile  
 More options Jun 25 2011, 9:34 am
From: Gábor Fási <maerl...@gmail.com>
Date: Sat, 25 Jun 2011 15:34:51 +0200
Local: Sat, Jun 25 2011 9:34 am
Subject: Re: [symfony1] Re: How can I create a user login mechanism in symfony 1.4
You already got two very good tips: one was a hint to use
sfDoctrineGuard, the other was a form class.

Justen Doherty sent the class, I recommend checking it, it's pretty
much what I'm using it a lot of projects - delegating login validation
to the form class, so the in action it's a simple `$form->isValid()`.

Manse sent the hint for the sfDoctrineGuardPlugin[1], I cannot
recommend it enough, it's the de-facto standard for user
authentication/authorization in the symfony 1 world. It's awesomely
written, so even if you do not end up using it, reading the code is
highly recommended.

[1]: http://www.symfony-project.org/plugins/sfDoctrineGuardPlugin

...

read more »


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »