Skip to content

Commit d4e3a98

Browse files
Merge pull request #61 from plesk/task-ayuzhakov-prot-dirs-management
Add protected directories management operations
2 parents 10fb37a + 9e203ac commit d4e3a98

File tree

5 files changed

+247
-0
lines changed

5 files changed

+247
-0
lines changed

src/Api/Operator/ProtectedDirectory.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,113 @@
33

44
namespace PleskX\Api\Operator;
55

6+
use PleskX\Api\Struct\ProtectedDirectory as Struct;
7+
68
class ProtectedDirectory extends \PleskX\Api\Operator
79
{
810

911
protected $_wrapperTag = 'protected-dir';
1012

13+
/**
14+
* @param string $name
15+
* @param integer $siteId
16+
* @param string $header
17+
* @return Struct\Info
18+
*/
19+
public function add($name, $siteId, $header = '')
20+
{
21+
$packet = $this->_client->getPacket();
22+
$info = $packet->addChild($this->_wrapperTag)->addChild('add');
23+
24+
$info->addChild('site-id', $siteId);
25+
$info->addChild('name', $name);
26+
$info->addChild('header', $header);
27+
28+
return new Struct\Info($this->_client->request($packet));
29+
}
30+
31+
/**
32+
* @param string $field
33+
* @param integer|string $value
34+
* @return bool
35+
*/
36+
public function delete($field, $value)
37+
{
38+
return $this->_delete($field, $value, 'delete');
39+
}
40+
41+
/**
42+
* @param string $field
43+
* @param integer|string $value
44+
* @return Struct\DataInfo|false
45+
*/
46+
public function get($field, $value)
47+
{
48+
$items = $this->getAll($field, $value);
49+
return reset($items);
50+
}
51+
52+
/**
53+
* @param string $field
54+
* @param integer|string $value
55+
* @return Struct\DataInfo[]
56+
*/
57+
public function getAll($field, $value)
58+
{
59+
$response = $this->_get('get', $field, $value);
60+
$items = [];
61+
foreach ($response->xpath('//result/data') as $xmlResult) {
62+
$items[] = new Struct\DataInfo($xmlResult);
63+
}
64+
return $items;
65+
}
66+
67+
/**
68+
* @param Struct\Info $protectedDirectory
69+
* @param string $login
70+
* @param string $password
71+
* @return Struct\UserInfo
72+
*/
73+
public function addUser($protectedDirectory, $login, $password)
74+
{
75+
$packet = $this->_client->getPacket();
76+
$info = $packet->addChild($this->_wrapperTag)->addChild('add-user');
77+
78+
$info->addChild('pd-id', $protectedDirectory->id);
79+
$info->addChild('login', $login);
80+
$info->addChild('password', $password);
81+
82+
return new Struct\UserInfo($this->_client->request($packet));
83+
}
84+
85+
/**
86+
* @param string $field
87+
* @param integer|string $value
88+
* @return bool
89+
*/
90+
public function deleteUser($field, $value)
91+
{
92+
return $this->_delete($field, $value, 'delete-user');
93+
}
94+
95+
/**
96+
* @param $command
97+
* @param $field
98+
* @param $value
99+
* @return \PleskX\Api\XmlResponse
100+
*/
101+
private function _get($command, $field, $value)
102+
{
103+
$packet = $this->_client->getPacket();
104+
$getTag = $packet->addChild($this->_wrapperTag)->addChild($command);
105+
106+
$filterTag = $getTag->addChild('filter');
107+
if (!is_null($field)) {
108+
$filterTag->addChild($field, $value);
109+
}
110+
111+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
112+
return $response;
113+
}
114+
11115
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
// Copyright 1999-2019. Plesk International GmbH.
3+
4+
namespace PleskX\Api\Struct\ProtectedDirectory;
5+
6+
use PleskX\Api\Struct;
7+
8+
class DataInfo extends Struct
9+
{
10+
/** @var string */
11+
public $name;
12+
13+
/** @var string */
14+
public $header;
15+
16+
public function __construct($apiResponse)
17+
{
18+
$this->_initScalarProperties($apiResponse, [
19+
'name',
20+
'header',
21+
]);
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
// Copyright 1999-2019. Plesk International GmbH.
3+
4+
namespace PleskX\Api\Struct\ProtectedDirectory;
5+
6+
use PleskX\Api\Struct;
7+
8+
class Info extends Struct
9+
{
10+
/** @var integer */
11+
public $id;
12+
13+
public function __construct($apiResponse)
14+
{
15+
$this->_initScalarProperties($apiResponse, [
16+
'id',
17+
]);
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
// Copyright 1999-2019. Plesk International GmbH.
3+
4+
namespace PleskX\Api\Struct\ProtectedDirectory;
5+
6+
class UserInfo extends \PleskX\Api\Struct
7+
{
8+
/** @var integer */
9+
public $id;
10+
11+
public function __construct($apiResponse)
12+
{
13+
$this->_initScalarProperties($apiResponse, [
14+
'id',
15+
]);
16+
}
17+
}

tests/ProtectedDirectoryTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
// Copyright 1999-2019. Plesk International GmbH.
3+
namespace PleskXTest;
4+
5+
class ProtectedDirectoryTest extends TestCase
6+
{
7+
/** @var \PleskX\Api\Struct\Webspace\Info */
8+
private static $webspace;
9+
10+
public static function setUpBeforeClass()
11+
{
12+
parent::setUpBeforeClass();
13+
static::$webspace = static::_createWebspace();
14+
}
15+
16+
public function testAdd()
17+
{
18+
$protectedDirectory = static::$_client->protectedDirectory()->add('/', static::$webspace->id);
19+
20+
$this->assertIsObject($protectedDirectory);
21+
$this->assertGreaterThan(0, $protectedDirectory->id);
22+
23+
static::$_client->protectedDirectory()->delete('id', $protectedDirectory->id);
24+
}
25+
26+
/**
27+
* @expectedException \PleskX\Api\Exception
28+
* @expectedExceptionCode 1019
29+
*/
30+
public function testAddInvalidDirectory()
31+
{
32+
static::$_client->protectedDirectory()->add('', static::$webspace->id);
33+
}
34+
35+
public function testDelete()
36+
{
37+
$protectedDirectory = static::$_client->protectedDirectory()->add('/', static::$webspace->id);
38+
39+
$result = static::$_client->protectedDirectory()->delete('id', $protectedDirectory->id);
40+
$this->assertTrue($result);
41+
}
42+
43+
public function testGetById()
44+
{
45+
$protectedDirectory = static::$_client->protectedDirectory()->add('test', static::$webspace->id);
46+
47+
$foundDirectory = static::$_client->protectedDirectory()->get('id', $protectedDirectory->id);
48+
$this->assertEquals('test', $foundDirectory->name);
49+
50+
static::$_client->protectedDirectory()->delete('id', $protectedDirectory->id);
51+
}
52+
53+
/**
54+
* @expectedException \PleskX\Api\Exception
55+
* @expectedExceptionCode 1013
56+
*/
57+
public function testGetUnknownDirectory()
58+
{
59+
$nonExistentDirectoryId = 99999999;
60+
static::$_client->protectedDirectory()->get('id', $nonExistentDirectoryId);
61+
}
62+
63+
public function testAddUser()
64+
{
65+
$protectedDirectory = static::$_client->protectedDirectory()->add('/', static::$webspace->id);
66+
67+
$user = static::$_client->protectedDirectory()->addUser($protectedDirectory, 'john', 'secret');
68+
$this->assertGreaterThan(0, $user->id);
69+
70+
static::$_client->protectedDirectory()->delete('id', $protectedDirectory->id);
71+
}
72+
73+
public function testDeleteUser()
74+
{
75+
$protectedDirectory = static::$_client->protectedDirectory()->add('/', static::$webspace->id);
76+
77+
$user = static::$_client->protectedDirectory()->addUser($protectedDirectory, 'john', 'secret');
78+
$result = static::$_client->protectedDirectory()->deleteUser('id', $user->id);
79+
$this->assertTrue($result);
80+
81+
static::$_client->protectedDirectory()->delete('id', $protectedDirectory->id);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)