BioInfoExpert

Bioinformatics day-by-day

Menu

Skip to content
  • Home
  • About me
  • BioInformatics
  • CloudComputing
  • Linux
  • Python
  • Raspberry Pi
  • Ubuntu

Tag Archives: account

Add users accounts in batch to Drupal8

Posted on June 21, 2016 by lpryszcz

I have spent quite some time today, trying to add batch of user accounts intro Drupal8. After all it’s not that difficult, all process is limited to installation of PHP module (that was stripped from Drupal8) and creating new page. Make sure, this page is available only to system administrator!

  1. Install & enable PHP filter
  2. PHP filter was removed from Drupal8 core.

  3. Create new PHP-enabled page (PHP filter)
  4. Below code, defines HTML form and PHP code, that reads the input and store users information. Entries are skipped if given user name is already registered.
    Make sure, this page is saved, but UNPUBLISHED! Otherwise, other user will be able to use it and register user accounts!!!

    <?php 
    if (!empty($_POST)): 
    echo "Adding users...<br>";
    
    // read textarea
    $text = trim($_POST['users']);
    $textAr = explode("\n", $text);
    $textAr = array_filter($textAr, 'trim'); // remove any extra \r characters left behind
    
    $i = 0;
    foreach ($textAr as $line) {
    // explode line
    $parts = preg_split('/\s+/', $line);
    
    // check if added
    $user = user_load_by_name($parts[0]); //$user_id = $user->uid;
    if(!$user){
    $i++; 
    echo " $i $parts[0] $parts[1] <br>";
    
    $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
    $user = \Drupal\user\Entity\User::create();
    
    //Mandatory settings
    $user->setPassword(user_password());
    $user->enforceIsNew();
    $user->setEmail($parts[1]);
    $user->setUsername($parts[0]);
    
    //Optional settings
    $user->set("init", $parts[1]);
    $user->set("langcode", $language);
    $user->set("preferred_langcode", $language);
    $user->set("preferred_admin_langcode", $language);
    $user->activate();
    //Save user
    $res = $user->save();
    
    // create user profile alias: /user/username -> /user/uid
    $uid = $user->get('uid')->value;
    $system_path = "/user/$uid";
    $path_alias = "/user/$parts[0]";
    $path = \Drupal::service('path.alias_storage')->save($system_path, $path_alias, $language);
    
    // sent email
    _user_mail_notify('register_no_approval_required', $user);
    }
    else {
    $uid = $user->get('uid')->value;
    echo "- $parts[0] already present with uid=$uid!<br>";
    } 
    }
    
    echo "Added $i users!<br>"; 
    ?>
    
    <?php else: ?>
        <form action="/add_users" method="post">
    <p>Provide new users data, one user per line, in this order: username and email (separated by spaces). </p>
            <textarea name="users" style="width: 400px; height: 200px;"></textarea><br>
            <input type="submit">
        </form>
    <?php endif; ?>
    

VoilĂ !

Inspired by drupal8.ovh & this discussion & this.

Posted in Big Data, drupal, Ubuntu, webserver | Tagged account, batch, drupal8, registration, user | Leave a comment

Recent posts

  • GPU basecalling with MinION ver. 21.06.0
  • GPU basecalling with MinION
  • Ubuntu with Gnome extensions for productivity
  • Monitoring GPU usage
  • Python code profiling and accelerating your calculations with numba
  • How to setup NVIDIA / CUDA for accelerated computation in Ubuntu 18.04 / Gnome / X11 workstation?
  • How to install Java and JavaWS in Linux

Tag Cloud

apache2 application aws bam BASH batch bedtools bigWig build compression configuration conversion convert database differential expression docker ec2 error gene git github gpu htop installation large libreoffice multiprocessing MySQL NGS no root pdf performance Pool problem Process push python raspberry pi remote access rnaseq sqlite3 tar ubuntu virtualenv xlsx

Category Cloud

android android AWS BASH benchmark Big Data BioInformatics camera CloudComputing Docker drupal EC2 encryption external GBrowse General git GPU Hard drive kindle Linux MinION Music MySQL Network NGS no-root Photos public services Python R Raspberry Pi Security Ubuntu Ubuntu Uncategorized USB3.0 VBox Video webserver
Proudly powered by WordPress | Theme: Able by WordPress.com. | Follow me on Mastodon