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.

LuticateSettingsBusiness.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Luticate\Auth\Business;
  3. use Luticate\Utils\LuBusiness;
  4. use Luticate\Auth\DataAccess\LuticateSettingsDataAccess;
  5. use Luticate\Auth\DBO\LuticateSettingsDbo;
  6. use Luticate\Utils\LuMultipleDbo;
  7. use Luticate\Utils\LuStringUtils;
  8. class LuticateSettingsBusiness extends LuBusiness {
  9. protected static function getDataAccess()
  10. {
  11. return new LuticateSettingsDataAccess();
  12. }
  13. /**
  14. * @param $setting LuticateSettingsDbo
  15. */
  16. public static function checkSetting($setting)
  17. {
  18. $val = $setting->getValue();
  19. $type = $setting->getType();
  20. if (is_null($setting->getName())) {
  21. self::notFound("Setting Name cannot be null");
  22. }
  23. if (is_null($val)) {
  24. self::notFound("Setting Value cannot be null");
  25. }
  26. if (is_null($type)) {
  27. self::notFound("Setting Type cannot be null");
  28. }
  29. if (!is_bool($setting->getIsBlocked())) {
  30. self::notFound("Setting IsBlocked must be boolean");
  31. }
  32. if (!is_bool($setting->getIsAdmin())) {
  33. self::notFound("Setting IsAdmin must be boolean");
  34. }
  35. else {
  36. if ($type == "string") {
  37. if (!is_string($val)) {
  38. self::badInput("Invalid Value");
  39. }
  40. }
  41. else if ($type == "int") {
  42. if (!is_int($val)) {
  43. self::badInput("Invalid Value");
  44. }
  45. }
  46. else if ($type == "float") {
  47. if (!is_float($val) && !is_int($val)) {
  48. self::badInput("Invalid Value");
  49. }
  50. }
  51. else if ($type == "bool") {
  52. if (!is_bool($val)) {
  53. self::badInput("Invalid Value");
  54. }
  55. }
  56. else if ($type == "object") {
  57. if (!is_array($val)) {
  58. self::badInput("Invalid Value");
  59. }
  60. }
  61. else {
  62. self::badInput("Unknown Type");
  63. }
  64. $setting->setValue(json_encode($val));
  65. }
  66. }
  67. public static function checkSettingName($setting_name)
  68. {
  69. $setting = LuticateSettingsDataAccess::getByName($setting_name);
  70. if (is_null($setting)) {
  71. self::notFound("Setting not found");
  72. }
  73. return $setting;
  74. }
  75. public static function getAll($page = 0, $perPage = 2000000000, $query = "")
  76. {
  77. return parent::getAll($page, $perPage, $query)->map(function($value)
  78. {
  79. $value->setValue(json_decode($value->getValue()));
  80. return $value;
  81. });
  82. }
  83. public static function getEffectiveSetting($user_id, $setting_name)
  84. {
  85. $val = LuticateSettingsDataAccess::getEffectiveSetting($user_id, $setting_name);
  86. if (is_null($val->getSetting())) {
  87. self::notFound("Setting not found");
  88. }
  89. $setting = LuStringUtils::convertJsonString($val->getSetting());
  90. $setting["Value"] = json_decode($setting["Value"]);
  91. return $setting;
  92. }
  93. public static function getEffectiveSettingValue($user_id, $setting_name)
  94. {
  95. $setting = self::getEffectiveSetting($user_id, $setting_name);
  96. return $setting["Value"];
  97. }
  98. public static function getAllEffectiveSetting($user_id)
  99. {
  100. return LuticateSettingsDataAccess::getAllEffectiveSetting($user_id)->map(function($value)
  101. {
  102. $setting = $value["Settings"];
  103. $setting["Value"] = json_decode($setting["Value"]);
  104. return $setting;
  105. });
  106. }
  107. public static function get($setting_name)
  108. {
  109. $setting = LuticateSettingsDataAccess::getByName($setting_name);
  110. if (is_null($setting)) {
  111. self::notFound("Permission not found");
  112. }
  113. $setting->setValue(json_decode($setting->getValue()));
  114. return $setting;
  115. }
  116. public static function getValue($setting_name)
  117. {
  118. $setting = self::get($setting_name);
  119. return $setting->getValue();
  120. }
  121. /**
  122. * @param $setting LuticateSettingsDbo
  123. * @return bool
  124. */
  125. public static function add($setting)
  126. {
  127. self::checkSetting($setting);
  128. $perm = LuticateSettingsDataAccess::getByName($setting->getName());
  129. if (is_null($perm)) {
  130. LuticateSettingsDataAccess::add($setting);
  131. return true;
  132. }
  133. self::badInput("Setting name already exists");
  134. return false;
  135. }
  136. public static function del($setting_name)
  137. {
  138. LuticateSettingsDataAccess::delete($setting_name);
  139. return true;
  140. }
  141. /**
  142. * @param $setting_name string
  143. * @param $setting LuticateSettingsDbo
  144. * @return bool
  145. */
  146. public static function edit($setting_name, $setting)
  147. {
  148. $set = self::checkSettingName($setting_name);
  149. $setting->setName($set->getName());
  150. $setting->setType($set->getType());
  151. self::checkSetting($setting);
  152. LuticateSettingsDataAccess::edit($setting_name, $setting);
  153. return true;
  154. }
  155. }