Search code examples
phpmysqlconcrete5concreteconcrete5-5.7

Concrete5 5.6/7 programaticly adding user and enriching attributes


What is the best aproace of enriching c5 user's attributes.

I have non C5 table with users information this information was created on old cms (non c5), and im now building new site with c5 would like to know best aproach of migrating users.

Is it good idea to use SQL query or should i use php script for enriching, I already created users in to c5 and manualy added email addresses for "anchor point" for later enrichment.

Would be realy glad if someone could tell or maby could lead to some examples.


Solution

  • finaly managed by myself, its rather simple: i exported external users to php array and used c5 user functions to add users and after enrich them my example:

    $external_users = array({
    array('id'=>'1', 'name'='JON', 'email'=>'[email protected]', 'last_name'=>'DOE', 'attr1'=>'smthing', 'attr2'=>'123'),
    array(...), ...
    });
    
    foreach($external_users as $singleUser_data){
    $email = $singleUser_data['email'];
    $ui = UserInfo::getByEmail($email);
    
        if (!is_null($ui)) {
            // Email is already in use, so let's not create the user
            return;
        }
    
        $userData['uName'] = $singleUser_data['name']." ".$singleUser_data['lastname'];
    
    //users later need to reset password
            $userData['uPassword'] = 'asd52465465456454asd';
            $userData['uPasswordConfirm'] = 'asd52465465456454asd';
    
    
         //user registererd
        $ui = UserInfo::register($userData);
    
        set_new_user_group($ui);
    enrichAtributes($ui, $singleUser_data);
            }
    
    function set_new_user_group($ui){
       // assign the new user to a group
        $g = Group::getByName('GroupName');
        $u = $ui->getUserObject();
        $u->enterGroup($g);
    }
    
    function enrichAtributes($ui, $singleUser_data){
    
    $ui->setAttribute('atr_handler1', $singleUser_data['attr1']);
        $ui->setAttribute('atr_handler2', $singleUser_data['attr2']);
    }
    

    Resource: User registration programaticly and seting group

    User information documentation (setting attributes)