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

Hello everybody,

I am pretty new to Nextcloud and also to Linux.
I looking to find a way to import users without using the web-interface or adding them manually via occ.

I managed to write a script that reads a csv-file and adds the users in there to groups, adds the email and adds a quota.

The problem is sometimes users are in more, sometimes in less groups.
I managed to add different groups by four variables for four groups. But this only works when each field for the groups is set. When I leave a field empty the script does not work because I guess occ does not accept empty values.
So need somekind of placeholder in the csv-file in the field for the groups that i do not need and so that they are not used in script.

My csv-file is something like this:
surname, name;name;group1;group2;group3;group4;email;quota

This is my script:

#!/bin/bash
var_datum=$(date +"%Y%m%d")
var_user_file="test.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 "Zeile: ${line}"
    var_password=$(pwgen 12 -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_email5=$(echo "${line}" | cut -d";" -f7)
    var_quota=$(echo "${line}" | cut -d";" -f8)
    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}'"
    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 "Benutzer ${var_username} wurde mit Passwort ${var_password} erstellt" >> "${var_result_file}"

What I need is a solution to use a csv-file like above without the need to fill every group-field.
Something like that:
surname, name;name;group1;group2;;group4;email;quota

To use group3 as an example I tried NULL, \N, ‘’ and many other things. But this only creates groups with the values typed in. To be exactly NULL for example creates a group “NULL”. Of course this is not what I want.

Anyone an idea? I would appreciate every kind of help.

Best regards
Tarnari

2 Likes

There is a python client library, there it is perhaps easier to write a script to do what you want:

Unfortunately, I can’t help you with your script though it must be possible. Why don’t you just delete the group NULL?

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 !

3 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:

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

1 Like

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