Skip to content

Commit 25d0ff4

Browse files
author
Jamie Hannaford
committed
Add ability for DBs to be granted to users
1 parent 8937fcc commit 25d0ff4

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

doc/services/database/users.rst

+12
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,15 @@ Check if root user is enabled
6565
6666
// true for yes, false for no
6767
$instance->isRootEnabled();
68+
69+
70+
Grant database access
71+
---------------------
72+
73+
To grant access to one or more databases, you can run:
74+
75+
.. code-block:: php
76+
77+
$user = $instance->user('{userName}');
78+
$user->grantDbAccess(['{dbName1}', '{dbName2}']);
79+

lib/OpenCloud/Database/Resource/User.php

+25
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,29 @@ protected function createJson()
152152
'users' => array($user)
153153
);
154154
}
155+
156+
/**
157+
* Grant access to a set of one or more databases to a user.
158+
*
159+
* @param []string $databases An array of one or more database names that this user will be granted access to. For
160+
* example, ['foo', 'bar'] or ['baz'] are valid inputs.
161+
*
162+
* @return \Guzzle\Http\Message\Response
163+
*/
164+
public function grantDbAccess(array $databases)
165+
{
166+
$json = [];
167+
168+
foreach ($databases as $database) {
169+
$json[] = ['name' => $database];
170+
}
171+
172+
$json = ['databases' => $json];
173+
174+
$url = $this->getUrl('databases');
175+
$headers = self::getJsonHeader();
176+
$body = json_encode($json);
177+
178+
return $this->getClient()->put($url, $headers, $body)->send();
179+
}
155180
}

tests/OpenCloud/Tests/Database/Resource/UserTest.php

+16-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function test__construct()
4646
'OpenCloud\Database\Resource\User', $this->user
4747
);
4848

49-
$u = $this->instance->user('glen', array('one', 'two'));
49+
$u = $this->instance->user('glen', ['one', 'two']);
5050
$this->assertEquals('glen', $u->name);
5151
$this->assertEquals(2, count($u->databases));
5252
}
@@ -84,14 +84,14 @@ public function testAddDatabase()
8484

8585
public function testCreate()
8686
{
87-
$response = $this->user->create(array(
87+
$response = $this->user->create([
8888
'name' => 'FOOBAR',
8989
'password' => 'BAZ',
90-
'databases' => array(
90+
'databases' => [
9191
'foo',
9292
'baz'
93-
)
94-
));
93+
]
94+
]);
9595
$this->assertLessThan(205, $response->getStatusCode());
9696
$this->assertEquals('FOOBAR', $this->user->getName());
9797
$this->assertEquals('BAZ', $this->user->password);
@@ -119,4 +119,15 @@ public function testNameFailsWhenNotSet()
119119
{
120120
$this->instance->user()->getName();
121121
}
122+
123+
public function test_It_Grants_Access_To_Db()
124+
{
125+
$this->user->name = 'foo';
126+
127+
$response = $this->user->grantDbAccess([
128+
'foo', 'bar', 'baz',
129+
]);
130+
131+
$this->isResponse($response);
132+
}
122133
}

0 commit comments

Comments
 (0)