| regarding date handling.
|
*/
-$config['time_reference'] = 'local';
+$config['time_reference'] = 'gmt';
/*
array(
'field'=>'username',
'label'=>'lang:user_username',
- 'rules'=>'trim|required|min_length[5]|max_length[32]'
- . '|strtolower|callback__valid_username'
+ 'rules'=>'trim|callback__required_by_register|min_length[5]|max_length[32]'
+ . '|strtolower|xss_clean|callback__valid_username'
),
array(
'field'=>'password',
'label'=>'lang:user_password',
- 'rules'=>'required'
+ 'rules'=>'callback__required_by_register|min_length[5]|max_length[32]'
),
array(
'field'=>'password-confirmation',
'label'=>'lang:user_password_confirmation',
- 'rules'=>'required'
+ 'rules'=>'callback__required_by_register|matches[password]'
+ ),
+ array(
+ 'field'=>'old-password',
+ 'label'=>'lang:user_old_password',
+ 'rules'=>'min_length[5]|max_length[32]|callback__valid_old_password[username]'
+ ),
+ array(
+ 'field'=>'new-password',
+ 'label'=>'lang:user_new_password',
+ 'rules'=>'min_length[5]|max_length[32]'
+ ),
+ array(
+ 'field'=>'new-password-confirmation',
+ 'label'=>'lang:user_new_password_confirmation',
+ 'rules'=>'callback__change_password_cond|matches[new-password]'
),
array(
'field'=>'email',
'label'=>'lang:user_email',
- 'rules'=>'required'
+ 'rules'=>'trim|required|xss_clean|valid_email'
),
array(
'field'=>'first-name',
'label'=>'lang:user_first_name',
- 'rules'=>'required'
+ 'rules'=>'trim|required|ucwords|xss_clean|prep_for_form'
),
array(
'field'=>'last-name',
'label'=>'lang:user_last_name',
- 'rules'=>'required'
+ 'rules'=>'trim|required|ucwords|xss_clean|prep_for_form'
),
array(
'field'=>'birth-date',
'label'=>'lang:user_birth_date',
- 'rules'=>''
+ 'rules'=>'trim|callback__valid_date'
),
array(
'field'=>'locality',
'label'=>'lang:user_locality',
- 'rules'=>''
+ 'rules'=>'trim|ucwords|xss_clean|prep_for_form'
)
)
);
*/
class User extends CI_Controller {
- private $username = NULL;
- private $email = NULL;
- private $user_id = NULL;
- private $ldap_user_info = NULL;
+ private $import = FALSE;
public function __construct()
{
parent::__construct();
$this->lang->load('user');
+ $this->load->model('users_model');
}
public function index()
public function login($redirect = '')
{
$this->load->library('form_validation');
- $this->load->model('users_model');
$this->form_validation->set_error_delimiters('<span class="error">',
'</span>');
// **
$this->load->view('html_begin', $this->html_head_params);
$this->load->view('header', array('selected_menu' => 'login'));
-
- $this->load->view('user/login_view', array(
- 'redirect'=> $redirect
- ));
+
+ $main_params['content'] = $this->load->view('user/login_view',
+ array('redirect'=> $redirect), TRUE);
+ $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
+ $this->load->view('main', $main_params);
$this->load->view('footer');
$this->load->view('html_end');
}
else
{
- if ($this->user_id !== NULL)
+ if (! $this->import)
{
- $this->session->set_userdata(array(
- 'user_id'=> $this->user_id,
- 'username'=> $this->username
- ));
-
// Redirect to last page before login.
header('Location: '. site_url(urldecode_segments($redirect)));
}
else
{
- $this->session->set_userdata(array(
- 'username'=> $this->username
- ));
-
- // Redirect to register page because an user authenticates here
- // for the first time with LDAP.
- // TODO
- header('Location: '. site_url(urldecode_segments($redirect)));
+ // Redirect to account page because an user authenticates here
+ // for the first time with external authentication. The page
+ // will display imported data.
+ header('Location: '. site_url('user/account'));
}
}
}
public function register($redirect = '')
{
$this->load->library('form_validation');
- $this->load->model('users_model');
$this->load->helper('localization');
$this->load->helper('date');
if ($this->form_validation->run('register') === FALSE)
{
+ // Edit account data if logged in, otherwise register.
+ if ($user_id = $this->session->userdata('user_id'))
+ {
+ $userdata = $this->users_model->get_userdata($user_id);
+ }
+ else
+ {
+ $userdata = FALSE;
+ }
+
$params = array('title' =>
$this->lang->line('ui_nav_menu_register')
.' – '
// **
$this->load->view('html_begin', $this->html_head_params);
$this->load->view('header', array('selected_menu' => 'register'));
-
- $this->load->view('user/register_view', array(
- 'redirect'=> $redirect
- ));
+
+ $main_params['content'] = $this->load->view('user/register_view',
+ array('userdata'=> $userdata, 'redirect'=> $redirect),
+ TRUE);
+ $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
+ $this->load->view('main', $main_params);
$this->load->view('footer');
$this->load->view('html_end');
}
else
{
- if ($this->user_id !== NULL)
+ $user_id = $this->input->post('user-id');
+ $data['email'] = $this->input->post('email');
+ $data['first_name'] = $this->input->post('first-name');
+ $data['last_name'] = $this->input->post('last-name');
+ $data['birth_date'] = $this->input->post('birth-date');
+ $data['country'] = $this->input->post('country');
+ $data['locality'] = $this->input->post('locality');
+ $data['ui_lang'] = $this->input->post('ui-lang');
+ $data['time_zone'] = $this->input->post('time-zone');
+
+ // Edit account data
+ if ($user_id)
{
- $this->session->set_userdata(array(
- 'user_id'=> $this->user_id,
- 'username'=> $this->username
- ));
-
- // Redirect to last page before login.
- header('Location: '. site_url(urldecode_segments($redirect)));
+ $password = $this->input->post('new-password');
+ if ($password)
+ $data['password'] = $this->input->post('new-password');
+
+ $this->users_model->set_userdata($user_id, $data);
}
+ // Registration
else
{
- $this->session->set_userdata(array(
- 'username'=> $this->username
- ));
-
- // Redirect to register page because an user authenticates here
- // for the first time with LDAP.
- // TODO
- header('Location: '. site_url(urldecode_segments($redirect)));
+ $data['username'] = $this->input->post('username');
+ $data['password'] = $this->input->post('password');
+
+ $this->users_model->register($data);
}
+
+ // Redirect to last page before login.
+ header('Location: '. site_url(urldecode_segments($redirect)));
}
}
+ public function account($redirect = '')
+ {
+ $this->register($redirect);
+ }
+
public function _valid_username($username)
{
- return (preg_match('/^[a-z0-9\._]+$/', $username) == 1);
+ return (preg_match('/^[a-z0-9\._]+$/', $username) === 1);
}
public function _valid_username_or_email($username)
else
return $this->_valid_username($username);
}
+
+ public function _valid_date($date)
+ {
+ if (! $date)
+ return TRUE;
+
+ return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1);
+ }
+
+ public function _valid_old_password($old_password, $field_username)
+ {
+ if (! $old_password)
+ return TRUE;
+
+ $username= $this->input->post($field_username);
+
+ if ($this->users_model->login($username, $old_password))
+ return TRUE;
+
+ return FALSE;
+ }
+
+ public function _change_password_cond($param)
+ {
+ $old = $this->input->post('old-password');
+ $new = $this->input->post('new-password');
+ $newc = $this->input->post('new-password-confirmation');
+
+ return (!$old && !$new && !$newc)
+ || ($old && $new && $newc);
+ }
+
+ public function _required_by_register($param)
+ {
+ $user_id = $this->input->post('user-id');
+
+ if (! $user_id && ! $param)
+ return FALSE;
+
+ return TRUE;
+ }
public function _do_login($username, $field_password)
{
- $password = $this->input->post('password');
+ $password = $this->input->post($field_password);
- $this->load->model('users_model');
$user = $this->users_model->login($username, $password);
- // Authentication failed
+ // Authentication failed.
if ($user === FALSE)
return FALSE;
- // First authentication of a user with LDAP, i.e. the user does not
- // have an user_id in `users` DB table yet.
- if ($user['auth_src'] == 'ldap_first_time')
- {
- $this->ldap_user_info = $user;
- $this->username = $user['uid'][0];
- $this->email = $user['mail'][0];
- return TRUE;
- }
-
- // Authentication when the user has an user_id in the DB.
- $this->username = $user['username'];
- $this->email = $user['email'];
- $this->user_id = $user['id'];
-
+ // Authentication successful: set session with user data.
+ $this->session->set_userdata(array(
+ 'user_id'=> $user['id'],
+ 'username'=> $user['username'],
+ 'auth_src'=> $user['auth_src']
+ ));
+ $this->import = $user['import'];
return TRUE;
}
}
include('system/language/english/form_validation_lang.php');
-$lang['_valid_username'] = 'You must enter a valid username with letters, numbers, . (dots) or _ (underscores).';
+$lang['_valid_username'] = 'You must enter a valid username with letters, numbers, . (dots) or _ (underscores).';
$lang['_valid_username_or_email'] = "You must enter an e-mail address or a valid username.";
-$lang['_do_login'] = "Wrong %s, or wrong %s.";
+$lang['_do_login'] = "Wrong %s, or wrong %s.";
+$lang['_valid_date'] = "Invalid %s! Use the specified format or leave the field blank if you don't want to specify it.";
+$lang['_valid_old_password'] = "Wrong %s.";
+$lang['_change_password_cond'] = 'If you want to change your password complete all three password related fields.';
+$lang['_required_by_register'] = 'The %s field is required.';
/* End of file form_validation_lang.php */
$lang['user_auth_src'] = 'Authentication Source';
$lang['user_video_prefs'] = 'Video Preferences';
$lang['user_note_required_fields'] = '* Required fields!';
+$lang['user_submit_register'] = 'Register';
+$lang['user_submit_save'] = 'Save';
/* End of file user_lang.php */
/* Location: ./application/language/english/user_lang.php */
\ No newline at end of file
// authenticating here for the first time so it does not have an entry
// in `users` table.
if ($query->num_rows() !== 1)
- return $this->ldap_login($username, $password);
+ {
+ $ldap_userdata = $this->ldap_login($username, $password);
+ $userdata = $this->convert_ldap_userdata($ldap_userdata);
+ $this->register($userdata);
+
+ $user = $this->login($username, $password);
+ $user['import'] = TRUE;
+ return $user;
+
+ /* foreach ($ldap_userdata as $k => $v)
+ {
+ echo "<h1>$k</h1>";
+ print_r($v);
+ }
+ die(); */
+ }
$user = $query->row_array();
return $user;
}
+ /**
+ * Converts an array returned by LDAP login to an array which contains
+ * user data ready to be used in `users` DB.
+ *
+ * @param array $ldap_userdata
+ * @return array
+ */
+ public function convert_ldap_userdata($ldap_userdata)
+ {
+ $userdata['username'] = $ldap_userdata['uid'][0];
+ $userdata['email'] = $ldap_userdata['mail'][0];
+ $userdata['first_name'] = $ldap_userdata['givenname'][0];
+ $userdata['last_name'] = $ldap_userdata['sn'][0];
+
+ $userdata['auth_src'] = 'ldap';
+
+ return $userdata;
+ }
+
/**
* Login with LDAP.
*
public function ldap_dn_belongs_ou($dn, $ou)
{
if (!is_array($ou))
- $ou = array ($ou);
+ $ou = array ($ou);
$founded = FALSE;
$words = explode(',', $dn);
$value = $parts[1];
if (strtolower($key) == "ou" && in_array($value, $ou) )
- $founded = TRUE;
+ $founded = TRUE;
}
+
return $founded;
}
+
+ /**
+ * Adds a new user to DB.
+ *
+ * @param array $data corresponds to DB columns
+ */
+ public function register($data)
+ {
+ $this->load->helper('array');
+
+ // TODO verify mandatory data existance
+
+ // Process data.
+ if (isset($data['password']))
+ $data['password'] = sha1($data['password']);
+ // TODO picture data: save, convert, make it thumbnail
+
+ $cols = '';
+ $vals = '';
+ foreach ($data as $col=> $val)
+ {
+ $cols .= "$col, ";
+ if (is_int($val))
+ $vals .= "$val, ";
+ else
+ $vals .= "'$val', ";
+ }
+ $cols = substr($cols, 0, -2);
+ $vals = substr($vals, 0, -2);
+
+ $query = $this->db->query("INSERT INTO `users`
+ ($cols)
+ VALUES ($vals)");
+
+ // TODO exception on failure
+ return $query;
+ }
+
+ /**
+ * Returns data from `users` table for user with $user_id.
+ *
+ * @param int $user_id
+ */
+ public function get_userdata($user_id)
+ {
+ $query = $this->db->query("SELECT * from `users`
+ WHERE id = $user_id");
+
+ if ($query->num_rows() === 0)
+ return FALSE;
+
+ return $query->row_array();
+ }
+
+ /**
+ * Modifies data from `users` table for user with $user_id.
+ *
+ * @param int $user_id
+ * @param array $data key-value pairs with columns and new values to be
+ * modified
+ */
+ public function set_userdata($user_id, $data)
+ {
+ // TODO verify mandatory data existance
+
+ // Process data.
+ if (isset($data['password']))
+ $data['password'] = sha1($data['password']);
+ // TODO picture data: save, convert, make it thumbnail
+
+ $set = '';
+ foreach ($data as $col => $val)
+ {
+ if (is_int($val))
+ $set .= "$col = $val, ";
+ else
+ $set .= "$col = '$val', ";
+ }
+ $set = substr($set, 0, -2);
+
+ $query_str = "UPDATE `users`
+ SET $set WHERE id = $user_id";
+ //echo "<p>$query_str</p>";
+ $query = $this->db->query($query_str);
+
+ // TODO exception
+ return $query;
+ }
}
/* End of file users_model.php */
<?php echo ($selected_menu == 'logout' ? 'class="selected"' : '') ?>><?php echo $this->lang->line('ui_nav_menu_logout') ?></a>
</li>
- <li class="menu-right"><a href="<?php echo site_url('user/account') ?>"
+ <li class="menu-right"><a href="<?php echo site_url('user/account/'. urlencode_segments(uri_string(), 'user/account')) ?>"
<?php echo ($selected_menu == 'account' ? 'class="selected"' : '') ?>><?php echo $this->lang->line('ui_nav_menu_account') ?></a>
</li>
-<?php echo form_open("user/register/$redirect") ?>
+<?php
+function _set_value($userdata, $field, $default = '')
+{
+ $post_value = set_value($field, $default);
+
+ if (! $userdata)
+ return $post_value;
+
+ return ($post_value === $default
+ ? $userdata[ str_replace('-','_',$field) ]
+ : $post_value);
+}
+?>
+<?php
+if (!$userdata):
+ echo form_open("user/register/$redirect");
+else:
+ echo form_open("user/account/$redirect");
+endif;
+?>
+
+<?php if ($userdata): ?>
+<input type="hidden" name="user-id" value="<?php echo $userdata['id'] ?>" />
+<input type="hidden" name="username" value="<?php echo $userdata['username'] ?>" />
+<!--<input type="hidden" name="password" value="12345" />
+<input type="hidden" name="password-confirmation" value="12345" />-->
+<?php endif ?>
+
<table class="form">
<tr>
- <td class="form-header"></td>
+ <td></td>
<td><span class="required"><?php echo $this->lang->line('user_note_required_fields') ?></span></td>
</tr>
<tr><td></td><td> </td></tr>
<tr>
- <th><?php echo $this->lang->line('user_username'). ' <span class="required">*</span> ' ?></th>
+ <?php if (! $userdata): ?>
+ <th><?php echo $this->lang->line('user_username'). ' <span class="required">*</span> : ' ?></th>
<td>
- <input type="text" name="username" size="16" value="<?php echo set_value('username') ?>" />
+ <input type="text" name="username" size="16" value="<?php echo _set_value($userdata, 'username') ?>" />
</td>
+ <?php else: ?>
+ <th><?php echo $this->lang->line('user_username'). ': ' ?></th>
+ <td>
+ <strong><?php echo $userdata['username'] ?></strong>
+ </td>`
+ <?php endif ?>
</tr>
<tr><td></td><td><?php echo form_error('username') ?></td></tr>
+ <?php // Register requires password ?>
+ <?php if (! $userdata):?>
<tr>
- <th><?php echo $this->lang->line('user_password'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_password'). ' <span class="required">*</span> : ' ?></th>
<td>
<input type="password" name="password" size="16" value="" />
</td>
<tr><td></td><td><?php echo form_error('password') ?></td></tr>
<tr>
- <th><?php echo $this->lang->line('user_password_confirmation'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_password_confirmation'). ' <span class="required">*</span> : ' ?></th>
<td>
<input type="password" name="password-confirmation" size="16" value="" />
</td>
</tr>
<tr><td></td><td><?php echo form_error('password-confirmation') ?></td></tr>
+ <?php // Edit account data requires password reset ?>
+ <?php elseif ($userdata && $userdata['auth_src'] == 'internal'): ?>
+ <tr>
+ <th><?php echo $this->lang->line('user_old_password'). ' <span class="required">*</span> : ' ?></th>
+ <td>
+ <input type="password" name="old-password" size="16" value="" />
+ </td>
+ </tr>
+ <tr><td></td><td><?php echo form_error('old-password') ?></td></tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_new_password'). ' <span class="required">*</span> : ' ?></th>
+ <td>
+ <input type="password" name="new-password" size="16" value="" />
+ </td>
+ </tr>
+ <tr><td></td><td><?php echo form_error('new-password') ?></td></tr>
+
+ <tr>
+ <th><?php echo $this->lang->line('user_new_password_confirmation'). ' <span class="required">*</span> : ' ?></th>
+ <td>
+ <input type="password" name="new-password-confirmation" size="16" value="" />
+ </td>
+ </tr>
+ <tr><td></td><td><?php echo form_error('new-password-confirmation') ?></td></tr>
+ <?php endif ?>
<tr>
- <th><?php echo $this->lang->line('user_email'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_email'). ' <span class="required">*</span> : ' ?></th>
<td>
- <input type="text" name="email" size="16" value="<?php echo set_value('email') ?>" />
+ <input type="text" name="email" size="16" value="<?php echo _set_value($userdata, 'email') ?>" />
</td>
</tr>
<tr><td></td><td><?php echo form_error('email') ?></td></tr>
<tr><td></td><td> </td></tr>
<tr>
- <th><?php echo $this->lang->line('user_first_name'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_first_name'). ' <span class="required">*</span> : ' ?></th>
<td>
- <input type="text" name="first-name" size="16" value="<?php echo set_value('first-name') ?>" />
+ <input type="text" name="first-name" size="16" value="<?php echo _set_value($userdata, 'first-name') ?>" />
</td>
</tr>
<tr><td></td><td><?php echo form_error('first-name') ?></td></tr>
<tr>
- <th><?php echo $this->lang->line('user_last_name'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_last_name'). ' <span class="required">*</span> : ' ?></th>
<td>
- <input type="text" name="last-name" size="16" value="<?php echo set_value('last-name') ?>" />
+ <input type="text" name="last-name" size="16" value="<?php echo _set_value($userdata, 'last-name') ?>" />
</td>
</tr>
<tr><td></td><td><?php echo form_error('last-name') ?></td></tr>
<tr>
- <th><?php echo $this->lang->line('user_birth_date'). ' ' ?></th>
+ <th><?php echo $this->lang->line('user_birth_date'). ' : ' ?></th>
<td>
- <input type="text" name="birth-date" id="birth-date" size="16" value="<?php echo set_value('birth-date') ?>" /> (<?php echo $this->lang->line('user_date_format_hint') ?>)
+ <input type="text" name="birth-date" id="birth-date" size="16" value="<?php echo _set_value($userdata, 'birth-date') ?>" /> (<?php echo $this->lang->line('user_date_format_hint') ?>)
</td>
</tr>
<tr><td></td><td><?php echo form_error('birth-date') ?></td></tr>
<tr>
- <th><?php echo $this->lang->line('user_country'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_country'). ' <span class="required">*</span> : ' ?></th>
<td>
- <?php echo country_dropdown('country', array('RO'), set_value('country', 'RO')) ?>
+ <?php echo country_dropdown('country', array('RO'), _set_value($userdata, 'country', 'RO')) ?>
</td>
</tr>
<tr><td></td><td><?php echo form_error('country') ?></td></tr>
<tr>
- <th><?php echo $this->lang->line('user_locality'). ' ' ?></th>
+ <th><?php echo $this->lang->line('user_locality'). ' : ' ?></th>
<td>
- <input type="text" name="locality" size="16" value="<?php echo set_value('locality') ?>" />
+ <input type="text" name="locality" size="16" value="<?php echo _set_value($userdata, 'locality') ?>" />
</td>
</tr>
<tr><td></td><td><?php echo form_error('locality') ?></td></tr>
<tr><td></td><td> </td></tr>
<tr>
- <th><?php echo $this->lang->line('user_ui_lang'). ' ' ?></th>
+ <th><?php echo $this->lang->line('user_ui_lang'). ' : ' ?></th>
<td>
- <?php echo available_languages_dropdown('ui-lang', set_value('ui-lang', 'en')) ?>
+ <?php echo available_languages_dropdown('ui-lang', _set_value($userdata, 'ui-lang', 'en')) ?>
</td>
</tr>
<tr><td></td><td><?php echo form_error('ui-lang') ?></td></tr>
<tr>
- <th><?php echo $this->lang->line('user_time_zone'). ' <span class="required">*</span> ' ?></th>
+ <th><?php echo $this->lang->line('user_time_zone'). ' <span class="required">*</span> : ' ?></th>
<td>
- <?php echo timezone_menu(set_value('time-zone', 'UP2')) ?>
+ <?php echo timezone_menu(_set_value($userdata, 'time-zone', 'UP2'), '', 'time-zone') ?>
</td>
</tr>
<tr><td></td><td><?php echo form_error('time-zone') ?></td></tr>
<tr>
<td></td>
<td>
- <input type="submit" value="<?php echo $this->lang->line('ui_nav_menu_register') ?>" />
+ <?php if (! $userdata): ?>
+ <input type="submit" value="<?php echo $this->lang->line('user_submit_register') ?>" />
+ <?php else: ?>
+ <input type="submit" value="<?php echo $this->lang->line('user_submit_save') ?>" />
+ <?php endif ?>
</td>
</tr>
</table>
table.form th
{
text-align: right;
- /*width: 25%;*/
+ width: 24em;
}
table.form td
{
- vertical-align: bottom;
+ vertical-align: top;
}
.error
/* End of file date_helper.php */
-/* Location: ./system/helpers/date_helper.php */
\ No newline at end of file
+/* Location: ./system/helpers/date_helper.php */