Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14343

[ion auth] Creating the first user

$
0
0
Hi, I needed to create a new admin user for an app using ion auth. Could someone give me opinionated feedback, or show me a better way of doing it?

Model
PHP Code:
 
  
// setup the admin account
 
 public function check_activation_required()
 
 {
 
   $query $this->db->query('SELECT * FROM users');
 
   // check for no accounts
 
   if(empty($query->result())){
 
     return TRUE;
 
   }
 
   else {
 
     $this->session->set_flashdata('message''An admin account has already been activated. Please login.');
 
     return FALSE;
 
   }

 
 }

 
 public function setup_admin_account()
 
 {
 
   $login $this->input->post('login');
 
   $password $this->input->post('password');

 
   // create an admin group
 
   $create_group $this->ion_auth->create_group('admin''The administration group');
 
   if(!$create_group) {
 
     $this->session->set_flashdata('message'$this->ion_auth->errors());
 
   }

 
   $admin_group NULL;
 
   $group_query $this->ion_auth->groups()->result();
 
   
    foreach
($group_query as $struct) {
 
     if ($struct->name == 'admin') {
 
       $admin_group $struct->id;
 
       break;
 
     }
 
   }
 
   if(is_null($admin_group)){
 
     $this->session->set_flashdata('message''Could not create group or find admin group id.');
 
     return;
 
   }

 
   /*
      Register (create) a new user.
      Parameters:
      1. 'Identity' - string REQUIRED. This must be the value that uniquely identifies the user when he is registered. If you chose "email" as $config['identity'] in the configuration file, you must put the email of the new user.
      2. 'Password' - string REQUIRED.
      3. 'Email' - string REQUIRED.
      4. 'Additional Data' - multidimensional array OPTIONAL.
      5. 'Group' - array OPTIONAL. If not passed the default group name set in the config will be used.
    */
 
   if ($this->ion_auth->register($login$password$loginNULL, array($admin_group) )) {
 
     $this->session->set_flashdata('message'$this->ion_auth->messages());
 
   }
 
   else {
 
     $this->session->set_flashdata('message'$this->ion_auth->errors());
 
   }

 
 

Controller
PHP Code:
 
  public 
function setup()
 
 {
 
   if($this->account_model->check_activation_required() === FALSE) {
 
     redirect('account/login''refresh');
 
   }

 
   $validate $this->account_model->validate['account']; 
 
   $this->form_validation->set_rules($validate); 
 
   if($this->form_validation->run() === FALSE) {
 
     $this->render('account_setup'); 
 
   }
 
   else {
 
     $this->account_model->setup_admin_account();
 
     redirect('account/login''refresh');
 
   }
 
 

Thanks Smile

Viewing all articles
Browse latest Browse all 14343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>