Воспользуйтесь RBAC:
$authManager = Yii::$app->authManager;
$authManager->removeAll();
$guestSignUp = $authManager->createPermission('guest-sign-up');
$authManager->add($guestSignUp);
$userRoleRule = new UserRoleRule();
$authManager->add($userRoleRule);
$guest = $authManager->createRole('guest');
$authManager->add($guest);
$authManager->addChild($guest, $guestSignUp);
А в месте, где нужно влепить ссылку, делаете эту проверку:
if (\Yii::$app->user->can('guest-sign-up')) {
echo Html::a('sign up', ['/sign-up']);
}
Когда захотите закрыть доступ, закомментируйте
$authManager->addChild($guest, $guestSignUp);
и всё.
PS:
<?php
namespace common\components\rbac;
use common\modules\user\models\User;
use yii;
use yii\rbac\Item;
use yii\rbac\Rule;
/**
* Class UserRoleRule
* @package common\components\rbac
*/
class UserRoleRule extends Rule
{
public $name = 'userRoleRule';
public function execute($user, $item, $params)
{
$role = Yii::$app->user->isGuest ? User::ROLE_GUEST : User::identity()->role;
if ($item->name === 'developer') {
return $role == User::ROLE_DEVELOPER;
} elseif ($item->name === 'admin') {
return $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'manager') {
return $role == User::ROLE_MANAGER || $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'author') {
return $role == User::ROLE_AUTHOR || $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'user') {
return $role == User::ROLE_USER || $role >= User::ROLE_ADMIN;
} elseif ($item->name === 'guest') {
return $role == User::ROLE_GUEST || $role >= User::ROLE_ADMIN;
}
return false;
}
}