BxDolStudioRolesUtils.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. /**
  10. * Default Studio roles actions.
  11. */
  12. define('BX_SRA_MANAGE_ROLES', 'manage roles');
  13. define('BX_SRA_MANAGE_APPS', 'manage apps');
  14. class BxDolStudioRolesUtils extends BxDol implements iBxDolSingleton
  15. {
  16. protected $oDb;
  17. public function __construct()
  18. {
  19. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  20. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  21. parent::__construct();
  22. $this->oDb = BxDolStudioRolesQuery::getInstance();
  23. }
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. static function getInstance()
  30. {
  31. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioRolesUtils();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. public function getRoles($bActive = true)
  36. {
  37. return $this->oDb->getRoles(array('type' => 'all' . ($bActive ? '_active' : '')));
  38. }
  39. public function getRole($iAccountId)
  40. {
  41. $aMember = $this->oDb->getMembers(array('type' => 'by_account_id', 'account_id' => $iAccountId));
  42. if(empty($aMember) || !is_array($aMember))
  43. return 0;
  44. return (int)$aMember['role'];
  45. }
  46. public function setRole($iAccountId, $mixedRole)
  47. {
  48. if(is_array($mixedRole)) {
  49. if(count($mixedRole) != 1 || current($mixedRole) != 0) {
  50. $iAccountRole = 0;
  51. foreach($mixedRole as $iRole)
  52. $iAccountRole = $iAccountRole | pow(2, ($iRole - 1));
  53. $mixedRole = $iAccountRole;
  54. }
  55. else
  56. $mixedRole = 0;
  57. }
  58. return $this->oDb->setRole($iAccountId, $mixedRole);
  59. }
  60. public function isActionAllowed($sAction, $iAccountId = 0)
  61. {
  62. if(empty($iAccountId))
  63. $iAccountId = getLoggedId();
  64. $aAction = $this->oDb->getActions(array('type' => 'by_name', 'name' => $sAction));
  65. if(empty($aAction) || !is_array($aAction))
  66. return true;
  67. $iRole = $this->getRole($iAccountId);
  68. return $this->oDb->isActionAllowed($iRole, (int)$aAction['id']);
  69. }
  70. }
  71. /** @} */