2012-02-19 40 views
0

我有一个通用的create函数提交一个新的用户到数据库 - 这工作正常。我在哪里被卡住是以下。控制器帮助和功能

  • 我知道我需要注册的用户在帐户登录前点击电子邮件中的链接。 当我运行create函数时,如何将其实现到我的if statement

  • 我有点困惑如何设置我的错误,如果有任何事情是正确的或错误的激活过程我目前使用$this->form_validation->set_message();设置消息。 我是否需要使用set_flashdata();?以及如何回应这些观点?

当我create一个新的用户我在0默认userActive字段集,也是默认组设置为users

控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Users extends CI_Controller { 


    public function index() 
    { 
     $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName; 
     $data['pageTitle'] = "Create User"; 
     $this->load->view('frontend/assets/header', $data); 
     $this->load->view('frontend/users', $data); 
     $this->load->view('frontend/assets/footer'); 
    } 

    public function create(){ 

     //If form validation fails load previous page with errors else do the job and insert data into db 

     if($this->form_validation->run('createUser') == FALSE) 
     { 
      $data['success'] = ""; 
     }else{ 
      $username = $this->input->post('userName'); 
      $password = $this->input->post('userPassword'); 
      $firstname = $this->input->post('userFirstName'); 
      $lastname = $this->input->post('userLastName'); 
      $email = $this->input->post('userEmail'); 

      $passwordHash = $this->encrypt->sha1($password); // Lets encrypt the password why sha1? MD5 is for tossers 

      $activateCode = $this->_activateCode(10); 

      // If the data is correct follow through with db insert 

      if($this->users_model->createUser($username,$passwordHash,$firstname,$lastname,$email,$activateCode)) 
      { 
       $data['success'] = TRUE; 

       redirect('frontend/users/create','refresh'); 

      } 

     } 
     $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName; 
     $data['pageTitle'] = "Create User"; 
     $this->load->view('frontend/assets/header', $data); 
     $this->load->view('frontend/user_create', $data); 
     $this->load->view('admin/assets/footer'); 

     echo get_class($this); 
     var_dump(method_exists($this, '_activateCode')); 
    } 

    function _userRegEmail($activateCode,$email,$firstname,$lastname){ 
     $data['companyName'] = $this->core_model->companyDetails()->coreCompanyName; 
     $data['companyEmail'] = $this->core_model->companyDetails()->coreCompanyEmail; 
     $data['companyContact'] = $this->core_model->companyDetails()->coreContactName; 
     $data['firstName'] = $firstName; 
     $data['lastName'] = $lastname; 
     $data['email'] = $email; 
     $data['activateCode'] = $activateCode; 

     $this->email->from($this->core_model->companyDetails()->coreCompanyEmail, $this->core_model->companyDetails()->coreCompanyName); 
     $this->email->to($email); 
     $this->email->subject($this->core_model->companyDetails()->coreCompanyName, 'User Registration Confirmation'); 

     $messageContent= $this->load->view('email_templates/userReg','', TRUE); 

     $this->email->message($messageContent); 

     //$this->email->send(); 
    } 

    function usersconfirm(){ 

     $activateCode = $this->uri->segment(3); 

     if($activateCode == '') 
     { 
      $this->form_validation->set_message('userConfirmError', 'Sorry you did not have a correct Activation Code.'); 
     } 
      $userConfirmed = $this->users_model->confirm_user($activateCode); 

      if($userConfirmed){ 
       $this->form_validation->set_message('userConfirmed', 'Thanks your account is now active you may login!'); 
      }else{ 
       $this->form_validation->set_message('userRecord', 'I am sorry we do not have any details with that Activation Code'); 
      } 
    } 

    function _username_check($username) 
    { 
     if($this->users_model->username_taken($username)) 
     { 
      $this->form_validation->set_message('username_check', 'Sorry the username %s is taken!'); 
      return FALSE; 
     }else{ 
      return TRUE; 
     } 
    } 

    function _email_check($email) 
    { 
     if($this->users_model->email_check($email)) 
     { 
      $this->form_validation->set_message('email_check','Sorry there is already a user with this %s'); 
      return FALSE; 
     }else{ 
      return TRUE; 
     } 

    } 

    function _activateCode($length) 
    { 
     return random_string('alnum', $length); 
    } 

} 

/* End of file users.php */ 
/* Location: ./application/controllers/users.php */ 

回答

0

你可以通过在if语句中检查数据库中的userActive来确定用户是否点击了激活链接。

当然可以使用闪存数据。您可以通过以下方式检索闪存数据: $this->session->flashdata('item');可以回显到视图。

http://codeigniter.com/user_guide/libraries/sessions.html>闪存数据

+0

谢谢,我似乎有,我仍然被添加到数据库中没有电子邮件的任何迹象的问题。即使通过我设置的回调函数来检查用户,它也在添加 – 2012-02-21 00:11:58

+0

这是可以预料的:用户在注册时被添加到数据库!当他们点击链接时,电子邮件将他们的帐户设置为ACTIVE。除非用户帐户处于ACTIVE状态,否则不会让用户进入该网站。因此,注册 - >用户在DB与account_active = 0;邮件已发送;点击链接 - > account_active = 1 – stormdrain 2012-02-21 13:15:17

+0

我知道,但它仍然添加,即使用户名管理存在,我没有回电,但我得到验证时,窗体是“必需”,但没有超过。 – 2012-02-21 20:25:47