Permalink
Browse files

Fix user login with unexisting record

  • Loading branch information...
1 parent 9017c86 commit b2c21aad5b6725e384721cd1de2dedb1a4acb256 @snwfog snwfog committed Apr 9, 2012
Showing with 39 additions and 34 deletions.
  1. +1 −1 application/config/database.php
  2. +33 −32 application/controllers/pasta.php
  3. +5 −1 application/views/static/header.php
@@ -66,4 +66,4 @@
/* End of file database.php */
-/* Location: ./application/config/database.php */
+/* Location: ./application/config/database.php */
@@ -71,7 +71,7 @@ public function register() {
// echo $query->num_rows();
if ($query->result()) {
- echo "USER ALREADY REGISTERED";
+ echo "User is already registered.";
} else {
$this->db->insert('logins', array(
'student_id' => $this->input->post('student_id'),
@@ -110,7 +110,7 @@ public function user_login() {
$this->form_validation->set_rules(
'login_student_id',
'Student ID',
- 'required|trim|xss_clean|exact_length[7]|numeric'
+ 'required|trim|xss_clean|exact_length[7]|numeric|callback_has_record'
);
$this->form_validation->set_rules(
@@ -122,34 +122,23 @@ public function user_login() {
if ($this->form_validation->run() == FALSE) {
$this->index();
} else {
- // from http://www.haughin.com/2008/02/handling-passwords-in-codeigniter/
- if ($this->logins_table->find_by_login_info(
- $this->input->post('login_student_id'),
- $this->encrypt->sha1($this->input->post('login_password')))) {
-
- // ------------------------------------
- // initialize sessions
- // ------------------------------------
-
- $user_data = $this->logins_table->find_by_student_id(
- $this->input->post('login_student_id'));
-
- $this->session->set_userdata(array(
- 'id' => $user_data['id'],
- 'student_id' => $user_data['student_id'],
- 'first_name' => $user_data['first_name'],
- 'last_name' => $user_data['last_name'],
- 'logged_in' => true
- ));
-
- // redirect to user profile page
- redirect('profile', 'redirect');
- } else {
- echo "Sorry, we could not find you in our records. "
- . ", should you "
- . anchor(site_url('pasta'), 'register first')
- . "?";
- }
+ // ------------------------------------
+ // initialize sessions
+ // ------------------------------------
+
+ $user_data = $this->logins_table->find_by_student_id(
+ $this->input->post('login_student_id'));
+
+ $this->session->set_userdata(array(
+ 'id' => $user_data['id'],
+ 'student_id' => $user_data['student_id'],
+ 'first_name' => $user_data['first_name'],
+ 'last_name' => $user_data['last_name'],
+ 'logged_in' => true
+ ));
+
+ // redirect to user profile page
+ redirect('profile', 'redirect');
}
}
@@ -168,13 +157,25 @@ public function alpha_whitespace($str)
* Callback function for an unique student it form validation
* Disallow already taken student it
*/
- public function is_unique_student_it($student_it)
+ public function is_unique_student_it($student_id)
{
$this->form_validation->set_message('is_unique_student_it',
'Student ID already taken.');
- $query = $this->logins_table->find_by_student_id($student_it);
+ $query = $this->logins_table->find_by_student_id($student_id);
return ($query == NULL) ? TRUE : FALSE;
}
+
+ /**
+ * Callback function for if an user tries to login with unexisting
+ * student ID from the record
+ */
+ public function has_record($student_id)
+ {
+ $this->form_validation->set_message('has_record',
+ 'User record not found.');
+ $query = $this->logins_table->find_by_student_id($student_id);
+ return ($query == NULL) ? FALSE : TRUE;
+ }
}
/* End of file Pasta.php */
@@ -3,6 +3,10 @@
<!-- P.A.S.T.A. header -->
<head>
<meta charset="utf-8">
+ <!-- meta -->
+ <meta name="viewport" content="initial-scale=1, maximum-scale=1" />
+ <meta name="apple-mobile-web-app-capable" content="yes" />
+
<title><?php echo $title; ?></title>
<!-- style -->
@@ -23,7 +27,7 @@
<script type="text/javascript" src="<?=base_url();?>assets/js/script.js"></script>
-
+
</head>
<body>

0 comments on commit b2c21aa

Please sign in to comment.