Completed
Push — master ( a83a36...e8a960 )
by vistart
03:12
created

UserController::getUser()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.2
cc 4
eloc 8
nc 3
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 rhosocial\user\User;
16
use Yii;
17
use yii\data\ActiveDataProvider;
18
use yii\filters\AccessControl;
19
use yii\web\Controller;
20
use yii\web\BadRequestHttpException;
21
use yii\web\ForbiddenHttpException;
22
use yii\web\MethodNotAllowedHttpException;
23
use yii\web\ServerErrorHttpException;
24
use yii\web\UnauthorizedHttpException;
25
26
/**
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
class UserController extends Controller
31
{
32
    public function behaviors() {
33
        return [
34
            'access' => [
35
                'class' => AccessControl::class,
36
                'rules' => [
37
                    [ // Disallow all unauthorized users to access this controller.
38
                        'allow' => false,
39
                        'roles' => ['?'],
40
                    ],
41
                    [ // Disallow non-admin user to access this controller.
42
                        'allow' => false,
43
                        '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...
44
                            return !Yii::$app->authManager->checkAccess(Yii::$app->user->identity, 'admin');
45
                        },
46
                        '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...
47
                            throw new UnauthorizedHttpException('You are not an administrator and have no access to this page.');
48
                        },
49
                    ],
50
                    [ // Disallow admin user to access deregister action directly, only `POST` accepted.
51
                        'actions' => ['deregister'],
52
                        'allow' => false,
53
                        '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...
54
                            return strtoupper(Yii::$app->request->getMethod()) != 'POST';
55
                        },
56
                        '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...
57
                            throw new MethodNotAllowedHttpException('You cannot access this page directly.');
58
                        },
59
                    ],
60
                    [ // Allow admin user to access other views.
61
                      // This is a final rule, if you want to add other rules, please put it before this rule.
62
                        'allow' => true,
63
                        'roles' => ['admin'],
64
                    ],
65
                ],
66
            ],
67
        ];
68
    }
69
70
    public function actionIndex()
71
    {
72
        $class = Yii::$app->user->identityClass;
73
        if (!class_exists($class)) {
74
            return $this->render('index', ['dataProvider' => null]);
75
        }
76
        $dataProvider = new ActiveDataProvider([
77
            'query' => $class::find(),
78
            'pagination' => [
79
                'pageSize' => 20,
80
            ],
81
        ]);
82
        return $this->render('index', ['dataProvider' => $dataProvider]);
83
    }
84
85
    public function actionRegisterNewUser()
86
    {
87
        return $this->render('register-new-user');
88
    }
89
90
    /**
91
     * Get user by ID.
92
     * @param string $id User ID.
93
     * @return User
94
     * @throws BadRequestHttpException throw if user not found.
95
     */
96
    protected function getUser($id)
97
    {
98
        $class = Yii::$app->user->identityClass;
99
        if (!class_exists($class)) {
100
            return null;
101
        }
102
        $user = $class::find()->id($id)->one();
103
        if (empty($user) || !($user instanceof User)) {
104
            throw new BadRequestHttpException('User Not Found.');
105
        }
106
        return $user;
107
    }
108
109
    /**
110
     * Deregister User.
111
     * @param string $id User ID.
112
     * @return string
113
     */
114
    public function actionDeregister($id)
115
    {
116
        $id = (int)$id;
117
        if (Yii::$app->user->identity->getID() == $id) {
118
            throw new ForbiddenHttpException('You cannot deregister yourself.');
119
        }
120
        $user = $this->getUser($id);
121
        try {
122
            $result = $user->deregister();
123
            if ($result instanceof \Exception) {
124
                throw $result;
125
            }
126
        } catch (\Exception $ex) {
127
            throw new ServerErrorHttpException($ex->getMessage());
128
        }
129
        if ($result !== true) {
130
            throw new ServerErrorHttpException('Failed to deregister user.');
131
        }
132
        return $this->redirect(['index']);
133
    }
134
135
    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...
136
    {
137
        return $this->render('view');
138
    }
139
140
    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...
141
    {
142
        return $this->render('update');
143
    }
144
}
145