Initial commit,修正
This commit is contained in:
Executable
+266
@@ -0,0 +1,266 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Author: fm453
|
||||
* @Date: 2018-04-23 14:23:45
|
||||
* @Last Modified by: fm453
|
||||
* @Last Modified time: 2021-09-09 00:17:15
|
||||
* @Email: fm453@lukegzs.com
|
||||
*/
|
||||
//运维人员管理
|
||||
namespace backend\controllers;
|
||||
|
||||
use Yii;
|
||||
use backend\models\AuthItem;
|
||||
use backend\models\AuthAssignment;
|
||||
use backend\models\Menu;
|
||||
use backend\models\Adminer;
|
||||
use yii\data\Pagination;
|
||||
use yii\web\Controller;
|
||||
|
||||
class AdminerController extends Controller
|
||||
{
|
||||
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('index');
|
||||
}
|
||||
|
||||
//用户列表
|
||||
public function actionList()
|
||||
{
|
||||
$username = Yii::$app->user->identity->username;
|
||||
$status = [
|
||||
0 => ['title'=>'禁用','css'=>'default'],
|
||||
10 => ['title'=>'启用','css'=>'success'],
|
||||
];
|
||||
$search = [];
|
||||
$search['username'] = Yii::$app->request->get('username');
|
||||
$search['mobile'] = Yii::$app->request->get('mobile');
|
||||
$page = (int)Yii::$app->request->get('page');
|
||||
$page = max(1,$page)-1;
|
||||
if (Yii::$app->request->post('search')) {
|
||||
$search = Yii::$app->request->post('search');
|
||||
}
|
||||
if($search['username']!=''){
|
||||
$username = $search['username'];
|
||||
$data = Adminer::find()->where(['LIKE','username',$username],['<>','status',1]);
|
||||
}elseif($search['mobile']!=''){
|
||||
$mobile = $search['mobile'];
|
||||
$data = Adminer::find()->where(['LIKE','mobile',$mobile],['<>','status',1]);
|
||||
}else{
|
||||
$data = Adminer::find()->where(['<>','status',1]);
|
||||
}
|
||||
|
||||
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20','params'=>$search,'page'=>$page]);
|
||||
$user = $data->joinWith('usergroup')->offset($pages->offset)->limit($pages->limit)->all();
|
||||
return $this->render('list',[
|
||||
'user'=>$user,
|
||||
'search'=>$search,
|
||||
'status' => $status,
|
||||
'pages' => $pages
|
||||
]);
|
||||
}
|
||||
|
||||
//新增用户
|
||||
public function actionCreate()
|
||||
{
|
||||
$model = new Adminer();
|
||||
$model1 = new AuthItem();
|
||||
$auth = Yii::$app->authManager;
|
||||
$item = $auth->getRoles();
|
||||
$itemArr =array();
|
||||
foreach($item as $v){
|
||||
$itemArr[] .= $v->name;
|
||||
}
|
||||
foreach($itemArr as $key=>$value)
|
||||
{
|
||||
$item_one[$value]=$value;
|
||||
}
|
||||
if ($model->load(Yii::$app->request->post())) {
|
||||
$post = Yii::$app->request->post();
|
||||
$model->username = $post['Adminer']['username'];
|
||||
$model->email = $post['Adminer']['email'];
|
||||
$model->mobile = $post['Adminer']['mobile'];
|
||||
|
||||
$user = Adminer::find()->where(['username'=>$model->username])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '用户名已存在!');
|
||||
return $this->redirect(['create']);
|
||||
}
|
||||
$user = User::find()->where(['mobile'=>$model->mobile])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '手机号已被使用!');
|
||||
return $this->redirect(['create']);
|
||||
}
|
||||
|
||||
$user = User::find()->where(['email'=>$model->email])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '邮箱重复!');
|
||||
return $this->redirect(['create']);
|
||||
}
|
||||
|
||||
$model->nickname = $post['Adminer']['nickname'];
|
||||
$model->avatar = $post['Adminer']['avatar'];
|
||||
$model->setPassword($post['Adminer']['auth_key']);
|
||||
$model->generateAuthKey();
|
||||
$model->created_at = time();
|
||||
$model->save();
|
||||
$user_id = $model->attributes['id']; //获取插入后id
|
||||
$role = $auth->createRole($post['AuthItem']['name']);//创建角色对象
|
||||
$auth->assign($role, $user_id);//添加对应关系
|
||||
|
||||
return $this->redirect(['list']);
|
||||
} else {
|
||||
return $this->render('create', [
|
||||
'model' => $model,
|
||||
'model1' => $model1,
|
||||
'item' => $item_one,
|
||||
'params' => Yii::$app->params
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
//更新用户
|
||||
public function actionUpdate(){
|
||||
$item_name = Yii::$app->request->get('item_name');
|
||||
$id = Yii::$app->request->get('id');
|
||||
$model = Adminer::find()->joinWith('usergroup')->where(['id' => $id])->one(); //加入了角色组数据
|
||||
|
||||
$auth = Yii::$app->authManager;
|
||||
$item = $auth->getRoles();
|
||||
$item_one = $itemArr =array();
|
||||
foreach($item as $v){
|
||||
$itemArr[] .= $v->name;
|
||||
}
|
||||
foreach($itemArr as $key=>$value)
|
||||
{
|
||||
$item_one[$value]=$value;
|
||||
}
|
||||
$model1 = $this->findModel($id); //仅账号本身数据
|
||||
|
||||
if ($model1->load(Yii::$app->request->post())) {
|
||||
$post = Yii::$app->request->post();
|
||||
|
||||
$mobile = $post['Adminer']['mobile'];
|
||||
if($model->mobile != $mobile){
|
||||
$user = Adminer::find()->where(['mobile'=>$mobile])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '该手机号已由其他账号绑定!');
|
||||
return $this->render('update',[
|
||||
'model' => $model,
|
||||
'item' => $item_one
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$email = $post['Adminer']['email'];
|
||||
if($model->email != $email){
|
||||
$user = Adminer::find()->where(['email'=>$email])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '该邮箱已有其他账号绑定!');
|
||||
return $this->render('update',[
|
||||
'model' => $model,
|
||||
'item' => $item_one
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
//更新密码
|
||||
if(!empty($post['Adminer']['auth_key_new'])){
|
||||
$model1->setPassword($post['Adminer']['auth_key_new']);
|
||||
$model1->generateAuthKey();
|
||||
}else{
|
||||
$model1->auth_key = $post['Adminer']['auth_key'];
|
||||
}
|
||||
$model1->avatar = $post['Adminer']['avatar'];
|
||||
$model1->mobile = $post['Adminer']['mobile'];
|
||||
$model->nickname = $post['Adminer']['nickname'];
|
||||
$model1->save($post);
|
||||
|
||||
//分配角色
|
||||
if(isset($post['AuthAssignment'])){
|
||||
$role = $auth->createRole($post['AuthAssignment']['item_name']); //创建角色对象
|
||||
$user_id = $id;//获取用户id
|
||||
$auth->revokeAll($user_id);
|
||||
$auth->assign($role, $user_id);//添加对应关系
|
||||
}
|
||||
\Yii::$app->getSession()->setFlash('sucess', '账户信息更新成功!');
|
||||
|
||||
return $this->redirect(['list']);
|
||||
}
|
||||
return $this->render('update',[
|
||||
'model' => $model,
|
||||
'item' => $item_one
|
||||
]);
|
||||
}
|
||||
|
||||
//剔除用户(软删)
|
||||
public function actionDelete($id)
|
||||
{
|
||||
$id = Yii::$app->request->get('id');
|
||||
$model = $this->findModel($id);
|
||||
$model->status = 1;
|
||||
$res = $model->save();
|
||||
var_dump($res);die;
|
||||
//剔除权限
|
||||
$connection=Yii::$app->db;
|
||||
$transaction=$connection->beginTransaction();
|
||||
try
|
||||
{
|
||||
$connection->createCommand()->delete("auth_assignment", "user_id = '$id'")->execute();
|
||||
$transaction->commit();
|
||||
}
|
||||
catch(Exception $ex)
|
||||
{
|
||||
$transaction->rollBack();
|
||||
}
|
||||
\Yii::$app->getSession()->setFlash('sucess', '该账户已删除!');
|
||||
return $this->redirect(['list']);
|
||||
}
|
||||
|
||||
//删除用户
|
||||
public function actionRemove($id)
|
||||
{
|
||||
$connection=Yii::$app->db;
|
||||
$transaction=$connection->beginTransaction();
|
||||
try
|
||||
{
|
||||
$connection->createCommand()->delete("c_adminer", "id = '$id'")->execute();
|
||||
$connection->createCommand()->delete("auth_assignment", "user_id = '$id'")->execute();
|
||||
$transaction->commit();
|
||||
}
|
||||
catch(Exception $ex)
|
||||
{
|
||||
$transaction->rollBack();
|
||||
}
|
||||
return $this->redirect(['list']);
|
||||
}
|
||||
|
||||
//启停用户
|
||||
public function actionStatus(){
|
||||
$id = Yii::$app->request->get('id');
|
||||
$id = (int)$id;
|
||||
if($id>0){
|
||||
$status = Yii::$app->request->get('status');
|
||||
$model = $this->findModel($id);
|
||||
$model->status = $status;
|
||||
$res = $model->save();
|
||||
if(!$res){
|
||||
\Yii::$app->getSession()->setFlash('error', '用户状态更新失败!');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->redirect(['adminer/list']);
|
||||
}
|
||||
|
||||
protected function findModel($id)
|
||||
{
|
||||
if (($model = Adminer::findOne($id)) !== null) {
|
||||
return $model;
|
||||
} else {
|
||||
throw new NotFoundHttpException('The requested page does not exist.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user