Support » Plugin: Import users from CSV with meta » Invalid Role in CSV File does not generate error

  • Resolved Malcolm

    (@malcolm-oph)


    An invalid role in the CSV file generates a user with a role of “none” because there is no check if the role supplied is valid.

    I fixed this by changing the section of code currently starting at line 313.
    This changes from:

    if( !empty( $role ) ){
    	if( is_array( $role ) ){
    		foreach ($role as $single_role) {
    			$user_object->add_role( $single_role );
    		}	
    	}
    	else{
    		$user_object->add_role( $role );
    	}
    }
    

    to:

    $invalidRoles = '';
    if( !empty( $role ) ){
    	if( !is_array( $role ) ) $role[] = $role;
    	
    	foreach ($role as $single_role) {
    		$single_role = strtolower($single_role);
    		if (get_role($single_role))
    		{
    			$user_object->add_role( $single_role );
    		}
    		else
    		{
    			$invalidRoles .= $single_role.' ';
    		}
    	}	
    }
    
    if ($invalidRoles != ''){
    	$invalidRoles = trim($invalidRoles);
    	$problematic_row = true;
    	$data[0] = __('Invalid Role','import-users-from-csv-with-meta').' ('.trim($invalidRoles).')';
    }
    

    I think that I’m right that roles are always stored internally in lower case, so in addition to the bug fix, I’ve forced the role to be lowercase.

    Could be more elegant, but hopefully it does the job

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Javier Carazo

    (@carazo)

    @malcolm-oph,

    Thanks for this new conditionals to make this plugin more proffessional.

    I have just included it with a little modification.

    $invalid_roles = array();
    if( !empty( $role ) ){
    	if( !is_array( $role ) ) 
    		$role[] = $role;
    	
    	foreach ($role as $single_role) {
    		$single_role = strtolower($single_role);
    		if( get_role( $single_role ) ){
    			$user_object->add_role( $single_role );
    		}
    		else{
    			$invalid_roles[] = trim( $single_role );
    		}
    	}	
    }
    
    if ( !empty( $invalid_roles ) ){
    	$problematic_row = true;
    	if( count( $invalid_roles ) == 1 )
    		$data[0] = __('Invalid role','import-users-from-csv-with-meta').' (' . reset( $invalid_roles ) . ')';
    	else
    		$data[0] = __('Invalid roles','import-users-from-csv-with-meta').' (' . implode( ', ', $invalid_roles ) . ')';
    }

    I am going to upload to the repository now!

    ¬°Gracias! As we say in Spain.

    • This reply was modified 1 year, 2 months ago by  Javier Carazo.
    • This reply was modified 1 year, 2 months ago by  Javier Carazo.
    Malcolm

    (@malcolm-oph)

    Excellent! Many thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Invalid Role in CSV File does not generate error’ is closed to new replies.