Importing Users from .csv file and adding Users to different groups

Many thx for your answer.
In the meanwhile I found a solution to my problem. I now use a check if there is an empty string in .csv-file.
Dependig if yes or no the script does different commands.
Like that:

 if [ "${var_group4}" != "" ] ;then
        su -s /bin/sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --group='${var_group2}' --group='${var_group3}' --group='${var_group4}' --display-name='${var_name}'"
    elif [ "${var_group3}" != "" ] ;then
        su -s /bin/sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --group='${var_group2}' --group='${var_group3}' --display-name='${var_name}'"
    elif [ "${var_group2}" != "" ] ;then
        su -s /bin/sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --group='${var_group2}' --display-name='${var_name}'"

This works great and my script can now add users with a random pw int different groups with a range of one to four groups and also adding email and quota in once.

Best regards
tarnari

3 Likes

Hello, nice work.
Can you share full script when need to add users in one group and share the csv file how you add data
Thank you

Here is the fullscript:

#!/bin/bash
var_datum=$(date +"%Y%m%d")
input="impfin.csv"
var_apache_user=www-data
var_path_nextcloud=/var/www/nextcloud
var_result_file="${var_datum}_user_create.txt"

while read -r line
do
    echo "Rang: ${line}"
    var_password=$(pwgen 8 -c -n -N 1)
    set -e
    export OC_PASS=$var_password
    echo "${var_password} ${OC_PASS}"
    var_username=$(echo "${line}" | cut -d";" -f2)
    var_name=$(echo "${line}" | cut -d";" -f1)
    var_group1=$(echo "${line}" | cut -d";" -f3)
    var_group2=$(echo "${line}" | cut -d";" -f4)
    var_group3=$(echo "${line}" | cut -d";" -f5)
    var_group4=$(echo "${line}" | cut -d";" -f6)
    var_email=$(echo "${line}" | cut -d";" -f7)
    var_quota=$(echo "${line}" | cut -d";" -f8)
    if [ "${var_group4}" != "" ] ;then
        su -s /bin/sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --group='${var_group2}' --group='${var_group3}' --group='${var_group4}' --display-name='${var_name}'"
    elif [ "${var_group3}" != "" ] ;then
        su -s /bin/sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --group='${var_group2}' --group='${var_group3}' --display-name='${var_name}'"
    elif [ "${var_group2}" != "" ] ;then
        su -s /bin/sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --group='${var_group2}' --display-name='${var_name}'"
    fi
    su -s /bin/sh ${var_apache_user} -c " php ${var_path_nextcloud}/occ user:setting ${var_username} settings email '${var_email}'"
    su -s /bin/sh ${var_apache_user} -c " php ${var_path_nextcloud}/occ user:setting ${var_username} files quota '${var_quota}'"
    echo "${var_username};${var_password}" >> "${var_result_file}"
done < "$input"
exit 0

The csv file is as explained by tarnari:
surname & name;name;group1;group2;group3;group4;email;quota;

Don’t forget the last ; after quota or it won’t work !

4 Likes

Thank you for share the script and share how make the csv.
I just have one question why you have 4 groups ? Is something important ?
May be is from database ? I see in database different tables for groups and id of groups.
Why ask, I have 2 groups admins, clients, may be make one more group.

And one question more. I not see to db connection or cloud connection?

Nice fix.
Can you share with the csv file.
I make the csv like

user1 user1 '“ '“ '“ '“ user1@domain.com X GB

I make the script and csv with you suggest but is only give me password on terminal not added users.
And when the script add users ?
In db have
table oc_users
table oc_accounts

Ok, I get some answer from script like this :

Rang: user1 Cloud1,,,,user1@domain.com ,XGB
password password

Too many arguments, expected arguments “command” “uid”.

user:add [–password-from-env] [–display-name [DISPLAY-NAME]] [-g|–group [GROUP]] [–]

Some time I add user from occ command and now understand need to change some script command.

linuxman you’re not using the right separator in your csv: it must be ; and not ,
Groups are not mandatory: you can add 4 groups per user, or leave it blank

Ok , I change the separator but result is the same

Hello,
Now I successful add users to Nexcloud with script and is maked Display name, username, password but not set the quota for disk usage
Below is script I change it some parts :

#!/bin/bash
var_datum=$(date +"%Y%m%d")
input="impfin.csv"
#var_apache_user=user
var_path_nextcloud=/home/user/public_html
var_result_file="${var_datum}_user_create.txt"

