Permission.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Support\Facades\Log;
  6. const SUPER_ADMIN = 1; // 超级管理员,拥有所有权限
  7. const SYSTEM_ADMIN = 2; // 系统管理员,交付产品时可提供的最高管理员账号
  8. const GUEST_USER = 128; //普通用户
  9. class Permission extends Model
  10. {
  11. protected $table = "permission";
  12. public $timestamps = false;
  13. // 此时应该初始化需要的管理员账号
  14. function __construct(array $attributes = [])
  15. {
  16. parent::__construct($attributes);
  17. }
  18. /**
  19. * @param string $uid
  20. * @param int $rights
  21. * @return string
  22. */
  23. public function IsAccess(string $uid, int $rights)
  24. {
  25. // $this->where("uid",$uid)
  26. // ->where("is_del", false)
  27. // ->where("status", "normal")
  28. // ->where("role", "&");
  29. $data = DB::select("select uid from permission where uid = ? and is_del = false and status = normal and role & ? > 0;", [$uid, $rights]);
  30. if (!$data) {
  31. return "permission denied";
  32. }
  33. return $data->uid;
  34. }
  35. /**
  36. * @param string $adminUid
  37. * @param string $uid
  38. * @param string $rights
  39. * @return string
  40. */
  41. public function ModifyRole(string $adminUid, string $uid, string $rights)
  42. {
  43. // Verify that the current user has permission to modify permissions
  44. $data = DB::select("select uid, role from permission where uid = ? and is_del = false and status = ? and role & ? > 0;", [$adminUid, "normal", SUPER_ADMIN | SYSTEM_ADMIN]);
  45. log::debug($data);
  46. if (!$data) {
  47. log::debug("ModifyRole with adminUid: " . $adminUid . ", uid: " . $uid . ", rights: " . $rights . " but permission denied");
  48. return "permission denied";
  49. }
  50. if ($data->uid < 1) {
  51. log::debug("ModifyRole with adminUid: " . $adminUid . ", uid: " . $uid . ", rights: " . $rights . " but invalid admin uid: " . $adminUid);
  52. return "invalid admin uid: " . $adminUid;
  53. }
  54. if ($data->right >= $rights) {
  55. log::debug("ModifyRole with adminUid: " . $adminUid . ", admin role: ". $data->role . ", uid: " . $uid . ", rights: " . $rights . " but have no permission to grant higher rights");
  56. return "have no permission to grant higher rights";
  57. }
  58. // modify the user's rights
  59. $result = $this->where("uid", $uid)
  60. ->where("is_del", false)
  61. ->where("status", "normal")
  62. ->update(["role" => $rights]);
  63. if (!$result) {
  64. return "grant rights failed";
  65. }
  66. return "success";
  67. }
  68. }