Skip to content

Commit 67e8514

Browse files
committed
Merge pull request #18 from sschwarz/master
Add support for sudo and creating project for another user
2 parents 3bdeff2 + 0b848c4 commit 67e8514

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

lib/Gitlab/Api/Projects.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public function create($name, array $params = array())
2323

2424
return $this->post('projects', $params);
2525
}
26+
27+
public function createForUser($user_id, $name, array $params = array())
28+
{
29+
$params['name'] = $name;
30+
31+
return $this->post('projects/user/'.urlencode($user_id), $params);
32+
}
2633

2734
public function remove($project_id)
2835
{

lib/Gitlab/Client.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,13 @@ public function api($name)
119119
* @param string $token Gitlab private token
120120
* @param null|string $authMethod One of the AUTH_* class constants
121121
*/
122-
public function authenticate($token, $authMethod = null)
122+
public function authenticate($token, $authMethod = null, $sudo = null)
123123
{
124124
$this->httpClient->addListener(
125125
new AuthListener(
126126
$authMethod,
127-
$token
127+
$token,
128+
$sudo
128129
)
129130
);
130131
}

lib/Gitlab/HttpClient/Listener/AuthListener.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@ class AuthListener implements ListenerInterface
1919
* @var string
2020
*/
2121
private $method;
22+
2223
/**
2324
* @var string
2425
*/
2526
private $token;
2627

28+
/**
29+
* @var string|null
30+
*/
31+
private $sudo;
32+
2733
/**
2834
* @param string $method
2935
* @param string $token
3036
*/
31-
public function __construct($method, $token)
37+
public function __construct($method, $token, $sudo = null)
3238
{
3339
$this->method = $method;
3440
$this->token = $token;
41+
$this->sudo = $sudo;
3542
}
3643

3744
/**
@@ -49,12 +56,19 @@ public function preSend(RequestInterface $request)
4956
switch ($this->method) {
5057
case Client::AUTH_HTTP_TOKEN:
5158
$request->addHeader('private_token: '.$this->token);
59+
if (!is_null($this->sudo)) {
60+
$request->addHeader('SUDO: '.$this->sudo);
61+
}
5262
break;
5363

5464
case Client::AUTH_URL_TOKEN:
5565
$url = $request->getUrl();
56-
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query(array('private_token' => $this->token), '', '&'));
57-
66+
$query=array('private_token' => $this->token);
67+
if (!is_null($this->sudo)) {
68+
$query['sudo'] = $this->sudo;
69+
}
70+
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($query, '', '&'));
71+
5872
$request->fromUrl(new Url($url));
5973
break;
6074
}

0 commit comments

Comments
 (0)