From c6a20e910a461d6f703884072de64b9b2ce7f2c8 Mon Sep 17 00:00:00 2001 From: printempw Date: Fri, 29 Jul 2016 15:31:05 +0800 Subject: [PATCH] add operation of setting/canceling admins #21 --- app/Controllers/AdminController.php | 40 +++++++++++++-- assets/src/js/admin.js | 36 +++++++++++--- resources/views/admin/players.tpl | 8 +++ resources/views/admin/users.tpl | 75 +++++++++++++++++++++++++---- 4 files changed, 139 insertions(+), 20 deletions(-) diff --git a/app/Controllers/AdminController.php b/app/Controllers/AdminController.php index 1a126eef..c8230e13 100644 --- a/app/Controllers/AdminController.php +++ b/app/Controllers/AdminController.php @@ -112,6 +112,8 @@ public function userAjaxHandler() $user = new User('', Utils::getValue('uid', $_POST)); + $current_user = new User($_SESSION['email']); + if (!$user->is_registered) throw new E('用户不存在', 1); @@ -148,12 +150,40 @@ public function userAjaxHandler() if ($user->setScore($_POST['score'])) View::json('积分修改成功', 0); - } else if ($action == "permission") { - $permission = $user->getPermission() == "0" - ? "-1" : "0"; + } else if ($action == "ban") { + if ($user->getPermission() == "1") { + if ($current_user->getPermission() != "2") + View::json('非超级管理员无法封禁普通管理员'); + } elseif ($user->getPermission() == "2") { + View::json('超级管理员无法被封禁'); + } + + $permission = $user->getPermission() == "-1" ? "0" : "-1"; + + if ($user->setPermission($permission)) { + View::json([ + 'errno' => 0, + 'msg' => '账号已被' . ($permission == '-1' ? '封禁' : '解封'), + 'permission' => $user->getPermission() + ]); + } - if ($user->setPermission($permission)) - View::json('账号已被'.($permission == "-1"?"封禁":"解封"), 0); + } else if ($action == "admin") { + if ($current_user->getPermission() != "2") + View::json('非超级管理员无法进行此操作'); + + if ($user->getPermission() == "2") + View::json('超级管理员无法被解除'); + + $permission = $user->getPermission() == "1" ? "0" : "1"; + + if ($user->setPermission($permission)) { + View::json([ + 'errno' => 0, + 'msg' => '账号已被' . ($permission == '1' ? '设为' : '解除') . '管理员', + 'permission' => $user->getPermission() + ]); + } } else if ($action == "delete") { if ($user->delete()) diff --git a/assets/src/js/admin.js b/assets/src/js/admin.js index 67c92a75..6fab105f 100644 --- a/assets/src/js/admin.js +++ b/assets/src/js/admin.js @@ -2,7 +2,7 @@ * @Author: printempw * @Date: 2016-07-22 14:02:44 * @Last Modified by: printempw -* @Last Modified time: 2016-07-23 21:12:23 +* @Last Modified time: 2016-07-29 15:27:02 */ 'use strict'; @@ -131,19 +131,43 @@ function changeUserScore(uid, score) { }); } -function changePermission(uid) { +function changeBanStatus(uid) { $.ajax({ type: "POST", - url: "../admin/users?action=permission", + url: "../admin/users?action=ban", dataType: "json", data: { 'uid': uid }, success: function(json) { if (json.errno == 0) { - var object = $($('#'+uid).find('ul').children()[6]); - var dom = '' + + var object = $('#'+uid).find('a#ban'); + var dom = '' + (object.text() == '封禁' ? '解封' : '封禁') + ''; object.html(dom); - $('#'+uid).find('#permission').text(object.text() == '封禁' ? '正常' : '封禁'); + + $('#'+uid).find('#permission').text(json.permission == '-1' ? '封禁' : '正常'); + toastr.success(json.msg); + } else { + toastr.warning(json.msg); + } + }, + error: showAjaxError + }); +} + +function changeAdminStatus(uid) { + $.ajax({ + type: "POST", + url: "../admin/users?action=admin", + dataType: "json", + data: { 'uid': uid }, + success: function(json) { + if (json.errno == 0) { + var object = $('#'+uid).find('a#admin'); + var dom = '' + + (object.text() == '设为管理员' ? '解除管理员' : '设为管理员') + ''; + object.html(dom); + + $('#'+uid).find('#permission').text(json.permission == '1' ? '管理员' : '正常'); toastr.success(json.msg); } else { toastr.warning(json.msg); diff --git a/resources/views/admin/players.tpl b/resources/views/admin/players.tpl index 816b1d8c..ca8c8bef 100644 --- a/resources/views/admin/players.tpl +++ b/resources/views/admin/players.tpl @@ -164,3 +164,11 @@ @endsection + +@section('script') + +@endsection diff --git a/resources/views/admin/users.tpl b/resources/views/admin/users.tpl index db734292..a0dc367d 100644 --- a/resources/views/admin/users.tpl +++ b/resources/views/admin/users.tpl @@ -26,6 +26,8 @@ + +
@@ -50,7 +52,17 @@ {{ $user->email }} {{ $user->nickname }} - {{ $user->permission == "-1" ? "封禁" : "正常" }} + + @if ($user->permission == "0") + 正常 + @elseif ($user->permission == "-1") + 封禁 + @elseif ($user->permission == "1") + 管理员 + @elseif ($user->permission == "2") + 超级管理员 + @endif + {{ $user->register_at }} @@ -66,21 +78,58 @@
  • 查看该用户拥有的角色
  • - @if ($user->permission == "1") -
  • 无法封禁管理员
  • - @elseif ($user->permission == "0") -
  • 封禁
  • + {{-- If current user is super admin --}} + @if ($current_user->getPermission() == "2") + + @if ($user->permission == "1") +
  • 解除管理员
  • + @elseif ($user->permission == "2") +
  • 无法解除超级管理员
  • + @else +
  • 设为管理员
  • + @endif + +
  • + + @if ($user->permission == "2") +
  • 无法封禁超级管理员
  • + @elseif ($user->permission == "-1") +
  • 解封
  • + @else +
  • 封禁
  • + @endif + + {{-- If current user is ordinary admin --}} @else -
  • 解封
  • + + @if ($user->permission == "1" || $user->permission == "2") +
  • 无法封禁管理员
  • + @elseif ($user->permission == "0") +
  • 封禁
  • + @else +
  • 解封
  • + @endif + @endif
    + {{-- If current user is super admin --}} + @if ($current_user->getPermission() == "2") + + @if ($user->permission == "2") + 删除用户 + @else + 删除用户 + @endif - @if ($user->permission == "1") - 删除用户 @else - 删除用户 + @if ($user->permission == "1" || $user->permission == "2") + 删除用户 + @else + 删除用户 + @endif + @endif @@ -134,3 +183,11 @@ @endsection + +@section('script') + +@endsection