Add user via occ via php

I try to add a user via occ command out of a PHP script, but there is no way i will find.

This works and i will get the settings of an existing user:

exec('/usr/bin/php74    occ user:setting "user" 2>&1',$test);
print_r($test);

But this not, because occ is expecting the password input by keyboard:

exec('/usr/bin/php74 -d memory_limit=512M   occ user:add  --display-name="Layla Smith"  2>&1',$test);

and the enviroment var cant be set with php:

exec('export OC_PASS=newpassword 2>&1',$test);
exec('/usr/bin/php74 -d memory_limit=512M   occ user:add  --password-from-env --display-name="Layla Smith"  2>&1',$test);

last try, like owncloud with an emailadress, will not work in Nextcloud:

exec('/usr/bin/php74 -d memory_limit=512M   occ user:add  --display-name="Layla Smith"  --email="test@test.de"    layla 2>&1',$test);

is there a way i forgott?
maybe a way thats works?

Perhaps it is not possible to use it in PHP with exec().
Please test all commands first without PHP.
If that works post your commands and perhaps than we find a php-script.

Hi devnull,

thank you for the offer.
I don’t have SSH access to this cloud, so i tried a lot with occ&PHP.
The last way was to pipe the password inside the exec() function.
But this will not work with an enabled save_mode, i read…

Instead i found the Nextcloud User Provisioning API :slight_smile:
with an example for CSV files and cut everything off i do’t need.

2 Likes

Hi @kolja,
I am curious to know how you resolved adding an email address to the user, as I cannot find any information about this online. Thanks!

I hope, that PHP can handle it. Try

exec('export OC_PASS=newpassword; /usr/bin/php74 -d memory_limit=512M occ user:add --password-from-env --display-name="Layla Smith" 2>&1',$test);

or

exec('export OC_PASS=newpassword && /usr/bin/php74 -d memory_limit=512M occ user:add --password-from-env --display-name="Layla Smith" 2>&1',$test);

If you execute the password-setting and adding the user in two “exec” instructions, it will not work, because the password is not persistent set and will be “forgotten”, if the shell command ends.
If you set the password variable and add the user in the same “exec” command, the variable still should be available at occ-command.

The “&&” excutes the occ-command only, if setting the password was successful.