Welcome Guest, Not a member yet? Register   Sign In
variables doesn't get saved
#1

[eluser]TomMRiddle[/eluser]
I use datamapper and at some point we had this working but it have stopped working. only fields that gets saved is the ones whose values have been set by the controller.

the salt, encrypted_email and encrypted_password doesn't get saved.
User Model:
Code:
<?php
class User extends DataMapper {
    
    //var $has_many = array('mymovie');
    //var $has_one = array('subscribed');
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
    //private $encrypted_password;
    var $validation = array(
        'name' => array(
            'label' => 'name',
            'rules' => array('unique', 'required', 'trim', 'alpha_dash', 'min_length' => 5, 'max_length' => 20),
        ),
        'password' => array(
            'label' => 'Password',
            'rules' => array('required', 'min_length' => 6, '_encrypt_password'),
        ),
        'confirm_password' => array(
            'label' => 'Confirm Password',
            'rules' => array('required', 'matches' => 'password'),
        ),
        'email' => array(
            'label' => 'Email Address',
            'rules' => array('required', 'trim', 'unique', 'valid_email', '_encrypt_email')
        )
    );
    
    function login()
    {
        // Create a temporary user object
        $u = new User();

        // Get this users stored record via their username
        $u->where('email', $this->email)->get();

        // Give this user their stored salt
        $this->salt = $u->salt;

        // Validate and get this user by their property values,
        // this will see the 'encrypt' validation run, encrypting the password with the salt
        $this->validate()->get();

        // If the username and encrypted password matched a record in the database,
        // this user object would be fully populated, complete with their ID.

        // If there was no matching record, this user would be completely cleared so their id would be empty.
        if (empty($this->id))
        {
            // Login failed, so set a custom error message
            $this->error_message('login', 'name or password invalid');

            return FALSE;
        }else if(!$this->confirmed)
        {
            $this->error_message('login', 'email adresse not confirmed');

            return FALSE;
        }
        else
        {
            // Login succeeded
            return TRUE;
        }
    }
    
    // Validation prepping function to encrypt passwords
    // If you look at the $validation array, you will see the password field will use this function
    private function _encrypt($field)
    {
        // Don't encrypt an empty string
        if (!empty($this->{$field}))
        {
            // Generate a random salt if empty
            if (empty($this->salt))
            {
                $this->salt = md5(uniqid(rand(), true));
            }

            return sha1($this->salt . $this->{$field});
        }
    }

    private function _encrypt_email($field)
    {
    $this->encrypted_email = _encrypt($field);
    }

    private function _encrypt_password($field)
    {
    $this->encrypted_password = _encrypt($field);
    }
}

/* End of file user.php */
/* Location: ./application/models/user.php */
?>

Controller:
Code:
<?php

class Users extends Controller {

    function __construct()
    {
    parent::__construct();
    }

    function index()
    {
    // Let's create a user
    $u = new User();
    $u->name = 'Fred_Smith';
    $u->password = 'apples';
    $u->confirm_password = 'apples';
    $u->email = '[email protected]';
    
    // And save them to the database (validation rules will run)
    if ($u->save())
    {
        // User object now has an ID
        echo 'ID: ' . $u->id . '<br />';
        echo 'name: ' . $u->name . '<br />';
        echo 'Email: ' . $u->email . '<br />';

        // Not that we'd normally show the password, but when we do, you'll see it has been automatically encrypted
        // since the User model is setup with an encrypt rule in the $validation array for the password field
        echo 'Password: ' . $u->password . '<br />';
        echo 'ePassword: ' . $u->encrypted_password . '<br />';
        echo 'Salt: ' . $u->salt . '<br />';
    }
    else
    {
        // loop through the error's all list
        foreach ($u->error->all as $error)
        {
        echo $error;
        }
    }


    // You can delete multiple relations of different types in the same way you can save them
    // Now that we're done with the user, let's delete him
    //$u->delete();

    // When you delete the user, you delete all his relations with other objects.  DataMapper does all the tidying up for you :)
    }
}

/* End of file users.php */
/* Location: ./application/controllers/users.php */
?&gt;
#2

[eluser]WanWizard[/eluser]
_encrypt() is a class method, so
Code:
private function _encrypt_email($field)
    {
    $this->encrypted_email = _encrypt($field);
    }

    private function _encrypt_password($field)
    {
    $this->encrypted_password = _encrypt($field);
    }

