From 8218263295eb209f1cc061b47126f60b6e25a927 Mon Sep 17 00:00:00 2001 From: "guillermo.fisher" Date: Fri, 26 Dec 2014 11:03:48 -0500 Subject: [PATCH 1/2] Added user suspension & unsuspension, resolves #207. --- doc/users.md | 16 +++++++++++++++ lib/Github/Api/User.php | 28 ++++++++++++++++++++++++++ test/Github/Tests/Api/UserTest.php | 32 ++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/doc/users.md b/doc/users.md index 810226af55b..6824a482c03 100644 --- a/doc/users.md +++ b/doc/users.md @@ -174,3 +174,19 @@ $emails = $client->api('current_user')->emails()->remove(array('first@provider.o ``` Return an array of the authenticated user emails. + +### Suspend a user (Enterprise only) + +> Requires [authentication](security.md). + +```php +$client->api('user')->suspend('ornicar'); +``` + +### Unsuspend a user (Enterprise only) + +> Requires [authentication](security.md). + +```php +$client->api('user')->unsuspend('ornicar'); +``` \ No newline at end of file diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index 20323934fca..0ab7562b8aa 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -182,4 +182,32 @@ public function publicEvents($username) { return $this->get('users/'.rawurlencode($username) . '/events/public'); } + + /** + * Suspend a user + * + * @link https://developer.github.com/v3/users/administration/#suspend-a-user + * + * @param string $username + * + * @return array + */ + public function suspend($username) + { + return $this->put('users/'.rawurldecode($username).'/suspended', array('Content-Length' => 0)); + } + + /** + * Unsuspend a user + * + * @link https://developer.github.com/v3/users/administration/#unsuspend-a-user + * + * @param string $username + * + * @return array + */ + public function unsuspend($username) + { + return $this->delete('users/'.rawurldecode($username).'/suspended'); + } } diff --git a/test/Github/Tests/Api/UserTest.php b/test/Github/Tests/Api/UserTest.php index dec69d7944b..ac40fe7ecc5 100644 --- a/test/Github/Tests/Api/UserTest.php +++ b/test/Github/Tests/Api/UserTest.php @@ -161,6 +161,38 @@ public function shouldGetUserGists() $this->assertEquals($expectedArray, $api->gists('l3l0')); } + /** + * @test + */ + public function shouldSuspendUser() + { + $expectedArray = array(); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('users/l3l0/suspended') + ->will($this->returnValue($expectedArray)); + $this->assertEquals($expectedArray, $api->suspend('l3l0')); + } + + /** + * @test + */ + public function shouldUnsuspendUser() + { + $expectedArray = array(); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('users/l3l0/suspended') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->unsuspend('l3l0')); + } + + protected function getApiClass() { return 'Github\Api\User'; From 81105b8a78e3b44b3cbec305291688f3569e24b3 Mon Sep 17 00:00:00 2001 From: "guillermo.fisher" Date: Fri, 2 Jan 2015 10:38:15 -0500 Subject: [PATCH 2/2] Moved methods to `UserAdmin` class. --- doc/enterprise.md | 21 ++++++++- doc/users.md | 16 ------- lib/Github/Api/Enterprise.php | 9 ++++ lib/Github/Api/Enterprise/UserAdmin.php | 36 ++++++++++++++++ lib/Github/Api/User.php | 28 ------------ .../Tests/Api/Enterprise/UserAdminTest.php | 43 +++++++++++++++++++ test/Github/Tests/Api/EnterpriseTest.php | 10 +++++ test/Github/Tests/Api/UserTest.php | 32 -------------- 8 files changed, 118 insertions(+), 77 deletions(-) create mode 100644 lib/Github/Api/Enterprise/UserAdmin.php create mode 100644 test/Github/Tests/Api/Enterprise/UserAdminTest.php diff --git a/doc/enterprise.md b/doc/enterprise.md index b4673061e74..9e38f13dce6 100644 --- a/doc/enterprise.md +++ b/doc/enterprise.md @@ -3,6 +3,7 @@ Provides information about a GitHub Enterprise installation. Wraps [GitHub Enterprise API](http://developer.github.com/v3/enterprise/). +### Configuration In order to configure the client to point to a GitHub Enterprise installation, do the following: ```php @@ -20,5 +21,23 @@ $client->setEnterpriseUrl('https://ghe.host'); $repositories = $client->api('user')->repositories('ornicar'); ``` -To use the Stats and License APIs, you will need to authenticate using a GitHub Enterprise site admin account. +### Authentication +The Admin Stats, License, and User Administration API endpoints are only accessible to GitHub Enterprise site administrators. The Management Console API endpoints are only accessible via the Management Console password. +### User Administration + +#### Suspend a user (Enterprise only) + +> Requires [authentication](security.md). + +```php +$client->api('enterprise')->userAdmin()->suspend('ornicar'); +``` + +#### Unsuspend a user (Enterprise only) + +> Requires [authentication](security.md). + +```php +$client->api('enterprise')->userAdmin()->unsuspend('ornicar'); +``` diff --git a/doc/users.md b/doc/users.md index 6824a482c03..810226af55b 100644 --- a/doc/users.md +++ b/doc/users.md @@ -174,19 +174,3 @@ $emails = $client->api('current_user')->emails()->remove(array('first@provider.o ``` Return an array of the authenticated user emails. - -### Suspend a user (Enterprise only) - -> Requires [authentication](security.md). - -```php -$client->api('user')->suspend('ornicar'); -``` - -### Unsuspend a user (Enterprise only) - -> Requires [authentication](security.md). - -```php -$client->api('user')->unsuspend('ornicar'); -``` \ No newline at end of file diff --git a/lib/Github/Api/Enterprise.php b/lib/Github/Api/Enterprise.php index 48f90f06b27..c23171a6614 100644 --- a/lib/Github/Api/Enterprise.php +++ b/lib/Github/Api/Enterprise.php @@ -5,6 +5,7 @@ use Github\Api\Enterprise\ManagementConsole; use Github\Api\Enterprise\Stats; use Github\Api\Enterprise\License; +use Github\Api\Enterprise\UserAdmin; /** * Getting information about a GitHub Enterprise instance. @@ -38,4 +39,12 @@ public function console() { return new ManagementConsole($this->client); } + + /** + * @return UserAdmin + */ + public function userAdmin() + { + return new UserAdmin($this->client); + } } diff --git a/lib/Github/Api/Enterprise/UserAdmin.php b/lib/Github/Api/Enterprise/UserAdmin.php new file mode 100644 index 00000000000..d01a975515c --- /dev/null +++ b/lib/Github/Api/Enterprise/UserAdmin.php @@ -0,0 +1,36 @@ +put('users/'.rawurldecode($username).'/suspended', array('Content-Length' => 0)); + } + + /** + * Unsuspend a user + * + * @link https://developer.github.com/v3/users/administration/#unsuspend-a-user + * + * @param string $username + * + * @return array + */ + public function unsuspend($username) + { + return $this->delete('users/'.rawurldecode($username).'/suspended'); + } +} diff --git a/lib/Github/Api/User.php b/lib/Github/Api/User.php index 0ab7562b8aa..20323934fca 100644 --- a/lib/Github/Api/User.php +++ b/lib/Github/Api/User.php @@ -182,32 +182,4 @@ public function publicEvents($username) { return $this->get('users/'.rawurlencode($username) . '/events/public'); } - - /** - * Suspend a user - * - * @link https://developer.github.com/v3/users/administration/#suspend-a-user - * - * @param string $username - * - * @return array - */ - public function suspend($username) - { - return $this->put('users/'.rawurldecode($username).'/suspended', array('Content-Length' => 0)); - } - - /** - * Unsuspend a user - * - * @link https://developer.github.com/v3/users/administration/#unsuspend-a-user - * - * @param string $username - * - * @return array - */ - public function unsuspend($username) - { - return $this->delete('users/'.rawurldecode($username).'/suspended'); - } } diff --git a/test/Github/Tests/Api/Enterprise/UserAdminTest.php b/test/Github/Tests/Api/Enterprise/UserAdminTest.php new file mode 100644 index 00000000000..42e6358bfff --- /dev/null +++ b/test/Github/Tests/Api/Enterprise/UserAdminTest.php @@ -0,0 +1,43 @@ +getApiMock(); + $api->expects($this->once()) + ->method('put') + ->with('users/l3l0/suspended') + ->will($this->returnValue($expectedArray)); + $this->assertEquals($expectedArray, $api->suspend('l3l0')); + } + + /** + * @test + */ + public function shouldUnsuspendUser() + { + $expectedArray = array(); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('users/l3l0/suspended') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->unsuspend('l3l0')); + } + + protected function getApiClass() + { + return 'Github\Api\Enterprise\UserAdmin'; + } +} \ No newline at end of file diff --git a/test/Github/Tests/Api/EnterpriseTest.php b/test/Github/Tests/Api/EnterpriseTest.php index cccd446c6d9..319e6e16232 100644 --- a/test/Github/Tests/Api/EnterpriseTest.php +++ b/test/Github/Tests/Api/EnterpriseTest.php @@ -34,6 +34,16 @@ public function shouldGetEnterpriseManagementConsoleApiObject() $this->assertInstanceOf('Github\Api\Enterprise\ManagementConsole', $api->console()); } + /** + * @test + */ + public function shouldGetEnterpriseUserAdminApiObject() + { + $api = $this->getApiMock(); + + $this->assertInstanceOf('Github\Api\Enterprise\UserAdmin', $api->userAdmin()); + } + protected function getApiClass() { return 'Github\Api\Enterprise'; diff --git a/test/Github/Tests/Api/UserTest.php b/test/Github/Tests/Api/UserTest.php index ac40fe7ecc5..dec69d7944b 100644 --- a/test/Github/Tests/Api/UserTest.php +++ b/test/Github/Tests/Api/UserTest.php @@ -161,38 +161,6 @@ public function shouldGetUserGists() $this->assertEquals($expectedArray, $api->gists('l3l0')); } - /** - * @test - */ - public function shouldSuspendUser() - { - $expectedArray = array(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('put') - ->with('users/l3l0/suspended') - ->will($this->returnValue($expectedArray)); - $this->assertEquals($expectedArray, $api->suspend('l3l0')); - } - - /** - * @test - */ - public function shouldUnsuspendUser() - { - $expectedArray = array(); - - $api = $this->getApiMock(); - $api->expects($this->once()) - ->method('delete') - ->with('users/l3l0/suspended') - ->will($this->returnValue($expectedArray)); - - $this->assertEquals($expectedArray, $api->unsuspend('l3l0')); - } - - protected function getApiClass() { return 'Github\Api\User';