...
 
Commits (2)
......@@ -8,6 +8,7 @@ namespace Minds\Controllers\api\v2\pro;
use Exception;
use Minds\Core\Di\Di;
use Minds\Core\Pro\Domain as ProDomain;
use Minds\Core\Pro\Manager;
use Minds\Core\Session;
use Minds\Entities\User;
......@@ -81,6 +82,18 @@ class settings implements Interfaces\Api
]);
}
if (isset($_POST['domain'])) {
/** @var ProDomain $proDomain */
$proDomain = Di::_()->get('Pro\Domain');
if (!$proDomain->isAvailable($_POST['domain'], (string) $user->guid)) {
return Factory::response([
'status' => 'error',
'message' => 'This domain is taken',
]);
}
}
try {
$success = $manager->set($_POST);
......
<?php
/**
* domain
* @author edgebal
*/
namespace Minds\Controllers\api\v2\pro\settings;
use Exception;
use Minds\Core\Di\Di;
use Minds\Core\Pro\Domain as ProDomain;
use Minds\Core\Session;
use Minds\Entities\User;
use Minds\Interfaces;
use Minds\Api\Factory;
class domain implements Interfaces\Api
{
/**
* Equivalent to HTTP GET method
* @param array $pages
* @return mixed|null
* @throws Exception
*/
public function get($pages)
{
$user = Session::getLoggedinUser();
if (isset($pages[0]) && $pages[0]) {
if (!Session::isAdmin()) {
return Factory::response([
'status' => 'error',
'message' => 'You are not authorized',
]);
}
$user = new User($pages[0]);
}
/** @var ProDomain $proDomain */
$proDomain = Di::_()->get('Pro\Domain');
return Factory::response([
'isValid' => $proDomain->isAvailable($_GET['domain'], (string) $user->guid)
]);
}
/**
* Equivalent to HTTP POST method
* @param array $pages
* @return mixed|null
*/
public function post($pages)
{
return Factory::response([]);
}
/**
* Equivalent to HTTP PUT method
* @param array $pages
* @return mixed|null
*/
public function put($pages)
{
return Factory::response([]);
}
/**
* Equivalent to HTTP DELETE method
* @param array $pages
* @return mixed|null
*/
public function delete($pages)
{
return Factory::response([]);
}
}
......@@ -9,6 +9,7 @@ namespace Minds\Core\Pro;
use Exception;
use Minds\Core\Config;
use Minds\Core\Di\Di;
use Minds\Core\Util\StringValidator;
use Minds\Entities\User;
class Domain
......@@ -49,6 +50,27 @@ class Domain
])->first();
}
/**
* @param string $domain
* @param string $userGuid
* @return bool|null
*/
public function isAvailable(string $domain, string $userGuid): ?bool
{
$rootDomains = $this->config->get('pro')['root_domains'] ?? [];
if (in_array(strtolower($domain), $rootDomains, true)) {
return false;
}
if (!StringValidator::isDomain($domain)) {
return null;
}
$settings = $this->lookup($domain);
return !$settings || ((string) $settings->getUserGuid() === $userGuid);
}
/**
* @param Settings $settings
* @param User|null $owner
......
......@@ -75,6 +75,104 @@ class DomainSpec extends ObjectBehavior
->shouldReturn(null);
}
public function it_should_check_if_domain_is_unavailable(
Response $getListResponse,
Settings $settings
) {
$this->config->get('pro')
->shouldBeCalled()
->willReturn([
'root_domains' => ['phpspec.test']
]);
$this->repository->getList([
'domain' => 'phpspec-test.com'
])
->shouldBeCalled()
->willReturn($getListResponse);
$getListResponse->first()
->shouldBeCalled()
->willReturn($settings);
$settings->getUserGuid()
->shouldBeCalled()
->willReturn(1001);
$this
->isAvailable('phpspec-test.com', 1000)
->shouldReturn(false);
}
public function it_should_check_if_domain_is_available_if_same_owner(
Response $getListResponse,
Settings $settings
) {
$this->config->get('pro')
->shouldBeCalled()
->willReturn([
'root_domains' => ['phpspec.test']
]);
$this->repository->getList([
'domain' => 'phpspec-test.com'
])
->shouldBeCalled()
->willReturn($getListResponse);
$getListResponse->first()
->shouldBeCalled()
->willReturn($settings);
$settings->getUserGuid()
->shouldBeCalled()
->willReturn(1000);
$this
->isAvailable('phpspec-test.com', 1000)
->shouldReturn(true);
}
public function it_should_check_if_domain_is_available(
Response $getListResponse
) {
$this->config->get('pro')
->shouldBeCalled()
->willReturn([
'root_domains' => ['phpspec.test']
]);
$this->repository->getList([
'domain' => 'phpspec-test.com'
])
->shouldBeCalled()
->willReturn($getListResponse);
$getListResponse->first()
->shouldBeCalled()
->willReturn(null);
$this
->isAvailable('phpspec-test.com', 1000)
->shouldReturn(true);
}
public function it_should_return_as_unavailable_if_root_domain()
{
$this->config->get('pro')
->shouldBeCalled()
->willReturn([
'root_domains' => ['phpspec.test']
]);
$this->repository->getList(Argument::cetera())
->shouldNotBeCalled();
$this
->isAvailable('phpspec.test', 1000)
->shouldReturn(false);
}
public function it_should_get_icon(
Settings $settings,
User $owner
......