From ac99bcd7ce70de8f155e65c11c26276d4cd2d68f Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Wed, 3 Jan 2018 03:41:15 -0500 Subject: [PATCH] First pass at getmemberships - pull in user listings and memberships --- getmemberships.php | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 getmemberships.php diff --git a/getmemberships.php b/getmemberships.php new file mode 100755 index 0000000..3494e3c --- /dev/null +++ b/getmemberships.php @@ -0,0 +1,47 @@ +#!/usr/bin/php +setCheckSslCertificate(true); + +$limit = 100; +$offset = 0; + +$projectList = getProjectList($limit, $offset, $client); +print_r($getProjectList); +die; +$userList = getUserList($limit, $offset, $client); +$membershipMapping = getUserMemberships($userList, $client); +//print_r($membershipMapping); + +/* + * Returns an array of all users. Key is user ID, value is email address. + */ +function getUserList($limit, $offset, $client) { + // We'll set a more realistic total user count after the first loop. + $totalUsers = 99999; + // Get a complete user list with email address. + while ($offset + $limit < $totalUsers) { + $listing = $client->user->all([ + 'limit' => $limit, + 'offset' => $offset, + ]); + foreach ($listing['users'] as $user) { + $users[$user['id']] = $user['mail']; + } + $totalUsers = $listing['total_count']; + $offset += $limit; + }; + return $users; +} +// Get the memberships of each user. It's sad that we can't do this in bulk. +function getUserMemberships($users, $client) { + foreach ($users as $id => $email) { + $userDetails = $client->user->show($id, ['include' => ['memberships']])['user']; + if (count($userDetails['memberships']) == 1) { + $membershipMapping[$email] = $userDetails['memberships'][0]['project']['id']; + } + } + return $membershipMapping; +}