123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class UserRole extends Model
- {
- protected $table = "user_roles";
- public $timestamps = false;
- /**
- * 配置用户的角色,当前的做法是,
- * 每次更新都先把userId匹配的数据全部标记删除
- * 然后批量插入新的配置的角色数据
- *
- * @param $uid
- * @param array $roles
- * @return array
- */
- public function AssignUserRoles($uid, array $roles)
- {
- if ($uid < 1) {
- return ["code" => INVALID_USER_ID];
- }
- // 删除当前用户所有的角色
- $this->where("user_id", $uid)->where("is_del", false)->update(["is_del" => true]);
- // 过滤掉无效的roleId
- $roleModel = new Role();
- $validRoles = $roleModel->select("id")->whereIn("id", $roles)->where("is_del", false)->get();
- $validRoleIds = [];
- foreach ($validRoles as $validRole) {
- array_push($validRoleIds, $validRole["id"]);
- }
- if (count($validRoleIds) < 1) {
- return ["code" => SUCCESS, "data" => ["roles" => []]];
- }
- // 重新添加用户角色新配置的角色数据
- $data = [];
- foreach ($validRoleIds as $role) {
- array_push($data, ["user_id" => $uid, "role_id" => $role]);
- }
- $this->insert($data);
- // 查出批量插入的数据,并返回给前端
- $new_data = $this->select("id", "user_id", "role_id", "status")->where("user_id", $uid)->where("is_del", false)
- ->orderBy("role_id", "asc")->get();
- $rids = [];
- foreach ($new_data as $nd) {
- array_push($rids, $nd["role_id"]);
- }
- if (count($rids) > 0) {
- $role = new Role();
- $roleInfo = $role->LoadRoleByIds_KV($rids);
- }
- $result = ["roles" => $new_data, "roleInfo" => $roleInfo];
- return ["code" => SUCCESS, "data" => $result];
- }
- /**
- * 配置角色下的用户数据,当前的做法是,
- * 每次更新都先把roleId匹配的数据全部标记删除
- * 然后批量插入新的配置的用户数据
- *
- * @param $rid
- * @param array $userIds
- * @return array
- */
- public function AssignRoleUsers($rid, array $userIds)
- {
- if ($rid < 1) {
- return ["code" => INVALID_ROLE_ID];
- }
- // 删除当前角色所有的用户
- $this->where("role_id", $rid)->where("is_del", false)->update(["is_del" => true]);
- // 过滤掉无效的userId
- $userModel = new User();
- $validUsers = $userModel->select("id")->whereIn("id", $userIds)->where("is_del", false)->get();
- $validUids = [];
- foreach ($validUsers as $validUser) {
- array_push($validUids, $validUser["id"]);
- }
- if (count($validUids) < 1) {
- return ["code" => SUCCESS, "data" => ["users" => []]];
- }
- // 重新添加指定角色下的用户数据
- $data = [];
- foreach ($validUids as $userId) {
- array_push($data, ["user_id" => $userId, "role_id" => $rid]);
- }
- $this->insert($data);
- //查出批量插入的数据,并返回给前端
- $new_data = $this->select("id", "user_id", "role_id", "status")->where("role_id", $rid)->where("is_del", false)
- ->orderBy("user_id", "asc")->get();
- $uids = [];
- foreach ($new_data as $nd) {
- array_push($uids, $nd["user_id"]);
- }
- if (count($uids) > 0) {
- $user = new User();
- $userInfo = $user->ListUserByIds_KV($uids);
- }
- $result = ["users" => $new_data, "userInfo" => $userInfo];
- return ["code" => SUCCESS, "data" => $result];
- }
- public function LoadRoleByUid($uid)
- {
- $role = $this->select("user_id", "role_id", "status")
- ->where("user_id", $uid)
- ->where("is_del", false)
- ->first();
- return $role;
- }
- /**
- * 加载指定用户拥有的角色信息
- *
- * @param $uid
- * @return array
- */
- public function LoadRole($uid)
- {
- $roles = $this->select("id", "user_id", "role_id", "status")
- ->where("user_id", $uid)->where("is_del", false)->get();
- if (count($roles) == 0) {
- return ["code" => 0, "data" => []];
- }
- $roleIds = [];
- foreach ($roles as $role) {
- array_push($roleIds, $role["role_id"]);
- }
- // 加載角色的信息
- $role = new Role();
- $roleInfo = $role->LoadRoleByIds_KV($roleIds);
- $result = ["roleUsers" => $roles, "roleInfo" => $roleInfo];
- return ["code" => SUCCESS, "data" => $result];
- }
- /**
- * 根据角色id,列出用户-角色数据
- *
- * @param array $rids 多个角色id
- * @return array
- */
- public function LoadUsersWithRoles(array $rids)
- {
- $userRoles = $this->select("id", "user_id", "role_id", "status")
- ->whereIn("role_id", $rids)->where("is_del", false)
- ->get();
- if (count($userRoles) == 0) {
- return [];
- }
- // 根据不同角色收集各自的user_id,同时收集所有的用户id,用于查出用户信息
- $allUids = [];
- $roleUsers = [];
- foreach ($userRoles as $userRole) {
- $key = $userRole["role_id"];
- $userId = $userRole["user_id"];
- if (!key_exists($key, $roleUsers)) {
- $roleUsers[$key] = [];
- }
- $tmpUsers = $roleUsers[$key];
- array_push($tmpUsers, $userId);
- $roleUsers[$key] = $tmpUsers;
- array_push($allUids, $userId);
- }
- // 这个if正常情况下是不会进入的,前面已经有if了。
- if (count($allUids) == 0) {
- return [];
- }
- // load user info
- $userObj = new User();
- $userInfo = $userObj->ListUserByIds_KV($allUids);
- $result = [];
- // 每个角色分别收取自己拥有的用户信息
- foreach ($roleUsers as $roleId => $uids) {
- $tmpUsers = [];
- foreach ($uids as $uid) {
- if (!key_exists($uid, $userInfo)) {
- continue;
- }
- array_push($tmpUsers, $userInfo[$uid]);
- }
- $result[$roleId] = $tmpUsers;
- }
- return $result;
- }
- }
|