forked from meolu/walle-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserController.php
More file actions
135 lines (112 loc) · 4.01 KB
/
Copy pathUserController.php
File metadata and controls
135 lines (112 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
namespace app\controllers;
use yii;
use yii\web\NotFoundHttpException;
use app\components\Controller;
use app\components\GlobalHelper;
use app\models\User;
use app\models\forms\UserResetPasswordForm;
use yii\base\InvalidParamException;
class UserController extends Controller {
// 头像大小限制200k
const AVATAR_SIZE = 200000;
public function actionIndex() {
$user = User::findOne($this->uid);
return $this->render('index', [
'user' => $user,
]);
}
public function actionAvatar() {
$fileParts = pathinfo($_FILES['avatar']['name']);
if ($_FILES['avatar']['error']) {
$this->renderJson([], -1, yii::t('user', 'upload failed'));
}
if ($_FILES['avatar']['size'] > static::AVATAR_SIZE) {
$this->renderJson([], -1, yii::t('user', 'attached\'s size too large'));
}
if (!in_array(strtolower($fileParts['extension']), \Yii::$app->params['user.avatar.extension'])) {
$this->renderJson([], -1, yii::t('user', 'type not allow', [
'types' => join(', ', \Yii::$app->params['user.avatar.extension'])
]));
}
$tempFile = $_FILES['avatar']['tmp_name'];
$baseName = sprintf('%s-%d.%s', date("YmdHis", time()), rand(10, 99), $fileParts['extension']);
$newFile = GlobalHelper::formatAvatar($baseName);
$targetFile = sprintf("%s/web/%s", rtrim(\Yii::$app->basePath, '/'), ltrim($newFile, '/'));
$ret = move_uploaded_file($tempFile, $targetFile);
if ($ret) {
$user = User::findOne($this->uid);
$user->avatar = $baseName;
$ret = $user->save();
}
$this->renderJson(['url' => $newFile], !$ret, $ret ?: yii::t('user', 'update avatar failed'));
}
public function actionAudit() {
$this->validateAdmin();
$apply = User::getInactiveAdminList();
return $this->render('audit', [
'apply' => $apply,
]);
}
/**
* 删除项目管理员
*
* @return string
* @throws \Exception
*/
public function actionDeleteAdmin($id) {
$this->validateAdmin();
$user = $this->findModel($id);
if ($user->role != User::ROLE_ADMIN || $user->is_email_verified != 1
|| $user->status != User::STATUS_INACTIVE) {
throw new \Exception(yii::t('user', 'cant\'t remove active manager'));
}
if (!$user->delete()) throw new \Exception(yii::t('w', 'delete failed'));
$this->renderJson([]);
}
/**
* 项目审核管理员审核通过
*
* @return string
* @throws \Exception
*/
public function actionActiveAdmin($id) {
$this->validateAdmin();
$user = $this->findModel($id);
if ($user->role != User::ROLE_ADMIN || $user->is_email_verified != 1
|| $user->status != User::STATUS_INACTIVE) {
throw new \Exception(yii::t('user', 'only pass inactive manager'));
}
$user->status = User::STATUS_ACTIVE;
if (!$user->update()) throw new \Exception(yii::t('w', 'update failed'));
$this->renderJson([]);
}
/**
* 用户重置密码
*/
public function actionResetPassword()
{
$user = new UserResetPasswordForm($this->uid);
if ($user->load(Yii::$app->request->post()) && $user->validate() && $user->resetPassword()) {
Yii::$app->getSession()->setFlash('success', 'New password was saved.');
return $this->goHome();
}
return $this->render('resetPassword', [
'model' => $user,
]);
}
/**
* 简化
*
* @param integer $id
* @return Notification the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id) {
if (($model = User::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException(yii::t('user', 'user not exists'));
}
}
}