Server: Export feature for user and group list

Created on 15 Mar 2019  路  10Comments  路  Source: nextcloud/server

Is your feature request related to a problem? Please describe.
There seems to be no simple way to export a list of all registered users. Especially on larger instances an external user overview with comments and task automation (like identifying long inactive users and notifying the admin to clean up) can be very handy.

Describe the solution you'd like
A simple button in the user management view, that exports the list to e.g. a csv file (or better: asks which format to export to and which columns should be included).
The same could be done to export a list of all existing groups, especially as the group management features in nextcloud are insufficient thus far.

Additional context
As the count of groups the individual user belongs to can differ a lot, a suitable export format should be chosen.

0. Needs triage enhancement

All 10 comments

I'm surprised nobody has commented on this. A user list export feature is something pretty basic and necessary.

Me too... Due to this missing basic function I've experienced quite an embarrassing situation some time ago. A group manager of one of our user groups needed an overview of all group members.
It resulted in taking several screenshots of the user management view :see_no_evil:

If I would have had more time, a database export might have been possible, but a professional UI shouldn't lack this feature. I wonder how large Nextcloud subscription users with several 1000 users handle this?

+1, i need to emailm all user ... so create a list for Thunderbird and no Export user list function.

I wonder how large Nextcloud subscription users with several 1000 users handle this?

LDAP

i need to emailm all user ... so create a list for Thunderbird and no Export user list function.

<?php

$nextcloudUrl = 'https://nextcloud.test';
$adminUsername = 'admin';
$adminPassword = 'admin';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $nextcloudUrl . '/ocs/v1.php/cloud/users');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, $adminUsername . ':' . $adminPassword);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'OCS-APIRequest: true',
    'Accept: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$data = json_decode(curl_exec($ch), true);

$emails = [];
if (isset($data['ocs']['data']['users'])) {
        $users = $data['ocs']['data']['users'];
        foreach($users as $userId) {
                curl_setopt($ch, CURLOPT_URL, $nextcloudUrl . '/ocs/v1.php/cloud/users/' . $userId);
                $userData = json_decode(curl_exec($ch), true);
                if(isset($userData['ocs']['data']['email'])) {
                        $emails[] = $userData['ocs']['data']['email'];
                }
        }
}

echo implode(',', $emails);

@kesselb Thank you!
Although it's quite slow (more than a minute for ~200 users) it does the job.

I did some minor changes because I don't like saving an unhashed admin password on the server and I needed a vertical list instead of the comma separated one.

Calling this through external sites app is quite convenient :+1:

<?php

if(isset($_POST['submit']))
{
    $nextcloudUrl = $_POST['url'];
    $adminUsername = $_POST['user'];
    $adminPassword = $_POST['password'];

    if(isset($adminPassword) && isset($adminUsername) && isset($nextcloudUrl))
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $nextcloudUrl . '/ocs/v1.php/cloud/users');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERPWD, $adminUsername . ':' . $adminPassword);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'OCS-APIRequest: true',
                'Accept: application/json'
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $data = json_decode(curl_exec($ch), true);

        $emails = [];
        if (isset($data['ocs']['data']['users'])) {
            $users = $data['ocs']['data']['users'];
                foreach($users as $userId) {
            curl_setopt($ch, CURLOPT_URL, $nextcloudUrl . '/ocs/v1.php/cloud/users/' . $userId);
                    $userData = json_decode(curl_exec($ch), true);
                    if(isset($userData['ocs']['data']['email'])) {
                        $emails[] = strtolower($userData['ocs']['data']['email']);
                    }
            }
    }

    echo "<ul><li>".implode('</li><li>', $emails)."</li></ul>";
    $adminPassword = ''; 
    }
}
?>

<form action="#" method="post">
    <input type="text" name="url" placeholder="https://cloud.example.com">
    <input type="text" name="user" placeholder="Admin user name">
    <input type="password" name="password" placeholder="Admin user password">
    <input type="submit" name="submit" value="submit">
</form>

I still think we should add a basic export feature sometimes. But in the meantime calling the api should do the trick.

Although it's quite slow (more than a minute for ~200 users) it does the job.

Yep. Curl is able to do multiple request at once. Probably not 200 but 5 or 10: https://www.php.net/manual/en/function.curl-multi-init.php

@kesselb I would like to enhance the code further, add some options to make it more versatile and publish it on github in a repository.
Would you mind if I use your initial code (see above) as a basis?

@bpcurse I don't mind. There is probably another (python based) script somewhere. Some people at help.nextcloud.com shared it.

The nextcloud-userexport script I made from the initial code provided above can be found here:
https://github.com/bpcurse/nextcloud-userexport
@kesselb Thanks for the support

I hope that it will be useful for some admins.
@derekblankmccoy @ledufakademy

Feedback, feature and pull requests are welcome. Happy holidays!

I have just released an enhanced version (v1.0.0) of the before mentioned script.
https://github.com/bpcurse/nextcloud-userexport/releases/tag/v1.0.0

As it helps as a workaround I hope this isn't seen as hijacking this issue.

Was this page helpful?
0 / 5 - 0 ratings