while read -r line
do
    echo "Rang: ${line}"
    var_password=$(pwgen 8 -c -n -N 1)
    set -e
    export OC_PASS=$var_password
    echo "${var_password} ${OC_PASS}"
    var_username=$(echo "${line}" | cut -d";" -f2)
    var_name=$(echo "${line}" | cut -d";" -f1)
    var_group1=$(echo "${line}" | cut -d";" -f3)
    var_group2=$(echo "${line}" | cut -d";" -f4)
    var_group3=$(echo "${line}" | cut -d";" -f5)
    var_group4=$(echo "${line}" | cut -d";" -f6)
    var_email=$(echo "${line}" | cut -d";" -f7)
    var_quota=$(echo "${line}" | cut -d";" -f8)
    if [ "${var_group4}" != "" ] ;then
        sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --display-name='${var_name}'"
    elif [ "${var_group3}" != "" ] ;then
        sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='${var_group1}' --display-name='${var_name}'"
    elif [ "${var_group2}" != "" ] ;then
        sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env  --group='${var_group1}' --display-name='${var_name}'"
    fi 
    sh ${var_apache_user} -c " php ${var_path_nextcloud}/occ user:setting ${var_username} settings email '${var_email}'"
    sh ${var_apache_user} -c " php ${var_path_nextcloud}/occ user:setting ${var_username} files quota '${var_quota}'"
    echo "${var_username};${var_password}" >> "${var_result_file}"
done < "$input"
exit 0

But to make the script work I change the part of code in Manager.php in
/home/user/public_html/lib/private/User/Manager.php line around 317
if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) {
to
if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $var_username)) {

But my problem is if possible not make csv but only txt (I think is possible.
And if I add more users to file script check if user exist make all below users and not stop when users exist.

Hello,
I some new edit of script and now all import from file is ok.
User add username, user group (I have 1 group for users), email and password, and read the settings of quota and add it two.
csv file I edit it from (geany text editor not excel) and need to have below columns

‘username’; ‘User Display Name’; ‘User_group’ ; ’ '; ’ '; ’ '; ‘user_email’;

And the updated script :

  #!/bin/bash
var_datum=$(date +"%Y%m%d")
input="impfin.csv" # or any other file name,  you can edit it with text editor (gedit,geany, notepad++) 
#var_apache_user=www-data
var_path_nextcloud=/var/www/nextcloud
var_result_file="${var_datum}_user_create.txt"
while read -r line
do
    echo "Rang: ${line}"
    var_password=$(pwgen 8 -c -n -N 1)
    set -e
    export OC_PASS=$var_password
    echo "${var_password} ${OC_PASS}"
    var_username=$(echo "${line}" | cut -d";" -f2)
    var_name=$(echo "${line}" | cut -d";" -f1)
    var_group1=$(echo "${line}" | cut -d";" -f3)
    var_email=$(echo "${line}" | cut -d";" -f7)
    var_quota=$(echo "${line}" | cut -d";" -f8)
    sh ${var_apache_user} -c "php ${var_path_nextcloud}/occ user:add ${var_username} --password-from-env --group='Merchandising' --display-name='${var_name}'"
    if [ "${var_quota}" != "" ] ;then
    sh ${var_apache_user} -c " php ${var_path_nextcloud}/occ user:setting ${var_username} files quota '${var_quota}'";
   fi
    sh ${var_apache_user} -c " php ${var_path_nextcloud}/occ user:setting ${var_username} settings email '${var_email}'";   
    echo "${var_username};${var_password}" >> "${var_result_file}"
done < "$input"
exit 0

1 Like

Hello,
I found this post User Provisioning API example script
about csv .
If you can you can to try it.
If is working write some steps how you tested it.

Thank you.

ok… i dont find a adress for the csv.
and wich csv formate?

Maybe a little bit late for you, but good to know:
Here is an import-script which import users from a .csv-file:

2 Likes

Since this python script didn’t work for me, I ended up adapting the provided scripts above, and made them available as a gitlab snippet here

2 Likes

I can’t reproduce your issue. More information would be great to fix your problem.

can anyone drop csv example, one line with correct order?

P.S OK, found order in script :smiley:

Hi everyone,
sorry for the silly question, but I’m a newbie … how to run the script?
Thx so much for attention
Ale

Searching on the forum I found this link https://github.com/t-markmann/nc-userimporter
This tool solve every problem …it works like a charm.
Thanks to everyone
Warm regards
Ale