From 746fb9826d2834d3eb5e443a9420473e7a957530 Mon Sep 17 00:00:00 2001 From: RomanL Date: Mon, 6 Dec 2021 11:01:12 +0300 Subject: [PATCH] #3059 Add the permission check and counter to the Invites module --- inc/classes/BxDolAcl.php | 39 +++++++++++++++++++ .../boonex/invites/classes/BxInvConfig.php | 2 +- .../invites/classes/BxInvGridRequests.php | 2 +- .../boonex/invites/classes/BxInvModule.php | 16 ++++---- .../boonex/invites/classes/BxInvTemplate.php | 10 ++--- modules/boonex/invites/install/langs/en.xml | 1 - modules/boonex/invites/install/sql/enable.sql | 21 +++++----- .../russian/data/langs/bx_invites/ru.xml | 1 - 8 files changed, 62 insertions(+), 30 deletions(-) diff --git a/inc/classes/BxDolAcl.php b/inc/classes/BxDolAcl.php index 2eaad477cb..d73d72e9e3 100644 --- a/inc/classes/BxDolAcl.php +++ b/inc/classes/BxDolAcl.php @@ -416,6 +416,34 @@ function checkAction($iProfileId, $iActionId, $bPerformAction = false) $aResult[CHECK_ACTION_RESULT] = CHECK_ACTION_RESULT_ALLOWED; return $aResult; } + + /** + * Get the number of allowed action + * + * @param int $iProfileId ID of a profile that is going to perform an action + * @param int $iActionId ID of the action itself + * @param boolean $bPerformAction if true, then action information is updated, i.e. action is 'performed' + * @return int if the action is countable, or true if it's not countable + */ + function getActionNumberLeft($iProfileId, $iActionId) + { + $aMembership = $this->getMemberMembershipInfo($iProfileId); // get current profile's membership information + + $aAction = $this->oDb->getAction($aMembership['id'], $iActionId); + + $iAllowedCnt = (int)$aAction['allowed_count']; ///< Number of allowed actions. Unlimited if not specified or 0 + + if($iAllowedCnt > 0) { + $aActionTrack = $this->oDb->getActionTrack($iActionId, $iProfileId); + + if(!$aActionTrack) + return $iAllowedCnt; + + return (int)$aActionTrack['actions_left']; + } + + return true; + } /** * Get the list of existing memberships @@ -796,4 +824,15 @@ function checkActionModule($iProfileId, $sActionName, $sModuleName, $bPerformAct return $oACL->checkAction($iProfileId, $iActionId, $bPerformAction); } +function getActionNumberLeftModule($iProfileId, $sActionName, $sModuleName) +{ + $oACL = BxDolAcl::getInstance(); + + $iActionId = $oACL->getMembershipActionId($sActionName, $sModuleName); + if (!$iActionId) + bx_trigger_error("Unknown action: '$sActionName' in module '$sModuleName'", 1); + + return $oACL->getActionNumberLeft($iProfileId, $iActionId); +} + /** @} */ diff --git a/modules/boonex/invites/classes/BxInvConfig.php b/modules/boonex/invites/classes/BxInvConfig.php index 991705ada5..f187b6b76a 100644 --- a/modules/boonex/invites/classes/BxInvConfig.php +++ b/modules/boonex/invites/classes/BxInvConfig.php @@ -91,7 +91,7 @@ public function init(&$oDb) public function getCountPerUser() { - return $this->_iCountPerUser; + return getActionNumberLeftModule(bx_get_logged_profile_id(), 'invite', $this->_sName); } public function getKeyCode() diff --git a/modules/boonex/invites/classes/BxInvGridRequests.php b/modules/boonex/invites/classes/BxInvGridRequests.php index 9299ad3ac1..d903924413 100644 --- a/modules/boonex/invites/classes/BxInvGridRequests.php +++ b/modules/boonex/invites/classes/BxInvGridRequests.php @@ -171,7 +171,7 @@ public function performActionInvite($aParams = array()) continue; $iInviteId = (int)array_shift($mixedResult); - $this->_oModule->isAllowedInvite($iProfileId, true); + $this->_oModule->onInvite($iProfileId); $this->_oModule->_oDb->attachInviteToRequest($iId, $iInviteId); $aIdsAffected[] = $iId; $iAffected++; diff --git a/modules/boonex/invites/classes/BxInvModule.php b/modules/boonex/invites/classes/BxInvModule.php index de106d81e1..038a1ba22b 100644 --- a/modules/boonex/invites/classes/BxInvModule.php +++ b/modules/boonex/invites/classes/BxInvModule.php @@ -39,8 +39,7 @@ function actionGetLink() return echoJson(array('message' => $mixedAllowed)); if(!isAdmin($iAccountId)) { - $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId)); - if(($this->_oConfig->getCountPerUser() - $iInvited) <= 0) + if($this->_oConfig->getCountPerUser() <= 0) return echoJson(array('message' => _t('_bx_invites_err_limit_reached'))); } @@ -58,6 +57,7 @@ function actionGetLink() 'email' => '', 'date' => time() )); + $this->onInvite($iProfileId); echoJson(array('popup' => $this->_oTemplate->getLinkPopup( $this->getJoinLink($sKey) @@ -132,8 +132,7 @@ public function serviceGetBlockInvite() if($mixedAllowed !== true) return ''; - $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId)); - if(!isAdmin($iAccountId) && $iInvited >= $this->_oConfig->getCountPerUser()) + if(!isAdmin($iAccountId) && $this->_oConfig->getCountPerUser() <= 0) return ''; return array( @@ -419,7 +418,7 @@ public function invite($sType, $sEmails, $sText, $mixedLimit = false, $oForm = n $iInviteId = (int)$this->_oDb->insertInvite($iAccountId, $iProfileId, $sKey, $sEmail, $iDate); array_push($aAccountIds, $iInviteId); - $this->onInvite($iAccountId, $iProfileId); + $this->onInvite($iProfileId); if($mixedLimit !== false) $mixedLimit -= 1; @@ -440,15 +439,14 @@ public function processFormObjectInvite($oForm) $mixedInvites = false; if(!isAdmin($iAccountId)) { - $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId)); - $mixedInvites = $this->_oConfig->getCountPerUser() - $iInvited; - if($mixedInvites <= 0) + if( $this->_oConfig->getCountPerUser() <= 0) return _t('_bx_invites_err_limit_reached'); } $sEmails = bx_process_input($oForm->getCleanValue('emails')); $sText = bx_process_pass($oForm->getCleanValue('text')); $mixedResult = $this->invite(BX_INV_TYPE_FROM_MEMBER, $sEmails, $sText, $mixedInvites, $oForm); + $this->onInvite($iProfileId); if($mixedResult !== false) $sResult = _t('_bx_invites_msg_invitation_sent', count($mixedResult)); else @@ -541,7 +539,7 @@ protected function getBlockManage($sType) ); } - protected function onInvite($iAccountId, $iProfileId) + protected function onInvite($iProfileId) { $this->isAllowedInvite($iProfileId, true); diff --git a/modules/boonex/invites/classes/BxInvTemplate.php b/modules/boonex/invites/classes/BxInvTemplate.php index 54a4363907..0187bb992d 100644 --- a/modules/boonex/invites/classes/BxInvTemplate.php +++ b/modules/boonex/invites/classes/BxInvTemplate.php @@ -64,13 +64,11 @@ public function getBlockInvite($iAccountId, $iProfileId) { $sInvitesRemain = ''; if(!isAdmin($iAccountId)) { - $iInvites = $this->_oConfig->getCountPerUser(); - $iInvited = $this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId)); - - $sInvitesRemain = $iInvites - $iInvited; + $mInvitesRemain = $this->_oConfig->getCountPerUser(); + if ($mInvitesRemain === true) + $sInvitesRemain = _t('_bx_invites_txt_unlimited'); } - else - $sInvitesRemain = _t('_bx_invites_txt_unlimited'); + $sUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($this->_oConfig->CNF['URL_INVITE']); diff --git a/modules/boonex/invites/install/langs/en.xml b/modules/boonex/invites/install/langs/en.xml index 8b52f813d1..6784a2034b 100644 --- a/modules/boonex/invites/install/langs/en.xml +++ b/modules/boonex/invites/install/langs/en.xml @@ -89,7 +89,6 @@ - diff --git a/modules/boonex/invites/install/sql/enable.sql b/modules/boonex/invites/install/sql/enable.sql index 79e8b5c1bb..6eb4c2f953 100644 --- a/modules/boonex/invites/install/sql/enable.sql +++ b/modules/boonex/invites/install/sql/enable.sql @@ -50,7 +50,6 @@ VALUES (@iTypeId, @sName, '_bx_invites', 1); SET @iCategId = LAST_INSERT_ID(); INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `check`, `check_error`, `extra`, `order`) VALUES -('bx_invites_count_per_user', '5', @iCategId, '_bx_invites_option_count_per_user', 'digit', '', '', '', 1), ('bx_invites_key_lifetime', '7', @iCategId, '_bx_invites_option_key_lifetime', 'digit', '', '', '', 2), ('bx_invites_enable_request_invite', 'on', @iCategId, '_bx_invites_option_enable_request_invite', 'checkbox', '', '', '', 3), ('bx_invites_requests_email', '', @iCategId, '_bx_invites_option_requests_email', 'digit', '', '', '', 4), @@ -96,18 +95,18 @@ INSERT INTO `sys_acl_matrix` (`IDLevel`, `IDAction`) VALUES -- delete invite (@iModerator, @iIdActionDeleteInvite), -(@iAdministrator, @iIdActionDeleteInvite), +(@iAdministrator, @iIdActionDeleteInvite); +INSERT INTO `sys_acl_matrix` (`IDLevel`, `IDAction`, `AllowedCOunt`) VALUES -- invite -(@iAccount, @iIdActionInvite), -(@iStandard, @iIdActionInvite), -(@iUnconfirmed, @iIdActionInvite), -(@iPending, @iIdActionInvite), -(@iSuspended, @iIdActionInvite), -(@iModerator, @iIdActionInvite), -(@iAdministrator, @iIdActionInvite), -(@iPremium, @iIdActionInvite); - +(@iAccount, @iIdActionInvite, 5), +(@iStandard, @iIdActionInvite, 5), +(@iUnconfirmed, @iIdActionInvite, 5), +(@iPending, @iIdActionInvite, 5), +(@iSuspended, @iIdActionInvite, 5), +(@iModerator, @iIdActionInvite, 5), +(@iAdministrator, @iIdActionInvite, 5), +(@iPremium, @iIdActionInvite, 5); -- GRIDS INSERT INTO `sys_objects_grid` (`object`, `source_type`, `source`, `table`, `field_id`, `field_order`, `field_active`, `paginate_url`, `paginate_per_page`, `paginate_simple`, `paginate_get_start`, `paginate_get_per_page`, `filter_fields`, `filter_fields_translatable`, `filter_mode`, `sorting_fields`, `sorting_fields_translatable`, `visible_for_levels`, `override_class_name`, `override_class_file`) VALUES diff --git a/modules/boonex/russian/data/langs/bx_invites/ru.xml b/modules/boonex/russian/data/langs/bx_invites/ru.xml index 5b12c3fe78..c04575a1b0 100644 --- a/modules/boonex/russian/data/langs/bx_invites/ru.xml +++ b/modules/boonex/russian/data/langs/bx_invites/ru.xml @@ -91,7 +91,6 @@ -