User Registration and Additional User Information

edited June 2016 in Modules
The documentation doesn't have any information on registering a user programatically or adding new fields.

I can create a new module with a table relation to the users for new user info. But how do I create a new user?

Is there no function like $this->fuel->users->create($array)?

Comments

  • edited 8:26AM
    Are you talking about Fuel users or 3rd party, front-end users?

    For the second option, the TankAuth Codeignitor modules works well but needs some integration.
  • I stumbled across the same problem (I think), to create a fuel user programatically.
    In my case I used a migration script:

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Migration_add_user extends CI_Migration {
    
        var $CI;
    
        function __construct()
        {
            $this->CI = &get_instance();
    
        }
    
    
        public function up()
        {
    
            $_POST = [
    
                'email' => 'test@test.com',
                'user_name' => 'test',
                'first_name' => 'Tester',
                'last_name' => 'Test',
                'language' => 'german',
                'password' => uniqid(),
                'is_invite' => 1 // trigger email to reset password
            ];
    
            // @link https://docs.getfuelcms.com/libraries/fuel_users_model
            // load fuel_users_model
            $user_model = $this->fuel->users->model();
            $user = $user_model->create($_POST);
    
            // save user and trigger Fuel_users_model::on_before/after_save() to trigger password
            $user_id = $user->save();
    
            if($user_id){
                // alternatively set 'is_invide' => 0 and send email with password in mail...
                //$res = $this->fuel->users->send_email($user_id);
            }else{
                show_error('User already exists');
            }
    
            // TODO: Permissions
        }
    
        public function down()
        {
            $this->fuel->users->model()->delete(['user_name' => 'test']);
        }
    }
    

    Feel free to correct this snippet if you think it's wrong or there is a better way.

  • Here is a better version of a migration script, that adds a new Fuel CMS user, sends an email so the new user can set a new password and applies the user role "Editor" which is a defined set of Fuel CMS permissions using the Group Access module .

    You can get my forked and updated version of Group Access here:
    https://github.com/marcus-at-localhost/FUEL-CMS-Group-Access

    <?php defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Migration_add_user extends CI_Migration {
    
        function __construct()  {}
    
    
        public function up()
        {
    
            $_POST = [
                'email' => 'test@test.com',
                'user_name' => 'test',
                'first_name' => 'Tester',
                'last_name' => 'Test',
                'language' => 'german',
                'password' => uniqid(),
                'is_invite' => 1 // trigger email to reset password
            ];
    
            // @link https://docs.getfuelcms.com/libraries/fuel_users_model
            // load fuel_users_model
            $user_model = $this->fuel->users->model();
            $user = $this->fuel->users->model()->find_or_create(['user_name' => 'test']);
    
            $user->fill($_POST);
    
            // save user and trigger Fuel_users_model::on_before/after_save() to trigger password
            $user_id = $user->save();
    
            if($user_id){
                // see @link https://github.com/marcus-at-localhost/FUEL-CMS-Group-Access
                $group_access_model = $this->fuel->group_access->model('Groups');
                $group = $group_access_model->find_one(['name' => 'Editor']);
                $users = array_keys($group->get_users(true)->options_list('id','id'));
                array_push($users,$user_id);
                $group->users = $users;
                $group->permissions = array_keys($group->get_permissions(true)->options_list('id','id'));
                $group->save();
    
                // alternatively set 'is_invide' => 0 and send email with password in mail...
                //$res = $this->fuel->users->send_email($user_id);
            }
    
        }
    
        public function down()
        {
            $user = $this->fuel->users->model()->find_one(['user_name' => 'stm']);
            $user_id = $user->id;
            // you can use:
            #$user->delete();
            // if this method was fixed:
            // https://github.com/daylightstudio/FUEL-CMS/commit/6db780170df08a833ced078d48a7322f0c49c32b
            // else:
            $this->fuel->users->model()->delete(['user_name' => 'test']);
    
            $group_access_model = $this->fuel->group_access->model('Groups');
            $group = $group_access_model->find_one(['name' => 'Editor']);
    
            $users = array_keys($group->get_users(true)->options_list('id','id'));
            $users = array_diff($users,[$user_id]);
            $group->users = $users;
    
            $group->permissions = array_keys($group->get_permissions(true)->options_list('id','id'));
            $group->save();
    
        }
    }
    
Sign In or Register to comment.