123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
-
- namespace Luticate\Auth\Business;
-
- use Luticate\Utils\LuBusiness;
- use Luticate\Auth\DataAccess\LuticateSettingsDataAccess;
- use Luticate\Auth\DBO\LuticateSettingsDbo;
- use Luticate\Utils\LuMultipleDbo;
- use Luticate\Utils\LuStringUtils;
-
- class LuticateSettingsBusiness extends LuBusiness {
- protected static function getDataAccess()
- {
- return new LuticateSettingsDataAccess();
- }
-
- /**
- * @param $setting LuticateSettingsDbo
- */
- public static function checkSetting($setting)
- {
- $val = $setting->getValue();
- $type = $setting->getType();
- if (is_null($setting->getName())) {
- self::notFound("Setting Name cannot be null");
- }
- if (is_null($val)) {
- self::notFound("Setting Value cannot be null");
- }
- if (is_null($type)) {
- self::notFound("Setting Type cannot be null");
- }
- if (!is_bool($setting->getIsBlocked())) {
- self::notFound("Setting IsBlocked must be boolean");
- }
- if (!is_bool($setting->getIsAdmin())) {
- self::notFound("Setting IsAdmin must be boolean");
- }
- else {
- if ($type == "string") {
- if (!is_string($val)) {
- self::badInput("Invalid Value");
- }
- }
- else if ($type == "int") {
- if (!is_int($val)) {
- self::badInput("Invalid Value");
- }
- }
- else if ($type == "float") {
- if (!is_float($val) && !is_int($val)) {
- self::badInput("Invalid Value");
- }
- }
- else if ($type == "bool") {
- if (!is_bool($val)) {
- self::badInput("Invalid Value");
- }
- }
- else if ($type == "object") {
- if (!is_array($val)) {
- self::badInput("Invalid Value");
- }
- }
- else {
- self::badInput("Unknown Type");
- }
- $setting->setValue(json_encode($val));
- }
- }
-
- public static function checkSettingName($setting_name)
- {
- $setting = LuticateSettingsDataAccess::getByName($setting_name);
- if (is_null($setting)) {
- self::notFound("Setting not found");
- }
- return $setting;
- }
-
- public static function getAll($page = 0, $perPage = 2000000000, $query = "")
- {
- return parent::getAll($page, $perPage, $query)->map(function($value)
- {
- $value->setValue(json_decode($value->getValue()));
- return $value;
- });
- }
-
- public static function getEffectiveSetting($user_id, $setting_name)
- {
- $val = LuticateSettingsDataAccess::getEffectiveSetting($user_id, $setting_name);
- if (is_null($val->getSetting())) {
- self::notFound("Setting not found");
- }
-
- $setting = LuStringUtils::convertJsonString($val->getSetting());
- $setting["Value"] = json_decode($setting["Value"]);
- return $setting;
- }
-
- public static function getEffectiveSettingValue($user_id, $setting_name)
- {
- $setting = self::getEffectiveSetting($user_id, $setting_name);
- return $setting["Value"];
- }
-
- public static function getAllEffectiveSetting($user_id)
- {
- return LuticateSettingsDataAccess::getAllEffectiveSetting($user_id)->map(function($value)
- {
- $setting = $value["Settings"];
- $setting["Value"] = json_decode($setting["Value"]);
- return $setting;
- });
- }
-
- public static function get($setting_name)
- {
- $setting = LuticateSettingsDataAccess::getByName($setting_name);
- if (is_null($setting)) {
- self::notFound("Permission not found");
- }
- $setting->setValue(json_decode($setting->getValue()));
- return $setting;
- }
-
- public static function getValue($setting_name)
- {
- $setting = self::get($setting_name);
- return $setting->getValue();
- }
-
- /**
- * @param $setting LuticateSettingsDbo
- * @return bool
- */
- public static function add($setting)
- {
- self::checkSetting($setting);
- $perm = LuticateSettingsDataAccess::getByName($setting->getName());
- if (is_null($perm)) {
- LuticateSettingsDataAccess::add($setting);
- return true;
- }
- self::badInput("Setting name already exists");
- return false;
- }
-
- public static function del($setting_name)
- {
- LuticateSettingsDataAccess::delete($setting_name);
- return true;
- }
-
- /**
- * @param $setting_name string
- * @param $setting LuticateSettingsDbo
- * @return bool
- */
- public static function edit($setting_name, $setting)
- {
- $set = self::checkSettingName($setting_name);
- $setting->setName($set->getName());
- $setting->setType($set->getType());
- self::checkSetting($setting);
- LuticateSettingsDataAccess::edit($setting_name, $setting);
- return true;
- }
- }
|