Completed
Push — master ( 9b9020...8f5910 )
by Razon
02:07
created

UserController::actionRefreshAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace App\Http\Api\Backend\Controller;
3
4
use App\Http\Api\Backend\Form\LoginForm;
5
use App\Http\Api\Backend\Form\LogoutForm;
6
use App\Http\Api\Backend\Form\SessionRefreshForm;
7
use App\Http\Api\Backend\Model\User;
8
use Yii;
9
10
class UserController extends ActiveController
11
{
12
    public $modelClass = User::class;
13
14
    public function behaviors()
15
    {
16
        $behaviors = parent::behaviors();
17
        $behaviors['authenticator']['optional'] = ['options', 'login', 'logout'];
18
        return $behaviors;
19
    }
20
21
    public function getPermission($action)
22
    {
23
        return 'user' . ucfirst($action);
24
    }
25
26
    public function actionLogin()
27
    {
28
        $form = new LoginForm();
29
        $form->load(Yii::$app->getRequest()->post(), '');
30
        return $form->handle();
31
    }
32
33
    public function actionRefreshAccessToken()
34
    {
35
        $form = new SessionRefreshForm();
36
        $form->load(Yii::$app->getRequest()->post(), '');
37
        return $form->handle();
38
    }
39
40
    public function actionLogout()
41
    {
42
        $form = new LogoutForm();
43
        return $form->handle();
44
    }
45
46
    public function searchModel()
47
    {
48
         return (new \yii\base\DynamicModel(['id', 'username' => null, 'email' => null, 'status' => null]))
49
            ->addRule(['id', 'status'], 'integer')
50
            ->addRule(['username', 'email'], 'trim')
51
            ->addRule(['username', 'email'], 'string');
52
    }
53
54
    protected function getQuery($action)
55
    {
56
        return parent::getQuery($action)
57
            ->alias('u')
58
            ->andWhere(['is_deleted' => 0])
59
            ->orderBy(['id' => SORT_DESC]);
60
    }
61
62
    protected function applyFilter($query, $model, $filter)
63
    {
64
        foreach (['id', 'username', 'email'] as $name) {
65
            if (!empty($model->$name)) {
66
                $query->andFilterWhere(['LIKE', 'u.' . $name, $model->$name]);
67
            }
68
        }
69
70
        if (is_numeric($model->status)) {
71
            $query->andWhere(['u.status' => intval($model->status)]);
72
        }
73
    }
74
}
75