Skip to content

Commit

Permalink
php-cs-fixer fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Milesi committed Dec 29, 2022
1 parent fdb53cf commit 406fa59
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/User/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ protected function initUrlRoutes(WebApplication $app)

/**
* Initializes web url for rest routes.
* @param WebApplication $app
* @param WebApplication $app
* @throws InvalidConfigException
*/
protected function initUrlRestRoutes(WebApplication $app)
Expand Down
112 changes: 56 additions & 56 deletions src/User/Controller/api/v1/AdminController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the 2amigos/yii2-usuario project.
*
* (c) 2amigOS! <http://2amigos.us/>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Da\User\Controller\api\v1;

use Da\User\Event\UserEvent;
Expand Down Expand Up @@ -27,8 +36,8 @@
/**
* Controller that provides REST APIs to manage users.
* This controller is equivalent to `Da\User\Controller\AdminController`.
*
* TODO:
*
* TODO:
* - `Info` and `SwitchIdentity` actions were not developed yet.
* - `Assignments` action implements only GET method (POST method not developed yet).
*/
Expand All @@ -45,7 +54,7 @@ class AdminController extends ActiveController
* {@inheritdoc}
*/
public $updateScenario = 'update';

/**
* {@inheritdoc}
*/
Expand All @@ -58,10 +67,10 @@ class AdminController extends ActiveController

/**
* AdminController constructor.
* @param string $id
* @param Module $module
* @param string $id
* @param Module $module
* @param UserQuery $userQuery
* @param array $config
* @param array $config
*/
public function __construct($id, Module $module, UserQuery $userQuery, array $config = [])
{
Expand All @@ -87,28 +96,9 @@ public function actions()
{
// Get and then remove some default actions
$actions = parent::actions();
unset($actions['create']);
unset($actions['update']);
unset($actions['delete']);
return $actions;
}
unset($actions['create'], $actions['update'], $actions['delete']);

/**
* {@inheritdoc}
*/
protected function verbs()
{
// Get parent verbs
$verbs = parent::verbs();

// Add new verbs and return
$verbs['update-profile'] = ['PUT', 'PATCH'];
$verbs['assignments'] = ['GET'];
$verbs['confirm'] = ['PUT', 'PATCH'];
$verbs['block'] = ['PUT', 'PATCH'];
$verbs['password-reset'] = ['PUT', 'PATCH'];
$verbs['force-password-change'] = ['PUT', 'PATCH'];
return $verbs;
return $actions;
}

/**
Expand Down Expand Up @@ -149,31 +139,31 @@ public function checkAccess($action, $model = null, $params = [])
}
}


/**
* Override beforeAction. If the api is called with parameter username get the id of the user and set it in query params
* @param mixed $action
*/
public function beforeAction($action)
{
if($action == 'create'){
if ($action == 'create') {
return parent::beforeAction($action);
}

$id = Yii::$app->request->getQueryParam('id');
if(!is_null($id)){
if (!is_null($id)) {
return parent::beforeAction($action);
}

$username = Yii::$app->request->getQueryParam('username');
if(is_null($username)){
if (is_null($username)) {
return parent::beforeAction($action);
}

$user = $this->userQuery->where(['username' => $username])->one();
if (is_null($user)) { // Check user, so ` $username` parameter
return parent::beforeAction($action);
}
}

$params = Yii::$app->request->getQueryParams();
$params['id'] = $user->id;
Yii::$app->request->setQueryParams($params);
Expand Down Expand Up @@ -247,7 +237,7 @@ public function actionUpdate($id = null)
}
return $user;
}

/**
* Delete a user.
* @param int $id ID of the user.
Expand Down Expand Up @@ -278,8 +268,7 @@ public function actionDelete($id)
if ($user->delete()) {
$this->trigger(ActiveRecord::EVENT_AFTER_DELETE, $event);
Yii::$app->getResponse()->setStatusCode(204); // 204 = No Content
}
else {
} else {
$this->throwServerError();
}
}
Expand Down Expand Up @@ -369,9 +358,8 @@ public function actionConfirm($id)
$this->trigger(UserEvent::EVENT_AFTER_CONFIRMATION, $event);
return $user;
}
else {
$this->throwServerError();
}

$this->throwServerError();
}

/**
Expand Down Expand Up @@ -404,9 +392,8 @@ public function actionBlock($id = null)
if ($this->make(UserBlockService::class, [$user, $event, $this])->run() || $user->hasErrors()) {
return $user;
}
else {
$this->throwServerError();
}

$this->throwServerError();
}

/**
Expand All @@ -430,11 +417,10 @@ public function actionPasswordReset($id)
if ($this->make(PasswordRecoveryService::class, [$user->email, $mailService])->run()) {
return $user;
}
else {
$this->throwServerError();
}

$this->throwServerError();
}

/**
* Forces the user to change password at next login.
* @param int $id ID of the user.
Expand All @@ -455,15 +441,32 @@ public function actionForcePasswordChange($id)
if ($this->make(PasswordExpireService::class, [$user])->run()) {
return $user;
}
else {
$this->throwServerError();
}

$this->throwServerError();
}

/**
* {@inheritdoc}
*/
protected function verbs()
{
// Get parent verbs
$verbs = parent::verbs();

// Add new verbs and return
$verbs['update-profile'] = ['PUT', 'PATCH'];
$verbs['assignments'] = ['GET'];
$verbs['confirm'] = ['PUT', 'PATCH'];
$verbs['block'] = ['PUT', 'PATCH'];
$verbs['password-reset'] = ['PUT', 'PATCH'];
$verbs['force-password-change'] = ['PUT', 'PATCH'];
return $verbs;
}

/**
* Handle server error (with default Yii2 response).
* @return void
* @throws ServerErrorHttpException
* @return void
*/
protected function throwServerError()
{
Expand All @@ -472,14 +475,11 @@ protected function throwServerError()

/**
* Handle 404 error for user (usually if the entered ID is not valid).
* @return void
* @throws NotFoundHttpException
* @return void
*/
protected function throwUser404()
{
throw new NotFoundHttpException(Yii::t('usuario', 'User not found.'));
}



}
}
5 changes: 2 additions & 3 deletions src/User/Form/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,9 @@ public function getUser()
{
return $this->user;
}



/**
* @param IdentityInterface $user
* @param IdentityInterface $user
* @return User
*/
public function setUser(IdentityInterface $user)
Expand Down
4 changes: 2 additions & 2 deletions src/User/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ class Module extends BaseModule
public $enableRestApi = false;
/**
* @var string Which class to use as authenticator for REST API.
* Possible values: `HttpBasicAuth`, `HttpBearerAuth` or `QueryParamAuth`.
* Default value = `yii\filters\auth\QueryParamAuth` class, therefore access tokens are sent as query parameter; for instance: `https://example.com/users?access-token=xxxxxxxx`.
* Possible values: `HttpBasicAuth`, `HttpBearerAuth` or `QueryParamAuth`.
* Default value = `yii\filters\auth\QueryParamAuth` class, therefore access tokens are sent as query parameter; for instance: `https://example.com/users?access-token=xxxxxxxx`.
*/
public $authenticatorClass = 'yii\filters\auth\QueryParamAuth';
/**
Expand Down

0 comments on commit 406fa59

Please sign in to comment.