Procházet zdrojové kódy

完善分配 用户/角色/群组 菜单的接口

tangs před 5 roky
rodič
revize
5be26f9cb4

+ 23 - 10
app/Http/Controllers/PermissionController.php

@@ -5,8 +5,8 @@ namespace App\Http\Controllers;
 use App\Models;
 use foo\bar;
 use Illuminate\Http\Request;
-use Mockery\Exception;
 use Illuminate\Support\Facades\Log;
+use Illuminate\Support\Facades\Validator;
 
 // todo 返回值为 数字代码还是json还是其他格式待定
 
@@ -21,30 +21,43 @@ class PermissionController extends Controller
      */
     public function AssignActionBars(Request $request)
     {
+        $validator = Validator::make($request->all(), [
+            "ownerType" => "required|max:32",
+            "ownerId" => "required|integer|min:1",
+        ]);
+        if ($validator->fails()) {
+            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+        }
+
         // 这里的uid从中间件那里获得
-//        $currentUid = $request->input("currentUid");
-        $ownerId = $request->input("ownerId");
+        $currentUid = $request->input("currentUid");
+        $ownerId = (int)$request->input("ownerId");
         $ownerType = $request->input("ownerType");
-        $barIds = $request->input("barIds");
+        $barIdsStr = trim($request->input("barIds"));
+
+        $barIds = $barIdsStr == "" ? [] : explode(",", $barIdsStr);
 
 
-        $currentUid = 1;
         // 判断当前用户是否有权分配权限
         $userRole = new Models\UserRole();
         $role = $userRole->LoadRoleByUid($currentUid);
 
-        if (!$role || $role->status != "normal" || $role->role & 1 == 0) {
-            return "permission denied";
+        if (!$role || $role["status"] != "normal" || $role["role"] != "admin") {
+//            return $this->fail(PERMISSION_DENIED, $this->error[PERMISSION_DENIED]);
         }
 
+
         // 有权分配
         $userActionBar = new Models\UserActionBar();
-        $userActionBar->AddUserActionBar($ownerType, $ownerId, $barIds);
-        return "success";
+        $result = $userActionBar->AssignUserActionBar($ownerType, $ownerId, $barIds);
+        if ($result["code"] == 0) {
+            return $this->success($result["data"]);
+        }
+        return $this->fail($result["code"], $this->error[$result["code"]]);
     }
 
     /**
-     * 分配用户的是否是管理员或者普通用户的权限
+     * 分配用户权限,是管理员或者普通用户的权限
      *
      * @param Request $request
      * @return string

+ 40 - 40
app/Http/Controllers/UserActionBarController.php

@@ -9,48 +9,48 @@ use Illuminate\Support\Facades\Validator;
 // 这个class的方法需要在中间件里加上权限判断,管理员才能执行
 class UserActionBarController extends Controller
 {
-    public function AddUserActionBar(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            "ownerType" => "required|max:32",
-            "ownerId" => "required|integer|min:1",
-            "barId" => "required|integer|min:1"
-        ]);
-        if ($validator->fails()) {
-            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
-        }
-
-        $ownerType = $request->input("ownerType");
-        $ownerId = (int)$request->input("ownerId");
-        $barId = (int)$request->input("barId");
-
-        $userActionBar = new Models\UserActionBar();
-        $result = $userActionBar->AddUserActionBar($ownerType, $ownerId, $barId);
-        if ($result["code"] == 0) {
-            return $this->success($result["data"]);
-        }
-        return $this->fail($result["code"], $this->error[$result["code"]]);
-    }
-
-    public function RemoveUserActionBar(Request $request)
-    {
-        $validator = Validator::make($request->all(), [
-            "id" => "required|integer|min:1",
+//    public function AddUserActionBar(Request $request)
+//    {
+//        $validator = Validator::make($request->all(), [
 //            "ownerType" => "required|max:32",
 //            "ownerId" => "required|integer|min:1",
 //            "barId" => "required|integer|min:1"
-        ]);
-        if ($validator->fails()) {
-            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
-        }
-
-        $id = (int)$request->input("id");
+//        ]);
+//        if ($validator->fails()) {
+//            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+//        }
+//
 //        $ownerType = $request->input("ownerType");
-//        $ownerId = $request->input("ownerId");
-//        $barId = $request->input("barId");
-
-        $userActionBar = new Models\UserActionBar();
-        $code = $userActionBar->RemoveUserActionBar($id);
-        return $this->response($code, $this->error[$code]);
-    }
+//        $ownerId = (int)$request->input("ownerId");
+//        $barId = (int)$request->input("barId");
+//
+//        $userActionBar = new Models\UserActionBar();
+//        $result = $userActionBar->AssignUserActionBar($ownerType, $ownerId, $barId);
+//        if ($result["code"] == 0) {
+//            return $this->success($result["data"]);
+//        }
+//        return $this->fail($result["code"], $this->error[$result["code"]]);
+//    }
+//
+//    public function RemoveUserActionBar(Request $request)
+//    {
+//        $validator = Validator::make($request->all(), [
+//            "id" => "required|integer|min:1",
+////            "ownerType" => "required|max:32",
+////            "ownerId" => "required|integer|min:1",
+////            "barId" => "required|integer|min:1"
+//        ]);
+//        if ($validator->fails()) {
+//            return $this->fail(REQUEST_PARAM_ERROR, $this->error[REQUEST_PARAM_ERROR], $validator->errors());
+//        }
+//
+//        $id = (int)$request->input("id");
+////        $ownerType = $request->input("ownerType");
+////        $ownerId = $request->input("ownerId");
+////        $barId = $request->input("barId");
+//
+//        $userActionBar = new Models\UserActionBar();
+//        $code = $userActionBar->RemoveUserActionBar($id);
+//        return $this->response($code, $this->error[$code]);
+//    }
 }

+ 33 - 17
app/Models/UserActionBar.php

@@ -11,33 +11,49 @@ class UserActionBar extends Model
     protected $fillable = ["owner_type", "owner_id", "bar_id"];
     public $timestamps = false;
 
-    public function AddUserActionBar($ownerType, $ownerId, $barId)
+    /**
+     * 配置角色/用户/群组有权限的action bars,当前的做法是,
+     * 每次更新都先把ownerId和ownerType匹配的数据全部标记删除,
+     * 然后然批量插入新的配置的action bars数据行。
+     *
+     * @param $ownerType 拥有者用户类型
+     * @param $ownerId 拥有者用户id
+     * @param $barId 分配的action bars
+     * @return array
+     */
+    public function AssignUserActionBar($ownerType, $ownerId, $barIds)
     {
         if ($ownerType == "") {
             return ["code" => EMPTY_OWNER_TYPE];
         }
-        if ($ownerId == "") {
+        if ($ownerId < 1) {
             return ["code" => EMPTY_OWNER_ID];
         }
-        if ($barId == "") {
-            return ["code" => EMPTY_BAR_ID];
-        }
 
-        // check owner and barId if exist in system.
-        $code = $this->checkOwner($ownerType, $ownerId);
-        if ($code != 0) {
-            return ["code" => $code];
+        // 删除所有ownerType 和 ownerId
+        $this->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)
+            ->update(["is_del" => true]);
+
+        Log::debug(gettype($barIds));
+        Log::debug(count($barIds));
+        Log::debug($barIds);
+
+        // 重新添加ownerType 和 ownerId的数据
+        if (count($barIds) < 1) {
+            return ["code" => SUCCESS, "data" => []];
         }
-        $code = $this->checkActionBar($barId);
-        if ($code != 0) {
-            return ["code" => $code];
+
+        $data = [];
+        foreach ($barIds as $barId) {
+            array_push($data, array("owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false));
         }
+        $this->insert($data);
+
+        // 插入批量插入的数据,并返回给前端
+        $new_data = $this->select("id", "owner_type", "owner_id", "bar_id", "status")
+            ->where("owner_type", $ownerType)->where("owner_id", $ownerId)->where("is_del", false)->get();
 
-        $result = $this->firstOrCreate(["owner_type" => $ownerType, "owner_id" => $ownerId, "bar_id" => $barId, "is_del" => false]);
-        unset($result["is_del"]);
-        unset($result["created_user_id"]);
-        unset($result["updated_user_id"]);
-        return ["code" => SUCCESS, "data" => $result];
+        return ["code" => SUCCESS, "data" => $new_data];
     }
 
     private function checkActionBar($barId)

+ 6 - 0
app/Models/UserRole.php

@@ -47,6 +47,12 @@ class UserRole extends Model
         return $role;
     }
 
+    /**
+     * 根据角色id,列出用户-角色数据
+     *
+     * @param $rids 多个角色id
+     * @return array
+     */
     public function LoadUsersWithRoles($rids)
     {
         $user_roles = $this->select("id", "user_id", "role_id", "status")

+ 2 - 0
config/errorcode.php

@@ -21,6 +21,7 @@ const EMPTY_OWNER_TYPE = 310;
 const EMPTY_OWNER_ID = 311;
 
 const EMPTY_BAR_ID = 410;
+const PERMISSION_DENIED = 403;
 
 const INVALID_U_A_ID = 510;
 
@@ -56,6 +57,7 @@ return [
     "311" => "empty owner id",
 
     "410" => "empty action bar id",
+    "403" => "permission denied",
 
     "510" => "invalid user-action-bar id",
 

+ 2 - 0
routes/web.php

@@ -33,6 +33,8 @@ Route::get("listRole", "RoleController@ListRole");
 Route::get("loadRoleUsers", "RoleController@LoadRoleUsers");
 
 // user action bar
+Route::get("admin/AssignActionBars", "PermissionController@assignActionBars");
+
 Route::get("addUserActionBar", "UserActionBarController@AddUserActionBar");
 Route::get("removeUserActionBar", "UserActionBarController@RemoveUserActionBar");