⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

managing ion auth tables



imag

imag
  • profile picture
  • Member

Posted 20 August 2012 - 08:58 AM

hi there,

I use ion auth2 and want to manage my users with grocerycrud.
I want to implement a function where I can change also the passwords ... So I think I need extra formields (password, password_confirm) on add and edit forms ...

anybody of you ever did something similar?

saulimus

saulimus
  • profile picture
  • Member

Posted 20 August 2012 - 14:07 PM

I did exactly that with ionauth:
-a checkbox titled "Change password?"
-two password fields
-validation rules for password length etc.
-callback before update to check if:
-the checkbox is selected
-the passwords are the same
etc.
So if I can do it you can too. :)

imag

imag
  • profile picture
  • Member

Posted 20 August 2012 - 14:32 PM

I found out that I can return own formfields with callback (callback_edit_field) ... there I add the "change pwd?" checkbox and the 2 pwd fields ... is this theoreticly your way?

saulimus

saulimus
  • profile picture
  • Member

Posted 22 August 2012 - 09:16 AM

Yes, that's how I did it... :)
Here's some of the code, hope it helps in finding your solution:
http://codepad.org/7O6vEY6h

I also added some jQuery to the page to disable the password fields until the checkbox is checked, just to make it look nicer:
http://codepad.org/0mWiA19N

imag

imag
  • profile picture
  • Member

Posted 23 August 2012 - 07:20 AM

thx saulimus!!!

couzcoco

couzcoco
  • profile picture
  • Member

Posted 25 September 2012 - 06:03 AM

Hi There,

I am quite new to Grocery Crud and Code Igniter and this post was really usefull for me.
But I have two problems with the solution provided by saulimus (thanks a lot by the way :)) :

- when I submit the form pushing the button "Update Changes", The validation does not appear
- when I submit the form pushing the button "Update and go back to list", it stays on the form.

I tried to add a callback_after_update with a redirect inside but It doesn't work.

Do you know the solution ?

Thanks in advance for the response.

saulimus

saulimus
  • profile picture
  • Member

Posted 25 September 2012 - 06:17 AM

When editing or adding?... can you post your code?...

couzcoco

couzcoco
  • profile picture
  • Member

Posted 25 September 2012 - 06:27 AM

When editing, i didn't try the add function for the moment.
This is quite the same code as the one you posted above

[font=courier new,courier,monospace] public function users()
{
if (!$this->ion_auth->logged_in())
{
//redirect them to the login page
redirect('auth/login', 'refresh');
}
else
{
$crud = new grocery_CRUD();

$crud->set_subject('utilisateurs');
$crud->set_table('users');


$crud
->columns('first_name', 'last_name', 'email', 'phone', 'company', 'groups', 'active')
->fields('first_name', 'last_name', 'email', 'phone', 'company', 'groups', 'active', 'change_password', 'edit_password', 'edit_password_confirm')
->display_as('change_password', 'Changer le mot de passe ?')
->display_as('first_name', 'Prénom')
->display_as('last_name', 'Nom')
->display_as('email', 'Email')
->display_as('phone', 'Téléphone')
->display_as('company', 'Entreprise')
->display_as('groups', 'Groupes')
->display_as('active', 'Actif')
->change_field_type('change_password', 'true_false')
->change_field_type('edit_password', 'password')
->change_field_type('edit_password_confirm', 'password')
->set_rules('change_password', 'Change password', 'callback__change_password_check')
->set_rules('edit_password', 'Password', 'min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']')
->callback_edit_field('change_password',array($this,'_cb_change_password_field'))
->callback_edit_field('edit_password', array($this, '_cb_empty_password_input_field'))
->callback_edit_field('edit_password_confirm', array($this,'_cb_empty_password_confirm_input_field'))
->callback_before_update(array($this, '_cb_user_password_update'))
->callback_after_update(array($this, '_cb_user_redirect'));

$output = $crud->render();

$this->_example_output($output);
}
}

