Completed
Push — master ( 245e1e...a83a36 )
by vistart
03:07
created

UserController::actionDeregister()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\web\admin\controllers;
14
15
use Yii;
16
use yii\data\ActiveDataProvider;
17
use yii\filters\AccessControl;
18
use yii\web\Controller;
19
use yii\web\ForbiddenHttpException;
20
use yii\web\MethodNotAllowedHttpException;
21
use yii\web\UnauthorizedHttpException;
22
23
/**
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class UserController extends Controller
28
{
29
    public function behaviors() {
30
        return [
31
            'access' => [
32
                'class' => AccessControl::class,
33
                'rules' => [
34
                    [ // Disallow all unauthorized users to access this controller.
35
                        'allow' => false,
36
                        'roles' => ['?'],
37
                    ],
38
                    [ // Disallow non-admin user to access this controller.
39
                        'allow' => false,
40
                        'matchCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
                            return !Yii::$app->authManager->checkAccess(Yii::$app->user->identity, 'admin');
42
                        },
43
                        'denyCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
                            throw new UnauthorizedHttpException('You are not an administrator and have no access to this page.');
45
                        },
46
                    ],
47
                    [ // Disallow admin user to access deregister action directly, only `POST` accepted.
48
                        'actions' => ['deregister'],
49
                        'allow' => false,
50
                        'matchCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
51
                            return strtoupper(Yii::$app->request->getMethod()) != 'POST';
52
                        },
53
                        'denyCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
                            throw new MethodNotAllowedHttpException('You cannot access this page directly.');
55
                        },
56
                    ],
57
                    [ // Allow admin user to access other views.
58
                      // This is a final rule, if you want to add other rules, please put it before this rule.
59
                        'allow' => true,
60
                        'roles' => ['admin'],
61
                    ],
62
                ],
63
            ],
64
        ];
65
    }
66
67
    public function actionIndex()
68
    {
69
        $class = Yii::$app->user->identityClass;
70
        if (!class_exists($class)) {
71
            return $this->render('index', ['dataProvider' => null]);
72
        }
73
        $dataProvider = new ActiveDataProvider([
74
            'query' => $class::find(),
75
            'pagination' => [
76
                'pageSize' => 20,
77
            ],
78
        ]);
79
        return $this->render('index', ['dataProvider' => $dataProvider]);
80
    }
81
82
    public function actionRegisterNewUser()
83
    {
84
        return $this->render('register-new-user');
85
    }
86
87
    /**
88
     * Deregister User.
89
     * @param string $id User ID.
90
     * @return string
91
     */
92
    public function actionDeregister($id)
93
    {
94
        $id = (int)$id;
95
        if (Yii::$app->user->identity->getID() == $id) {
96
            throw new ForbiddenHttpException('You cannot deregister yourself.');
97
        }
98
        return $this->redirect(['index']);
99
    }
100
101
    public function actionView($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        return $this->render('view');
104
    }
105
106
    public function actionUpdate($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        return $this->render('update');
109
    }
110
}