1
0

BxBaseMenuSetAclLevel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Menu to set acl level for profile
  11. */
  12. class BxBaseMenuSetAclLevel extends BxTemplMenu
  13. {
  14. protected $bWithDuration;
  15. protected $mixedProfileId;
  16. public function __construct ($aObject, $oTemplate)
  17. {
  18. parent::__construct ($aObject, $oTemplate);
  19. $this->bWithDuration = true;
  20. }
  21. public function getCode ($mixedProfileId = 0)
  22. {
  23. $this->mixedProfileId = $mixedProfileId;
  24. if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && ($mixedProfileId = bx_get('profile_id', 'post')) && ($iAclLevelId = bx_get('level_id', 'post'))) {
  25. $mixedProfileId = urldecode($mixedProfileId);
  26. if(!is_numeric($mixedProfileId))
  27. $mixedProfileId = unserialize($mixedProfileId);
  28. echoJson($this->setMembership($mixedProfileId, $iAclLevelId, bx_get('duration', 'post') !== false ? (int)bx_get('duration', 'post') : 0, (int)bx_get('card', 'post') > 0));
  29. exit;
  30. }
  31. $mixedProfileId = $this->mixedProfileId;
  32. if(bx_get('profile_id') !== false)
  33. $mixedProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  34. if($this->bWithDuration && !is_array($mixedProfileId)) {
  35. $oForm = BxDolForm::getObjectInstance('sys_acl', 'sys_acl_set');
  36. if($oForm) {
  37. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . bx_append_url_params('menu.php', [
  38. 'o' => 'sys_set_acl_level',
  39. ]);
  40. $oForm->aInputs['profile_id']['value'] = $mixedProfileId;
  41. $oForm->aInputs['card']['value'] = 0;
  42. $oAcl = BxDolAcl::getInstance();
  43. $aAclLevels = $oAcl->getMembershipsBy(array('type' => 'all_active_not_automatic_pair'));
  44. foreach ($aAclLevels as $k => $s)
  45. $oForm->aInputs['level_id']['values'][] = ['key' => $k, 'value' => _t($s)];
  46. $aProfileAclLevel = $oAcl->getMemberMembershipInfo($mixedProfileId);
  47. $oForm->aInputs['level_id']['value'] = $aProfileAclLevel['id'];
  48. return PopupBox('sys_acl_set_' . $mixedProfileId, _t('_sys_form_popup_acl_set'), $this->_oTemplate->parseHtmlByName('acl_set.html', [
  49. 'form' => $oForm->getCode(),
  50. 'form_id' => $oForm->getId(),
  51. 'profile_id' => $mixedProfileId,
  52. ]), true);
  53. }
  54. }
  55. return parent::getCode ();
  56. }
  57. public function getMenuItems ()
  58. {
  59. $this->loadData();
  60. return parent::getMenuItems();
  61. }
  62. protected function loadData()
  63. {
  64. $mixedProfileId = $this->mixedProfileId;
  65. if(bx_get('profile_id') !== false)
  66. $mixedProfileId = bx_process_input(bx_get('profile_id'), BX_DATA_INT);
  67. $oAcl = BxDolAcl::getInstance();
  68. if (!$oAcl)
  69. return;
  70. $bBulk = is_array($mixedProfileId);
  71. if(!$bBulk) {
  72. $aProfileAclLevel = $oAcl->getMemberMembershipInfo($mixedProfileId);
  73. $this->setSelected('', $aProfileAclLevel['id']);
  74. }
  75. $aAclLevels = $oAcl->getMembershipsBy(array('type' => 'all_active_not_automatic_pair'));
  76. foreach ($aAclLevels as $k => $s)
  77. $aAclLevels[$k] = _t($s);
  78. $aItems = array();
  79. foreach ($aAclLevels as $iId => $sTitle) {
  80. $aItems[] = array(
  81. 'id' => $iId,
  82. 'name' => $iId,
  83. 'class' => '',
  84. 'title' => $sTitle,
  85. 'icon' => '',
  86. 'link' => 'javascript:void(0);',
  87. 'onclick' => "bx_set_acl_level(" . (!$bBulk ? $mixedProfileId : "'" . urlencode(serialize($mixedProfileId)) . "'") . ", {$iId}, " . bx_js_string("$(this).parents('.bx-popup-applied:first')") . ");"
  88. );
  89. }
  90. $this->_aObject['menu_items'] = $aItems;
  91. }
  92. protected function setMembership($mixedProfileId, $iAclLevelId, $iAclLevelDuration = 0, $bAclCard = false)
  93. {
  94. bx_import('BxDolAcl');
  95. if(!is_array($mixedProfileId))
  96. $mixedProfileId = array($mixedProfileId);
  97. $iPerformerId = bx_get_logged_profile_id();
  98. $aCheck = checkActionModule($iPerformerId, 'set acl level', 'system', false);
  99. if(!isAdmin() && $aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  100. return array('code' => 1, 'msg' => $aCheck[CHECK_ACTION_MESSAGE]);
  101. $iSet = 0;
  102. $aCards = array();
  103. $oAcl = BxDolAcl::getInstance();
  104. foreach($mixedProfileId as $iProfileId) {
  105. if(!$oAcl->setMembership($iProfileId, $iAclLevelId, $iAclLevelDuration, true))
  106. continue;
  107. $iSet += 1;
  108. if($bAclCard)
  109. $aCards[$iProfileId] = $oAcl->getProfileMembership($iProfileId);
  110. checkActionModule($iPerformerId, 'set acl level', 'system', true); // perform action
  111. }
  112. if(count($mixedProfileId) != $iSet)
  113. return array('code' => 2, 'msg' => _t('_error occured'));
  114. $aResult = array('code' => 0);
  115. if($bAclCard)
  116. $aResult['card'] = $aCards;
  117. return $aResult;
  118. }
  119. }
  120. /** @} */