function _change_password_check($change_password)
{
$error = false;
if($change_password == 1)
{
$password = $this->input->post('edit_password');
$password_confirm = $this->input->post('edit_password_confirm');
$message = $this->lang->line('required');

if(empty($password))
{
$message = sprintf($message,'Password');
$error = TRUE;
}
if(empty($password_confirm))
{
$message = sprintf($message,'Confirm password');
$error = TRUE;
}
if($password != $password_confirm)
{
$message = $this->lang->line('matches');
$message = sprintf($message, 'Password', 'Confirm Password');
$error = TRUE;
}
}
if($error == TRUE)
{
$this->form_validation->set_message('_change_password_check', $message);
return false;
}
else
{
return TRUE;
}
}

function _cb_empty_password_input_field($value, $primary_key)
{
$data = array(
'name' => 'edit_password',
'type' => 'password',
'id' => 'field-edit_password'
);
return form_input($data);
}

function _cb_empty_password_confirm_input_field($value, $primary_key)
{
$data = array(
'name' => 'edit_password_confirm',
'id' => 'field-edit_password_confirm',
'type' => 'password'
);
return form_input($data);
}

function _cb_user_password_update($post_array, $primary_key)
{
// let's not submit the check box
unset($post_array['change_password']);
$data = array(
'password' => $post_array['edit_password']
);
// let's not submit the password fields
unset($post_array['edit_password']);
unset($post_array['edit_password_confirm']);

$this->ion_auth->update($primary_key, $data);

return $post_array;
}

function _cb_user_redirect($post_array, $primary_key)
{
redirect('main', 'refresh');
}

function _cb_change_password_field($value, $primary_key)
{
$data = array(
'id' => 'field_change_password',
'name' => 'change_password'
);
$str = form_checkbox($data, 0);
return $str;
}[/font]

saulimus

saulimus
  • profile picture
  • Member

Posted 25 September 2012 - 06:57 AM

You're missing this relation:

$crud->set_relation_n_n('groups', 'users_groups', 'groups','user_id','group_id','description');

Without this line, it's trying to insert the 'groups' field into your 'users' table instead of 'users_groups'...
EDIT: Also, you don't need callback_after_update when it's working properly... so don't forget to remove it.

couzcoco

couzcoco
  • profile picture
  • Member

Posted 25 September 2012 - 07:14 AM

Thanks a lot, it really helped me !!! Now it fits all the needs.

couzcoco

couzcoco
  • profile picture
  • Member

Posted 25 September 2012 - 12:42 PM

Now that I'm testing the Add functionality, I've got two notices


[b] A PHP Error was encountered[/b]

Severity: Notice
Message: Undefined property: stdClass::$default
Filename: libraries/Grocery_CRUD.php
Line Number: 1865

[b] A PHP Error was encountered[/b]

Severity: Notice
Message: Undefined property: stdClass::$default
Filename: libraries/Grocery_CRUD.php
Line Number: 1868


Do you know where it comes from ?

saulimus

saulimus
  • profile picture
  • Member

Posted 25 September 2012 - 12:45 PM

