You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuticatePermissionsBusiness.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Luticate\Auth\Business;
  3. use Luticate\Utils\LuBusiness;
  4. use Luticate\Auth\DataAccess\LuticatePermissionsDataAccess;
  5. use Luticate\Auth\DBO\LuticatePermissionsDbo;
  6. use Luticate\Utils\LuMultipleDbo;
  7. class LuticatePermissionsBusiness extends LuBusiness {
  8. protected static function getDataAccess()
  9. {
  10. return new LuticatePermissionsDataAccess();
  11. }
  12. public static function checkPermissionName($permission_name)
  13. {
  14. $perm = LuticatePermissionsDataAccess::getByName($permission_name);
  15. if (is_null($perm)) {
  16. self::notFound("Permission not found");
  17. }
  18. return $perm;
  19. }
  20. public static function getEffectivePermission($user_id, $permission_name)
  21. {
  22. self::checkPermissionName($permission_name);
  23. $val = LuticatePermissionsDataAccess::getEffectivePermission($user_id, $permission_name);
  24. $perm = new LuticatePermissionsDbo();
  25. $perm->setValue($val);
  26. $perm->setName($permission_name);
  27. return $perm;
  28. }
  29. public static function getAllEffectivePermission($user_id)
  30. {
  31. $values = LuticatePermissionsDataAccess::getAllEffectivePermission($user_id);
  32. $dbos = [];
  33. foreach ($values as $value) {
  34. $dbo = new LuticatePermissionsDbo();
  35. $dbo->setName($value->getPermissonName());
  36. $dbo->setValue($value->getVal());
  37. $dbos[] = $dbo;
  38. }
  39. return new LuMultipleDbo(count($dbos), $dbos);
  40. }
  41. public static function getEffectiveDynamicPermission($user_id, $permission_name, $resource_id)
  42. {
  43. $dynamic_permission_name = "LU_DYN_" . $permission_name . "_" . $resource_id;
  44. $val = self::getEffectivePermission($user_id, $dynamic_permission_name);
  45. if (is_null($val->getValue())) {
  46. return self::getEffectivePermission($user_id, $permission_name);
  47. }
  48. return $val;
  49. }
  50. public static function get($permission_name)
  51. {
  52. $perm = LuticatePermissionsDataAccess::getByName($permission_name);
  53. if (is_null($perm)) {
  54. self::notFound("Permission not found");
  55. }
  56. return $perm;
  57. }
  58. public static function add($permission_name, $permission_value)
  59. {
  60. $perm = LuticatePermissionsDataAccess::getByName($permission_name);
  61. if (is_null($perm)) {
  62. $perm = new LuticatePermissionsDbo();
  63. $perm->setName($permission_name);
  64. $perm->setValue($permission_value);
  65. LuticatePermissionsDataAccess::add($perm);
  66. return true;
  67. }
  68. self::badInput("Permission name already exists");
  69. return false;
  70. }
  71. public static function del($permission_name)
  72. {
  73. LuticatePermissionsDataAccess::delete($permission_name);
  74. return true;
  75. }
  76. public static function edit($permission_name, $permission_value)
  77. {
  78. self::checkPermissionName($permission_name);
  79. LuticatePermissionsDataAccess::edit($permission_name, $permission_value);
  80. return true;
  81. }
  82. }