should be
Code:
private function _encrypt_email($field)
    {
    $this->encrypted_email = $this->_encrypt($field);
    }

    private function _encrypt_password($field)
    {
    $this->encrypted_password = $this->_encrypt($field);
    }
#3

[eluser]Unknown[/eluser]
I'm having a similar problem, although my code is closer to what is in the DataMapper documentation. I'm not encrypting the email address, only the password. The encrypted password is saved correctly, but the salt is never saved. When the user is created, I can see that the salt is in the User object. However, after I run save(), then lookup the newly saved user, I can see that the salt is not saved. I only have the local encrypt method in the validation rules. I'm stumped as to why this isn't working.

Controller:
Code:
&lt;?php
class Create_User extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        // Your own constructor code
    }
    
    function index()
    {
        // Let's create a user
        $u = new User();
        $u->username = 'testuser';
        $u->password = 'testpw';
        $u->email = '[email protected]';

        // And save them to the database (validation rules will run)
        if ($u->save())
        {
            // User object now has an ID
            echo 'ID: ' . $u->id . '<br />';
            echo 'Username: ' . $u->username . '<br />';
            echo 'Email: ' . $u->email . '<br />';
            echo 'Salt: ' . $u->salt . '<br />';

            // Not that we'd normally show the password, but when we do, you'll see it has been automatically encrypted
            // since the User model is setup with an encrypt rule in the $validation array for the password field
            echo 'Password: ' . $u->password . '<br />';
        }
        else
        {
            // If validation fails, we can show the error for each property
            echo $u->error->username;
            echo $u->error->password;
            echo $u->error->email;

            // or we can loop through the error's all list
            foreach ($u->error->all as $error)
            {
                echo $error;
            }

            // or we can just show all errors in one string!
            echo $u->error->string;

            // Each individual error is automatically wrapped with an error_prefix and error_suffix, which you can change (default: <p>error message</p>)
        }

        // This lookup will show that no salt was stored
        $nu = new User();
        $nu->where('username', 'testuser')->get();
        echo 'ID: ' . $nu->id . '<br />';
        echo 'Username: ' . $nu->username . '<br />';
        echo 'Email: ' . $nu->email . '<br />';
        echo 'Salt: ' . $nu->salt . '<br />';
    
    }
        
}

Model:
Code:
&lt;?php
class User extends DataMapper {

    var $validation = array(
        'username' => array(
            'field' => 'username',
            'label' => 'Username',
            'rules' => array('required')
        ),
        'password' => array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => array('required', 'trim', 'min_length' => 6, '_encrypt')
        ),
        'email' => array(
            'field' => 'email',
            'label' => 'Email Address',
            'rules' => array('required', 'trim', 'unique', 'valid_email')
        )
    );


    /**
     * Constructor: calls parent constructor
     */
    function __construct($id = NULL)
    {
        parent::__construct($id);
    }

    function login()
    {
        // Create a temporary user object
        $u = new User();

        // Get this users stored record via their username
        $u->where('username', $this->username)->get();

        // Give this user their stored salt
        $this->salt = $u->salt;

        // Validate and get this user by their property values,
        // this will see the 'encrypt' validation run, encrypting the password with the salt
        $this->validate()->get();

        // If the username and encrypted password matched a record in the database,
        // this user object would be fully populated, complete with their ID.

        // If there was no matching record, this user would be completely cleared so their id would be empty.
        if (empty($this->id))
        {
            // Login failed, so set a custom error message
            $this->error_message('login', 'Username or password invalid');

            return FALSE;
        }
        else
        {
            // Login succeeded
            return TRUE;
        }
    }

    // Validation prepping function to encrypt passwords
    // If you look at the $validation array, you will see the password field will use this function
    function _encrypt($field)
    {
        // Don't encrypt an empty string
        if (!empty($this->{$field}))
        {
            // Generate a random salt if empty
            if (empty($this->salt))
            {
                $this->salt = md5(uniqid(rand(), true));
            }

            $this->{$field} = sha1($this->salt . $this->{$field});
            //$this->{$field} = md5($this->{$field}, true);
        }
    }


}




Theme © iAndrew 2016 - Forum software by © MyBB