BxDolStudioRolesUtils.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. function __construct()
  17. {
  18. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  19. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  20. parent::__construct();
  21. $this->oDb = BxDolStudioRolesQuery::getInstance();
  22. }
  23. public function __clone()
  24. {
  25. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  26. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  27. }
  28. static function getInstance()
  29. {
  30. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  31. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolStudioRolesUtils();
  32. return $GLOBALS['bxDolClasses'][__CLASS__];
  33. }
  34. public function getRoles($bActive = true)
  35. {
  36. return $this->oDb->getRoles(array('type' => 'all' . ($bActive ? '_active' : '')));
  37. }
  38. public function getRole($iAccountId)
  39. {
  40. $aMember = $this->oDb->getMembers(array('type' => 'by_account_id', 'account_id' => $iAccountId));
  41. if(empty($aMember) || !is_array($aMember))
  42. return 0;
  43. return (int)$aMember['role'];
  44. }
  45. public function setRole($iAccountId, $mixedRole)
  46. {
  47. if(is_array($mixedRole)) {
  48. if(count($mixedRole) != 1 || current($mixedRole) != 0) {
  49. $iAccountRole = 0;
  50. foreach($mixedRole as $iRole)
  51. $iAccountRole = $iAccountRole | pow(2, ($iRole - 1));
  52. $mixedRole = $iAccountRole;
  53. }
  54. else
  55. $mixedRole = 0;
  56. }
  57. return $this->oDb->setRole($iAccountId, $mixedRole);
  58. }
  59. public function isActionAllowed($sAction, $iAccountId = 0)
  60. {
  61. if(empty($iAccountId))
  62. $iAccountId = getLoggedId();
  63. $aAction = $this->oDb->getActions(array('type' => 'by_name', 'name' => $sAction));
  64. if(empty($aAction) || !is_array($aAction))
  65. return true;
  66. $iRole = $this->getRole($iAccountId);
  67. return $this->oDb->isActionAllowed($iRole, (int)$aAction['id']);
  68. }
  69. }
  70. /** @} */