Welcome Guest, Not a member yet? Register   Sign In
[ion auth] Creating the first user
#1

(This post was last modified: 02-10-2017, 05:19 AM by n2fole00.)

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
Reply
#2

(This post was last modified: 02-10-2017, 10:15 AM by enlivenapp.)

I do something similar in open blog. http://open-blog.org

Take a look in installer/controllers/installer...

Basically I pulled a couple of the functions from ion auth 2 to properly build and hash the password. The rest is trivial.

Sorry for the brevity, I'm on my phone.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB