Skip to content

Commit

Permalink
#3059 Add the permission check and counter to the Invites module
Browse files Browse the repository at this point in the history
  • Loading branch information
romanlesnikov committed Dec 6, 2021
1 parent 69974cb commit 746fb98
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
39 changes: 39 additions & 0 deletions inc/classes/BxDolAcl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/** @} */
2 changes: 1 addition & 1 deletion modules/boonex/invites/classes/BxInvConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion modules/boonex/invites/classes/BxInvGridRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
16 changes: 7 additions & 9 deletions modules/boonex/invites/classes/BxInvModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
}

Expand All @@ -58,6 +57,7 @@ function actionGetLink()
'email' => '',
'date' => time()
));
$this->onInvite($iProfileId);

echoJson(array('popup' => $this->_oTemplate->getLinkPopup(
$this->getJoinLink($sKey)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -541,7 +539,7 @@ protected function getBlockManage($sType)
);
}

protected function onInvite($iAccountId, $iProfileId)
protected function onInvite($iProfileId)
{
$this->isAllowedInvite($iProfileId, true);

Expand Down
10 changes: 4 additions & 6 deletions modules/boonex/invites/classes/BxInvTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down
1 change: 0 additions & 1 deletion modules/boonex/invites/install/langs/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@

<!-- Options -->
<string name="_bx_invites_option_automatically_befriend"><![CDATA[Enable automatically befriend inviter and invited]]></string>
<string name="_bx_invites_option_count_per_user"><![CDATA[How many invitations one user can send]]></string>
<string name="_bx_invites_option_key_lifetime"><![CDATA[Invitation key lifetime (in days)]]></string>
<string name="_bx_invites_option_enable_request_invite"><![CDATA[Enable invitation requests by email]]></string>
<string name="_bx_invites_option_requests_notifications"><![CDATA[Enable invitation requests in notifications]]></string>
Expand Down
21 changes: 10 additions & 11 deletions modules/boonex/invites/install/sql/enable.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion modules/boonex/russian/data/langs/bx_invites/ru.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@

<!-- Options -->
<string name="_bx_invites_option_automatically_befriend"><![CDATA[Автоматическая дружба между приглашающим и приглашенным]]></string>
<string name="_bx_invites_option_count_per_user"><![CDATA[Сколько приглашений может послать один пользователь]]></string>
<string name="_bx_invites_option_key_lifetime"><![CDATA[Срок действия ключа приглашения (в днях)]]></string>
<string name="_bx_invites_option_enable_request_invite"><![CDATA[Разрешить запросы на приглашение]]></string>
<string name="_bx_invites_option_requests_email"><![CDATA[Эл. адреса для посылки запросов на приглашение (разделенение запятой)]]></string>
Expand Down

0 comments on commit 746fb98

Please sign in to comment.