Commit c340b657 authored by Emiliano Balbuena's avatar Emiliano Balbuena Committed by Mark Harding
Browse files

(feat): Domain validation

parent 826fbcf7
Loading
Loading
Loading
Loading
+13 −0
Original line number Original line Diff line number Diff line
@@ -8,6 +8,7 @@ namespace Minds\Controllers\api\v2\pro;


use Exception;
use Exception;
use Minds\Core\Di\Di;
use Minds\Core\Di\Di;
use Minds\Core\Pro\Domain as ProDomain;
use Minds\Core\Pro\Manager;
use Minds\Core\Pro\Manager;
use Minds\Core\Session;
use Minds\Core\Session;
use Minds\Entities\User;
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 {
        try {
            $success = $manager->set($_POST);
            $success = $manager->set($_POST);


+77 −0
Original line number Original line Diff line number Diff line
<?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([]);
    }
}
+22 −0
Original line number Original line Diff line number Diff line
@@ -9,6 +9,7 @@ namespace Minds\Core\Pro;
use Exception;
use Exception;
use Minds\Core\Config;
use Minds\Core\Config;
use Minds\Core\Di\Di;
use Minds\Core\Di\Di;
use Minds\Core\Util\StringValidator;
use Minds\Entities\User;
use Minds\Entities\User;


class Domain
class Domain
@@ -49,6 +50,27 @@ class Domain
        ])->first();
        ])->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 Settings $settings
     * @param User|null $owner
     * @param User|null $owner
+98 −0
Original line number Original line Diff line number Diff line
@@ -75,6 +75,104 @@ class DomainSpec extends ObjectBehavior
            ->shouldReturn(null);
            ->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(
    public function it_should_get_icon(
        Settings $settings,
        Settings $settings,
        User $owner
        User $owner