I found a bug! :(
The password update callback didn't care about the unchecked checkbox at all.. and so would reset the password...
The following code should work, just tested it...


function _cb_user_password_update($post_array, $primary_key)
{
// because change_password is checked we know that the validation has run
if($post_array['change_password'] == 1)
{
$data = array(
'password' => $post_array['edit_password']
);
$this->ion_auth->update($primary_key, $data);
}
unset($post_array['change_password']);
unset($post_array['edit_password']);
unset($post_array['edit_password_confirm']);
return $post_array;
}

UPDATE: Forgot "return $post_array";

saulimus

saulimus
  • profile picture
  • Member

Posted 25 September 2012 - 12:49 PM

What I can see at first is that you need to separate the add and edit fields like this:

$crud
->add_fields('username', 'first_name', 'last_name', 'groups', 'password', 'password_confirm')
->edit_fields('username', 'first_name', 'last_name', 'groups', 'active', 'change_password', 'edit_password', 'edit_password_confirm')

couzcoco

couzcoco
  • profile picture
  • Member

Posted 25 September 2012 - 12:50 PM

[quote name='saulimus' timestamp='1348577125' post='3612']
I found a bug! :(
The password update callback didn't care about the unchecked checkbox at all.. and so would reset the password...
The following code should work, just tested it...


function _cb_user_password_update($post_array, $primary_key)
{
// because change_password is checked we know that the validation has run
if($post_array['change_password'] == 1)
{
$data = array(
'password' => $post_array['edit_password']
);
$this->ion_auth->update($primary_key, $data);
}
unset($post_array['change_password']);
unset($post_array['edit_password']);
unset($post_array['edit_password_confirm']);
}

[/quote]

Is the return missing at the end ?

return $post_array;

couzcoco

couzcoco
  • profile picture
  • Member

Posted 26 September 2012 - 12:58 PM

If you want to add user this could be usefull :


function _cb_user_password_insert($post_array, $primary_key)
{
// because change_password is checked we know that the validation has run
$data = array(
'password' => $post_array['password']
);

// Here we can't use $this->ion_auth->update($primary_key, $data); because the primary key is not known
// We can't use $this->ion_auth->register because it creates a record and gc tries to create one too !!!
$post_array['salt'] = $this->config->item('store_salt', 'ion_auth') ? $this->ion_auth->salt() : FALSE;
$post_array['password'] = $this->ion_auth->hash_password($post_array['password'], $salt);

unset($post_array['password_confirm']);

return $post_array;
}


I had to put it in the callback_before_insert of the $crud

stavgian

stavgian
  • profile picture
  • Member

Posted 01 October 2012 - 18:02 PM

It's easier if you use ion_auth library itself.
I have used

$crud->callback_insert(array($this, 'create_user_callback'));
$crud->callback_update(array($this, 'edit_user_callback'));
$crud->callback_delete(array($this, 'delete_user'));



[php]function delete_user($primary_key) {

if ($this->ion_auth_model->delete_user($primary_key)) {
return true;
} else {
return false;
}
}

function edit_user_callback($post_array, $primary_key = null) {

$username = $post_array['first_name'] . ' ' . $post_array['last_name'];
$email = $post_array['email'];
$data = array(
'username' => $username,
'email' => $email,
'phone' => $post_array['phone'],
'first_name' => $post_array['first_name'],
'last_name' => $post_array['last_name']
);

$this->ion_auth_model->update($primary_key, $data);

return true;
}

function create_user_callback($post_array, $primary_key = null) {

$username = $post_array['first_name'] . ' ' . $post_array['last_name'];
$password = $post_array['password'];
$email = $post_array['email'];
$data = array(
'phone' => $post_array['phone'],
'first_name' => $post_array['first_name'],
'last_name' => $post_array['last_name']
);

$this->ion_auth_model->register($username, $password, $email, $data);

return $this->db->insert_id();
}[/php]

couzcoco

couzcoco
  • profile picture
  • Member

Posted 02 October 2012 - 04:32 AM

Thanks a lot !!! That's what I was looking for ;)

Djoudi

Djoudi
  • profile picture
  • Member

Posted 02 October 2012 - 07:27 AM

can you put the best code for ion_auth with grocerycrud here ? :)

couzcoco

couzcoco
  • profile picture
  • Member

Posted 29 October 2012 - 06:29 AM

I've found a problem with your Stavgian's solution.
In my case, I have set_relation_n_n('groups', 'users_groups', 'groups','user_id','group_id','description').
It does not work anymore.
How can I reproduce it now ?

DREON

DREON
  • profile picture
  • Member

Posted 22 February 2013 - 02:38 AM

@couzcoco sir i use ur code for insert here :

function _cb_user_password_insert($post_array, $primary_key)
{
     // because change_password is checked we know that the validation has run
     $data = array(
             'password' => $post_array['password']
     );

     // Here we can't use $this->ion_auth->update($primary_key, $data); because the primary key is not known
     // We can't use $this->ion_auth->register because it creates a record and gc tries to create one too !!!
     $post_array['salt']         = $this->config->item('store_salt', 'ion_auth') ? $this->ion_auth->salt() : FALSE;
     $post_array['password']     = $this->ion_auth->hash_password($post_array['password'], $salt);

     unset($post_array['password_confirm']);

     return $post_array;
}

 

but when i click add nothing happened.. thnx