From e35f6d7a52b93aba906e508c8dfb849f2e64c5fa Mon Sep 17 00:00:00 2001 From: Samuel Schwarz Date: Tue, 5 Nov 2013 20:42:58 +0100 Subject: [PATCH 1/2] Add support for sudo and creating project for another user --- lib/Gitlab/Api/Projects.php | 7 +++++++ lib/Gitlab/Client.php | 5 +++-- lib/Gitlab/HttpClient/Listener/AuthListener.php | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Gitlab/Api/Projects.php b/lib/Gitlab/Api/Projects.php index 6e8dca3ab..31ff53b5d 100644 --- a/lib/Gitlab/Api/Projects.php +++ b/lib/Gitlab/Api/Projects.php @@ -23,6 +23,13 @@ public function create($name, array $params = array()) return $this->post('projects', $params); } + + public function createForUser($user_id, $name, array $params = array()) + { + $params['name'] = $name; + + return $this->post('projects/user/'.urlencode($user_id), $params); + } public function remove($project_id) { diff --git a/lib/Gitlab/Client.php b/lib/Gitlab/Client.php index b16195570..aaab56c08 100644 --- a/lib/Gitlab/Client.php +++ b/lib/Gitlab/Client.php @@ -119,12 +119,13 @@ public function api($name) * @param string $token Gitlab private token * @param null|string $authMethod One of the AUTH_* class constants */ - public function authenticate($token, $authMethod = null) + public function authenticate($token, $authMethod = null, $sudo=null) { $this->httpClient->addListener( new AuthListener( $authMethod, - $token + $token, + $sudo ) ); } diff --git a/lib/Gitlab/HttpClient/Listener/AuthListener.php b/lib/Gitlab/HttpClient/Listener/AuthListener.php index ab40a4e74..7afaa09f8 100644 --- a/lib/Gitlab/HttpClient/Listener/AuthListener.php +++ b/lib/Gitlab/HttpClient/Listener/AuthListener.php @@ -23,15 +23,20 @@ class AuthListener implements ListenerInterface * @var string */ private $token; + /** + * @var string|null + */ + private $sudo; /** * @param string $method * @param string $token */ - public function __construct($method, $token) + public function __construct($method, $token, $sudo=null) { $this->method = $method; $this->token = $token; + $this->sudo = $sudo; } /** @@ -49,12 +54,17 @@ public function preSend(RequestInterface $request) switch ($this->method) { case Client::AUTH_HTTP_TOKEN: $request->addHeader('private_token: '.$this->token); + if(!is_null($this->sudo)){ + $request->addHeader('SUDO: '.$this->sudo); + } break; case Client::AUTH_URL_TOKEN: $url = $request->getUrl(); $url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query(array('private_token' => $this->token), '', '&')); - + if(!is_null($this->sudo)){ + $url.="&".utf8_encode(http_build_query(array('sudo' => $this->sudo), '', '&')); + } $request->fromUrl(new Url($url)); break; } From 0b848c4a624f38d6decb4818d74e5122e5034d56 Mon Sep 17 00:00:00 2001 From: Samuel Schwarz Date: Wed, 6 Nov 2013 15:58:21 +0100 Subject: [PATCH 2/2] Amendments --- lib/Gitlab/Client.php | 2 +- lib/Gitlab/HttpClient/Listener/AuthListener.php | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/Gitlab/Client.php b/lib/Gitlab/Client.php index aaab56c08..4277355fb 100644 --- a/lib/Gitlab/Client.php +++ b/lib/Gitlab/Client.php @@ -119,7 +119,7 @@ public function api($name) * @param string $token Gitlab private token * @param null|string $authMethod One of the AUTH_* class constants */ - public function authenticate($token, $authMethod = null, $sudo=null) + public function authenticate($token, $authMethod = null, $sudo = null) { $this->httpClient->addListener( new AuthListener( diff --git a/lib/Gitlab/HttpClient/Listener/AuthListener.php b/lib/Gitlab/HttpClient/Listener/AuthListener.php index 7afaa09f8..8a42a4885 100644 --- a/lib/Gitlab/HttpClient/Listener/AuthListener.php +++ b/lib/Gitlab/HttpClient/Listener/AuthListener.php @@ -19,10 +19,12 @@ class AuthListener implements ListenerInterface * @var string */ private $method; + /** * @var string */ private $token; + /** * @var string|null */ @@ -32,7 +34,7 @@ class AuthListener implements ListenerInterface * @param string $method * @param string $token */ - public function __construct($method, $token, $sudo=null) + public function __construct($method, $token, $sudo = null) { $this->method = $method; $this->token = $token; @@ -54,17 +56,19 @@ public function preSend(RequestInterface $request) switch ($this->method) { case Client::AUTH_HTTP_TOKEN: $request->addHeader('private_token: '.$this->token); - if(!is_null($this->sudo)){ + if (!is_null($this->sudo)) { $request->addHeader('SUDO: '.$this->sudo); } break; case Client::AUTH_URL_TOKEN: $url = $request->getUrl(); - $url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query(array('private_token' => $this->token), '', '&')); - if(!is_null($this->sudo)){ - $url.="&".utf8_encode(http_build_query(array('sudo' => $this->sudo), '', '&')); + $query=array('private_token' => $this->token); + if (!is_null($this->sudo)) { + $query['sudo'] = $this->sudo; } + $url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($query, '', '&')); + $request->fromUrl(new Url($url)); break; }