Skip to content

Commit

Permalink
优化PR,细节点很多,明天还有得修
Browse files Browse the repository at this point in the history
  • Loading branch information
wushuiyong committed Apr 17, 2016
1 parent 425ce4f commit bdc1c74
Show file tree
Hide file tree
Showing 20 changed files with 514 additions and 485 deletions.
19 changes: 14 additions & 5 deletions components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@

class Controller extends yii\web\Controller {

/**
* 返回成功
*/
const SUCCESS = 0;

/**
* 返回失败
*/
const FAIL = -1;

public $uid = null;

/**
Expand All @@ -35,15 +45,14 @@ public function beforeAction($action) {
* @param string $msg 错误信息
* @param int $option json_encode options
*/
public static function renderJson($data, $code = 0, $msg = '', $option = 0) {
public static function renderJson($data, $code = self::SUCCESS, $msg = '', $option = 0) {
Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
$ret = [

Yii::$app->response->data = [
'code' => (int)$code,
'msg' => $msg,
'data' => $data,
];

Yii::$app->response->data = $ret;
];;
Yii::$app->end();
}

Expand Down
1 change: 1 addition & 0 deletions components/GlobalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public static function formatAvatar($pic) {
public static function isValidAdmin() {
return \Yii::$app->user
&& \Yii::$app->user->identity->role == \app\models\User::ROLE_ADMIN
&& \Yii::$app->user->identity->is_email_verified == \app\models\User::MAIL_ACTIVE
&& \Yii::$app->user->identity->status == \app\models\User::STATUS_ACTIVE;
}

Expand Down
143 changes: 135 additions & 8 deletions controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use app\models\User;
use app\models\forms\UserResetPasswordForm;
use yii\base\InvalidParamException;
use app\models\forms\AddUserForm;
use yii\data\Pagination;


class UserController extends Controller {
Expand All @@ -26,13 +28,13 @@ public function actionIndex() {
public function actionAvatar() {
$fileParts = pathinfo($_FILES['avatar']['name']);
if ($_FILES['avatar']['error']) {
$this->renderJson([], -1, yii::t('user', 'upload failed'));
$this->renderJson([], self::FAIL, yii::t('user', 'upload failed'));
}
if ($_FILES['avatar']['size'] > static::AVATAR_SIZE) {
$this->renderJson([], -1, yii::t('user', 'attached\'s size too large'));
$this->renderJson([], self::FAIL, 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', [
$this->renderJson([], self::FAIL, yii::t('user', 'type not allow', [
'types' => join(', ', \Yii::$app->params['user.avatar.extension'])
]));
}
Expand Down Expand Up @@ -62,6 +64,25 @@ public function actionAudit() {
}


/**
* 用户管理
*/
public function actionList($page = 1, $size = 10) {
$userList = User::find()->orderBy('id desc');
$kw = \Yii::$app->request->post('kw');
if ($kw) {
$userList->andFilterWhere(['like', "username", $kw])
->orFilterWhere(['like', "email", $kw]);
}
$pages = new Pagination(['totalCount' => $userList->count(), 'pageSize' => $size]);
$userList = $userList->offset(($page - 1) * $size)->limit($size)->asArray()->all();

return $this->render('list', [
'userList' => $userList,
'pages' => $pages,
]);
}

/**
* 删除项目管理员
*
Expand All @@ -72,11 +93,6 @@ 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([]);
}
Expand Down Expand Up @@ -133,4 +149,115 @@ protected function findModel($id) {
}
}

/**
* 设置为管理员
*
* @return json
*/
public function actionToAdmin($uid) {
$this->validateAdmin();
if ($uid) {
User::updateAll(['role' => User::ROLE_ADMIN], ['id' => $uid]);
}

$this->renderJson([], self::SUCCESS);
}

/**
* 设置为普通用户
*
* @return json
*/
public function actionToDev($uid) {
$this->validateAdmin();
if ($uid) {
User::updateAll(['role' => User::ROLE_DEV], ['id' => $uid]);
}

$this->renderJson([], self::SUCCESS);
}

/**
* 帐号冻结
*
* @return json
*/
public function actionBan($uid) {
$this->validateAdmin();
if ($uid) {
User::updateAll(['status' => User::STATUS_INVALID], ['id' => $uid]);
}

$this->renderJson([], self::SUCCESS);
}

/**
* 帐号解冻
*
* @return json
*/
public function actionUnBan($uid) {
$this->validateAdmin();
if ($uid) {
User::updateAll(['status' => User::STATUS_ACTIVE], ['id' => $uid]);
}

$this->renderJson([], self::SUCCESS);
}

/**
* 删除帐号
*
* @return json
*/
public function actionDelete($uid) {
$this->validateAdmin();
$user = User::findOne($uid);
if ($user) {
$user->delete();
}

$this->renderJson([], self::SUCCESS);
}

/**
* 修改真实姓名
*
* @return json
*/
public function actionRename($realName, $uid) {
$this->validateAdmin();
if ($realName && $uid) {
$res = User::updateAll(['realname' => $realName], ['id' => $uid]);
$this->renderJson([], $res ? self::SUCCESS : self::FAIL, $res ? '' : Yii::t('w', 'update failed'));
}
$this->renderJson([], self::FAIL, Yii::t('w', 'update failed'));
}

/**
* 新增用户
*/
public function actionAdd() {
$this->validateAdmin();
$model = new AddUserForm();

if ($model->load(Yii::$app->request->post()) ) {
if ($user = $model->signup()) {
Yii::$app->mail->compose('accountNotice', ['user' => $user])
->setFrom(Yii::$app->mail->messageConfig['from'])
->setTo($user->email)
->setSubject('瓦力平台 - 帐号已开通')
->send();

return $this->redirect('@web/user/list');
}
else {
throw new \Exception(yii::t('user', 'email exists'));
}
}

return $this->render('add', [
'model' => $model
]);
}
}
124 changes: 0 additions & 124 deletions controllers/UserManagementController.php

This file was deleted.

14 changes: 12 additions & 2 deletions mail/accountNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
* @var yii\web\View $this
* @var common\models\User $user
*/
$confirmationLink = Yii::$app->urlManager->createAbsoluteUrl(['site/confirm-email', 'token' => $user->email_confirmation_token]);

?>

<?= yii::t('user-management', 'dear') ?><strong><?= $user->realname ?></strong>:
<?= yii::t('user', 'dear') ?> <strong><?= $user->realname ?></strong>:

<br><br>
<span style="text-indent: 2em"><?= yii::t('user-mangement', 'notice account has been opened') ?></span>
<span style="text-indent: 2em"><?= yii::t('user', 'notice account has been opened') ?></span>
<br>
<?= yii::t('user', 'email') ?> : <?= $user->email ?><br>
<?= yii::t('user', 'password') ?> : <?= $user->password ?><br>
<?= yii::t('user', 'role') ?> : <?= \Yii::t('w', 'user_role_' . $user->role) ?><br>
<h3><?= Html::a(yii::t('user', 'active'), $confirmationLink) ?></h3>
<a href="<?= $confirmationLink ?>"><?= $confirmationLink ?></a>


3 changes: 2 additions & 1 deletion mail/passwordResetToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@

<br><br>
<h3><?= Html::a(yii::t('user', 'reset password'), $resetLink) ?></h3>

<br>
<a href="<?= $resetLink ?>"> <?= $resetLink ?></a>
Loading

0 comments on commit bdc1c74

Please sign in to comment.