Trait luya\admin\traits\AdminRestBehaviorTrait
A trait for LUYA admin rest behaviors.
Implemented by
- {{luya\admin\base\RestActiveController}}
- {{luya\admin\base\RestController}}
Public Methods
Method | Description | Defined By |
---|---|---|
behaviors() | Override the default {{yii\rest\Controller::behaviors()}} method. | luya\admin\traits\AdminRestBehaviorTrait |
canApiUserAccess() | If the current user is an API user this action might be not permitted. As Api Users also acts as proxy for JWT authenticated users sensitive informations could be exposed. | luya\admin\traits\AdminRestBehaviorTrait |
getCompositeAuthMethods() | Return all Auth methods for Composite Auth. | luya\admin\traits\AdminRestBehaviorTrait |
init() | Initializes the object. | luya\admin\traits\AdminRestBehaviorTrait |
isActionAuthOptional() | Wether the given action id does not required authentication or not. | luya\admin\traits\AdminRestBehaviorTrait |
userAuthClass() | Get the current user auth object. | luya\admin\traits\AdminRestBehaviorTrait |
Method Details
Override the default {{yii\rest\Controller::behaviors()}} method.
The following changes are differ to the base implementation:
- If {{luya\rest\UserBehaviorInterface}} is not implemented, the
authenticator
behavior ({{yii\filters\auth\CompositeAuth}}) is removed. - If {{luya\rest\UserBehaviorInterface}} is implemented, the
authenticator
behavior ({{yii\filters\auth\CompositeAuth}}) is enabled. - If {{luya\rest\UserBehaviorInterface}} is implemented, the
contentNegotiator
behavior ({{yii\filters\ContentNegotiator}}) is enabled. - The
rateLimiter
behavior filter is removed by default.
public array behaviors ( ) | ||
return | array |
Returns an array with registered behavior filters based on the implementation type. |
---|
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors[] = [
'class' => UserRequestBehavior::class,
];
return $behaviors;
}
If the current user is an API user this action might be not permitted. As Api Users also acts as proxy for JWT authenticated users sensitive informations could be exposed.
For example a JWT authenticated user proxied trough Api User could access admin/api-admin-user/session
as the {{luya\admin\apis\UserController::actionSession()}} is only secured through authentification and not
trough a given permission (let's say "view my session data").
As Api Users are not allowed to login the don't need access to those generic admin ui API endpoints, this method checks if the current user is an api user and therefore restricsts the access to such calls, unless the property {{luya\admin\Module::$apiUserAllowActionsWithoutPermissions}} is enabled.
public void canApiUserAccess ( ) |
public function canApiUserAccess()
{
if ($this->userAuthClass()->identity->is_api_user && !$this->_module->apiUserAllowActionsWithoutPermissions) {
throw new ForbiddenHttpException("This controller ({$this->id}) action is forbidden for API users unless apiUserAllowActionsWithoutPermissions is enabled in admin module config.");
}
}
Return all Auth methods for Composite Auth.
public array getCompositeAuthMethods ( ) |
public function getCompositeAuthMethods()
{
$methods = parent::getCompositeAuthMethods();
// if the jwt component is registered, authentication will be enabled.
if (Yii::$app->get('jwt', false)) {
array_unshift($methods, [
'class' => 'bizley\jwt\JwtHttpBearerAuth',
'auth' => [Yii::$app->jwt, 'authenticateUser'],
'throwException' => false,
]);
}
return $methods;
}
Initializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
public void init ( ) |
public function init()
{
parent::init();
$this->_module = AdminModule::getInstance();
$this->enableCors = $this->_module->cors;
$this->jsonCruft = $this->_module->jsonCruft;
// pass all the luya admin languages to the content negoiator, the default language must be the first
// element in the array.
$this->languages = [Yii::$app->composition->langShortCode];
foreach (Yii::$app->adminLanguage->languages as $lang) {
array_push($this->languages, $lang['short_code']);
}
// disable session for rest usage
Yii::$app->adminuser->enableSession = false;
}
Wether the given action id does not required authentication or not.
{@since 3.6.0} this will also return true when cors is enabled and the request method is OPTIONS. As the
optional
actions list is passed to the authenticator behavior, this is the place where authentication happens and is done anyhow beforeisActionAuthOptional() is used in
beforeAction()` checks.
public boolean isActionAuthOptional ( $actionId ) | ||
$actionId | string |
public function isActionAuthOptional($actionId)
{
if ($this->enableCors && Yii::$app->request->isOptions) {
return true;
}
return in_array($actionId, $this->authOptional);
}
Get the current user auth object.
public luya\admin\components\AdminUser userAuthClass ( ) |
public function userAuthClass()
{
return Yii::$app->adminuser;
}