Hello,

I'm implementing the Advanced Validation Tutorial from the wiki and am
running into the problem of invalidating a field in the model because I
cannot get access to the necessary variables (that only exist in the
controller/view).

I'm building a login page.  My workflow is such that a user's system
access is verified via database (where no passwords reside, so
passwords are not in the model), then the user is authenticated via
LDAP (with username and password).

I can't figure out the best place to perform my LDAP authentication.
In my controller I have access to the entered password; however, I'm
not sure how to create a custom error in the controller, since the
tutorial only shows error creation in the model (see isUserRegistered()
below).  On the other hand, in the model I can't get access to the
password that I need to verify via LDAP.  Kind of a catch 22.

Is there a way to get controller variables into the model that are not
in the table/model?  Or is there a way to invalidate a field and create
a custom error message from the controller?  Could I somehow use
beforeValidate() to pass the password and validate/create errors as
needed?  Thanks!

-----------------------------------------------------------------------------------------------------------------

class User extends AppModel
{
        var $name = 'User';
        var $validate = array (
                'username' => array (
                        'required' => array ( VALID_NOT_EMPTY, "Username is 
required" ),
                        'unregistered' => array (
                                'isUserRegistered', 'User is not registered' )
                ),
                'password' => array (
                        'required' => array ( VALID_NOT_EMPTY, 'Password is 
required' )
                )
        );

        var $primaryKey = 'id';
        var $useTable   = 'user';

        function isUserRegistered ()
        {
                return ( $this->hasAny (
                        array ( 'User.username' => 
$this->data[$this->name]['username'] ) )
);
        }
}

-----------------------------------------------------------------------------------------------------------------

class UserController extends AppController
{
        var $helpers = array( 'Error', 'Html', 'Javascript' );

        var $name = 'User';
        var $uses = array ( 'User' );

        function login ()
        {
                // Page was loaded for the first time
                if ( empty ( $this -> params['data'] ) )
                {
                        $this -> render();
                }
                // It's a submit
                else
                {
                        $this->set ( 'data', $this->params['data'] );

                        // Get parameters from the form
                        $username = ( $this -> data['User']['username'] );
                        $password = ( $this -> data['User']['password'] );

                        if ( $this -> User -> validates ( $this -> data ) )
                        {
                                $ldap = new LdapUser();
                                if ( $ldap -> auth ( $username, $password ) )
                                {
                                        $this -> Session -> write ( 
'valid_user', 1 );
                                        $this -> redirect ( '/' );
                                }
                                else
                                {
                                        // **Unable to authenticate - create 
custom msg**
                                }
                        }
                        else
                        {
                                // **Unable to validate**
                                $this->validateErrors( $this -> User );
                                $this->render();
                        }
                }
        }

        function logout ()
        {
                $this -> Session -> write ( 'valid_user', 0 );
                $this -> redirect ( '/User/login' );
        }
}

-----------------------------------------------------------------------------------------------------------------

In the view:
<?php echo $html -> password ( 'User/password', array ( 'size' => '40'
) ); ?>
<?php echo $error -> showMessage ( 'User/password' ); ?>


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to