[eluser]TerryT[/eluser]
Has anyone implemented editing a user with Ion Auth? I see the sample view to do this and the method in the model, but don't see anything in the controller. Am I missing something? Thanks.
Sorry this is posted in the wrong section. My mistake and I don't have the authorization to delete or change it.
Terry
[eluser]Jose Forte[/eluser]
I made something to Edit a user with Ion Auth
It's simple but you can adapt it for yourself.
First of all, I added this to the (controllers/auth) controller
Code: function edit_user()
{
$this->data['title'] = "Edit";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
redirect('auth', 'refresh');
}
if ($this->ion_auth->logged_in() && $this->ion_auth->is_admin())
{
//validate form input
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('email', 'Email Address', 'required|valid_email');
$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');
$this->form_validation->set_rules('phone1', 'First Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
$email = $this->input->post('email');
$additional_data = array('first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
);
$id = $this->input->post('id');
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $username,
'email' => $email,
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3')
);
$this->ion_auth->update_user($id, $data);
//display the create user form
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->data['first_name'] = array('name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name'),
);
$this->data['last_name'] = array('name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name'),
);
$this->data['email'] = array('name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['phone1'] = array('name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1'),
);
$this->data['phone2'] = array('name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2'),
);
$this->data['phone3'] = array('name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3'),
);
$this->data['id'] = array('name' => 'id',
'id' => 'id',
'type' => 'text',
'value' => $this->form_validation->set_value('id'),
);
$this->load->view('auth/edit_user', $this->data);
}
}
[eluser]Jose Forte[/eluser]
Then I made this view (views/auth/edit_user)
Code: <?php $this->load->view('auth/header'); ?>
<div class='mainInfo'>
<h1>Editar Usuario</h1>
<p>Ingresa la informaciĆ³n del usuario que quieras cambiar o no apliques cambios en los campos.</p>
<div id="infoMessage"><?php echo $message;?></div>
<?php
// Estas 2 consultas las hago para llenar los campos del formulario por si no quiero cambiarlos y asi proveo de la info al usuario
$this->db->select('email');
$this->db->where('id', $this->uri->segment(3));
$q1 = $this->db->get('users');
foreach ($q1->result() as $row)
{
$email = $row->email;
}
$this->db->select('*');
$this->db->where('id', $this->uri->segment(3));
$q2 = $this->db->get('meta');
foreach ($q2->result() as $row)
{
$fname = $row->first_name;
$lname = $row->last_name;
$cname = $row->company;
$phone = $row->phone;
}
$phone1 = substr($phone, 0,3);
$phone2 = substr($phone, 4, 3);
$phone3 = substr($phone, 8);
?>
<?php echo form_open("auth/edit_user/".$this->uri->segment(3));?>
<p>First Name:<br />
<?php echo form_input('first_name',$fname);?>
</p>
<p>Last Name:<br />
<?php echo form_input('last_name',$lname);?>
</p>
<p>Company Name:<br />
<?php echo form_input('company',$cname);?>
</p>
<p>Email:<br />
<?php echo form_input('email',$email);?>
</p>
<p>Phone:<br />
<?php echo form_input('phone1',$phone1);?>-<?php echo form_input('phone2',$phone2);?>-<?php echo form_input('phone3',$phone3);?>
</p>
<!-- <p>
<input type=checkbox name="reset_password"> <label for="reset_password">Reset Password</label>
</p> -->
<?php echo form_hidden('id',$this->uri->segment(3)); ?>
<?php echo form_submit('submit', 'Actualizar');?>
<?php echo form_close();?>
<p><a href="<?php echo site_url('auth');?>"><u>Go Back</u></a></p>
</div>
[eluser]Jose Forte[/eluser]
And finally I call it from this file (views/auth/index), adding this code:
Code: <table cellpadding=0 cellspacing=10>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Group</th>
<th>Status</th>
<th>Edit</th>
</tr>
<?php foreach ($users as $user):?>
<tr>
<td><?php echo $user['first_name']?></td>
<td><?php echo $user['last_name']?></td>
<td><?php echo $user['email'];?></td>
<td><?php echo $user['group_description'];?></td>
<td><?php echo ($user['active']) ? anchor("auth/deactivate/".$user['id'], 'Active') : anchor("auth/activate/". $user['id'], 'Inactive');?></td>
<td><?php echo (anchor("auth/edit_user/".$user['id'], 'Edit'));?></td>
</tr>
<?php endforeach;?>
</table>
[eluser]iMefisto[/eluser]
Thanks. Very useful.
Did not you forget :
Code: if ($this->form_validation->run() == true)
into controller ? (similar to create_user)
[eluser]rip_pit[/eluser]
Yeah thanx a lot! it inspired me, i used it and tweaked it to make my own :
i modified a bit the controller using the existing "create_user" as source:
- using ->run() as noticed before,
- access by logged user only
- simplifed view code.
- Still Disabled/missing: the reset_password part
1. controllers/auth.php
Code: //edit a user
//logged in user
function edit_user() {
if ( ! $this->ion_auth->logged_in() )
{
redirect('auth', 'refresh');
}
//validate form input
$this->form_validation->set_rules('first_name', 'First Name', 'required|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('phone1', 'Phone part 1', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone2', 'Phone part 2', 'required|xss_clean|min_length[3]|max_length[3]');
$this->form_validation->set_rules('phone3', 'Phone part 3', 'required|xss_clean|min_length[4]|max_length[4]');
$this->form_validation->set_rules('company', 'Company', 'required|xss_clean');
if ($this->form_validation->run() == true)
{
$id = (int)$this->input->post('id');
$data = array('first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name')),
'email' => $this->input->post('email'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone1') . '-' . $this->input->post('phone2') . '-' . $this->input->post('phone3'),
);
}
if ($this->form_validation->run() == true && $this->ion_auth->update_user($id, $data) )
{ //check to see if we are editing the user
//redirect them back to the admin page
//EXECUTE THE RESET PASSWORD HERE IF CHECKED
$this->session->set_flashdata('ion_message', "User edited");
redirect('auth/index', 'refresh');
}
else
{ //display the edit user form
//set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('ion_message')));
//get posted ID if exists, else the one from uri.
//in order to get user datas from table
$id = (isset($id)) ? $id : (int)$this->uri->segment(3);
//get current user datas from table and set default form values
$user = $this->ion_auth->get_user($id);
//passing user id to view
$this->data['user_id'] = $user->id;
//get phone parts
$phoneparts=explode('-',$user->phone);
//prepare form
$this->data['first_name'] = array('name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name', $user->first_name),
);
$this->data['last_name'] = array('name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name', $user->last_name),
);
$this->data['email'] = array('name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email', $user->email),
);
$this->data['company'] = array('name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company', $user->company),
);
$this->data['phone1'] = array('name' => 'phone1',
'id' => 'phone1',
'type' => 'text',
'value' => $this->form_validation->set_value('phone1', $phoneparts[0]),
);
$this->data['phone2'] = array('name' => 'phone2',
'id' => 'phone2',
'type' => 'text',
'value' => $this->form_validation->set_value('phone2', $phoneparts[1]),
);
$this->data['phone3'] = array('name' => 'phone3',
'id' => 'phone3',
'type' => 'text',
'value' => $this->form_validation->set_value('phone3', $phoneparts[2]),
);
$this->data['id'] = array('name' => 'id',
'id' => 'id',
'type' => 'hidden',
'value' => $this->form_validation->set_value('id', $user->id),
);
$this->load->view('auth/edit_user', $this->data);
}
}
2. views/auth/edit_user.php
Code: <div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open("auth/edit_user/".$user_id); ?>
<p>First Name:<br />
<?php echo form_input($first_name);?>
</p>
<p>Last Name:<br />
<?php echo form_input($last_name);?>
</p>
<p>Company Name:<br />
<?php echo form_input($company);?>
</p>
<p>Email :<br />
<?php echo form_input($email);?>
</p>
<p>Phone :<br />
<?php echo form_input($phone1);?>-<?php echo form_input($phone2);?>-<?php echo form_input($phone3);?>
</p>
<!--<p>
<input type=checkbox name="reset_password"> <label for="reset_password">Reset password</label>
</p>
-->
<?php echo form_input($id);?>
<?php echo form_submit('submit', 'OK');?>
<?php echo form_close();?>
Finally, I used the exact same code given by Jose for the /views/auth/index
[eluser]Unknown[/eluser]
hi guys for some reason the code the you have provided will not work for the life of me. is this because I'm using the v.2.
the error I'm getting is:
An Error Was Encountered
Unable to load the requested file: auth/edit_user.php
I don't understand why help?
[eluser]rip_pit[/eluser]
there was a small error in my step#2 path.
the view file must be placed in the default views, auth folder.
it's to say : /view s/auth/edit_user.php
error has been fixed now.
Let me know if it works better.
PS: note the latest version of ion auth has several changes in it.
This "edit" script can still be used but i might need some small fixes.
[eluser]dkrape[/eluser]
Thanks for this! It worked well for me.
I did need to change this line slightly:
Code: if ($this->form_validation->run() == true && $this->ion_auth->update_user($id, $data) )
Instead of being called "update_user" the function is simply called "update".
|