File: /home/aliazzsr/api.crm.vqode.com/controllers/UserController.php
<?php
namespace app\controllers;
use app\components\actions\PatchAction;
use app\components\actions\UserAvatarAction;
use app\components\actions\UsersStatisticsAction;
use app\components\BaseActiveController;
use app\models\core\UserFile;
use app\models\enums\PermissionEnum;
/**
* Class UserController
* @package app\controllers
*/
class UserController extends BaseActiveController
{
public $modelClass = 'app\models\core\User';
protected static $permissionRequired = PermissionEnum::USERS;
/**
* @inheritdoc
*/
public function actions()
{
$actions = parent::actions();
$actions['statistics'] = [
'class' => UsersStatisticsAction::className(),
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
$actions['patch'] = [
'class' => PatchAction::className(),
'modelClass' => $this->modelClass,
'checkAccess' => [$this, 'checkAccess'],
];
$actions['image'] = [
'class' => UserAvatarAction::className(),
'modelClass' => UserFile::className(),
];
return $actions;
}
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$app = \Yii::$app;
if ('image' === $app->controller->action->id && $app->request->isGet) {
unset($behaviors['auth']);
}
return $behaviors;
}
}
/**
* @SWG\Get(
* path="/users?expand={models}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Finds User records existing.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(in="path", name="models", type="string",
* description="List of relative to User models comma-separated. Available: role"
* ),
* @SWG\Response(response=200, description="Successful operation.",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/User"))
* ),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=422, description="Data validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Get(
* path="/users/{id}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Returns a single User record.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(description="Unique identifier", in="path", name="id", required=true, type="integer"),
* @SWG\Response(response=200, description="Successful operation.", @SWG\Schema(ref="#/definitions/User")),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=404, description="Not found."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Post(
* path="/users",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Creates a new User record.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(name="body", required=true, in="body", @SWG\Schema(ref="#/definitions/User")),
* @SWG\Response(response=201, description="Successful operation."),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=422, description="Data validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Patch(
* path="/users/{id}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Updates an existed User record, validates only provided records.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(description="Unique identifier", in="path", name="id", required=true, type="integer"),
* @SWG\Parameter(name="body", required=true, in="body", @SWG\Schema(ref="#/definitions/User")),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=422, description="Data validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Put(
* path="/users/{id}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Updates an existed User record, validates an entire model.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(description="Unique identifier", in="path", name="id", required=true, type="integer"),
* @SWG\Parameter(name="body", required=true, in="body", @SWG\Schema(ref="#/definitions/User")),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=422, description="Data validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Delete(
* path="/users/{id}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Deletes a User record.",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(description="Unique identifier", in="path", name="id", required=true, type="integer"),
* @SWG\Response(response=204, description="Successful operation."),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=404, description="Not found."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Get(
* path="/users/statistics?roles={roles}&period={period}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Returns users statistics",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(in="path", name="roles", required=true, type="string",
* description="Comma-separated list of roles ids, ie: 1,2,3"),
* @SWG\Parameter(in="path", name="period", required=true, type="string",
* description="Period. Available options: month, year, total."),
* @SWG\Response(response=200, description="Users statistics.", @SWG\Schema(ref="#/definitions/UsersStatistics")),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=404, description="Not found."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Get(
* path="/users/statistics?roles={roles}&period={period}&user={user}",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Returns users statistics",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(in="path", name="roles", required=true, type="string",
* description="Comma-separated list of roles ids, ie: 1,2,3"),
* @SWG\Parameter(in="path", name="period", required=true, type="string",
* description="Period. Available options: month, year, total."),
* @SWG\Parameter(in="path", name="user", required=true, type="integer",
* description="User identifier for the details"),
* @SWG\Response(response=200, description="Sessions list.",
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Session"))),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=404, description="Not found."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*
* @SWG\Definition(
* definition="UsersStatistics",
* type="object",
* description="UsersStatistics model",
* allOf={
* @SWG\Schema(ref="#/definitions/UsersStatistics"),
* @SWG\Schema(
* @SWG\Property(property="id", type="integer", example=7, description="Unique user identifier"),
* @SWG\Property(property="name", type="string", example="Chelsea", description="Username"),
* @SWG\Property(property="times", type="integer", example=8, description="Number of times the user has logged in"),
* @SWG\Property(property="duration", type="integer", example=36541, description="Overall sessions time"),
* )
* }
* )
*
* @SWG\Get(
* path="/users/{id}/image",
* tags={"Users"},
* summary="Returns user avatar image.",
* @SWG\Parameter(in="path", name="id", type="integer",
* description="User identifier"
* ),
* @SWG\Response(response=200, description="Successful operation."),
* @SWG\Response(response=500, description="Internal server error.")
* )
* @SWG\Post(
* path="/users/{id}/image",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Uploads user avatar image.",
* consumes={"multipart/form-data"},
* @SWG\Parameter(in="path", name="id", type="integer", description="User identifier"),
* @SWG\Parameter(name="content", required=true, in="formData", type="file", description="Image file to upload"),
* @SWG\Response(response=200, description="Successful operation."),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=404, description="Not found."),
* @SWG\Response(response=422, description="Data validation error."),
* @SWG\Response(response=500, description="Internal server error.")
* )
* @SWG\Delete(
* path="/users/{id}/image",
* tags={"Users"},
* security={{"bearer":{}}},
* summary="Deletes user avatar image.",
* @SWG\Parameter(in="path", name="id", type="integer", description="User identifier"),
* @SWG\Response(response=204, description="Successful operation."),
* @SWG\Response(response=401, description="Unauthorized."),
* @SWG\Response(response=404, description="Not found."),
* @SWG\Response(response=500, description="Internal server error.")
* )
*/