Skip to content

Commit

Permalink
Ticket #3636 - Ability to select multiple membership levels for priva…
Browse files Browse the repository at this point in the history
…cy field.
  • Loading branch information
AntonLV committed Jan 10, 2022
1 parent ccc507f commit dd411c6
Show file tree
Hide file tree
Showing 16 changed files with 394 additions and 103 deletions.
155 changes: 114 additions & 41 deletions inc/classes/BxDolPrivacy.php
Expand Up @@ -15,7 +15,7 @@
define('BX_DOL_PG_FRIENDS_SELECTED', '6');
define('BX_DOL_PG_RELATIONS', '7');
define('BX_DOL_PG_RELATIONS_SELECTED', '8');
define('BX_DOL_PG_ACL', '9');
define('BX_DOL_PG_MEMBERSHIPS_SELECTED', '9');
define('BX_DOL_PG_CUSTOM', '99');

define('BX_DOL_PG_DEFAULT', BX_DOL_PG_ALL);
Expand Down Expand Up @@ -71,8 +71,9 @@ class BxDolPrivacy extends BxDolFactory implements iBxDolFactoryObject
protected $_aGroupsSettings;
protected $_aGroupsExclude;

protected $_sFormUsers;
protected $_sFormDisplayUsersSelect;
protected $_sFormGroupCustom;
protected $_sFormDisplayGcMembers;
protected $_sFormDisplayGcMemberships;

/**
* Constructor
Expand All @@ -90,19 +91,39 @@ protected function __construct($aObject)

$this->_aGroupsSettings = [
BX_DOL_PG_FRIENDS_SELECTED => [
'name' => 'friends',
'is_allowed' => '',
'connection' => 'sys_profiles_friends',
'js_method_create_group' => 'selectUsers'
'js_method_create_group' => 'selectMembers',
'db_table_items' => 'sys_privacy_groups_custom_members',
'db_field_item' => 'member_id',
'uri_get_items' => 'users_list',
],
BX_DOL_PG_RELATIONS_SELECTED => [
'name' => 'relations',
'is_allowed' => '',
'connection' => 'sys_profiles_relations',
'js_method_create_group' => 'selectUsers'
'js_method_create_group' => 'selectMembers',
'db_table_items' => 'sys_privacy_groups_custom_members',
'db_field_item' => 'member_id',
'uri_get_items' => 'users_list',
],
BX_DOL_PG_MEMBERSHIPS_SELECTED => [
'name' => 'memberships',
'is_allowed' => 'isAllowedMemberships',
'connection' => '',
'js_method_create_group' => 'selectMemberships',
'db_table_items' => 'sys_privacy_groups_custom_memberships',
'db_field_item' => 'membership_id',
'uri_get_items' => 'memberships_list',
]
];

$this->_aGroupsExclude = array();

$this->_sFormUsers = 'sys_privacy_group_custom';
$this->_sFormDisplayUsersSelect = 'sys_privacy_group_custom_manage';
$this->_sFormGroupCustom = 'sys_privacy_group_custom';
$this->_sFormDisplayGcMembers = 'sys_privacy_group_custom_members';
$this->_sFormDisplayGcMemberships = 'sys_privacy_group_custom_memberships';
}

/**
Expand Down Expand Up @@ -154,12 +175,10 @@ public static function getGroupChooser($sObject, $iOwnerId = 0, $aParams = array
if(empty($sValue))
$sValue = $oPrivacy->_oDb->getDefaultGroup($sModule, $sAction);

$aValues = $oPrivacy->getGroups();
$aValues = $oPrivacy->getGroups($iOwnerId, $aParams);

$aValues = $oPrivacy->addDynamicGroups($aValues, $iOwnerId, $aParams);

$aValues = $oPrivacy->addAclGroups($aValues, $iOwnerId, $aParams);

$aValues = $oPrivacy->addSpaces($aValues, $iOwnerId, $aParams);

$sName = $oPrivacy->convertActionToField($sAction);
Expand Down Expand Up @@ -230,7 +249,7 @@ public function actionLoadGroupCustom()
$iContentId = (int)bx_get('content_id');
$iGroupId = (int)bx_get('group_id');

$oForm = BxDolForm::getObjectInstance($this->_sFormUsers, $this->_sFormDisplayUsersSelect);
$oForm = BxDolForm::getObjectInstance($this->_sFormGroupCustom, $this->_sFormDisplayGcMembers);
return echoJson(array('eval' => $this->getJsObjectName() . '.onSelectGroup(oData);', 'content' => $oForm->getElementGroupCustom(array(
'profile_id' => $iProfileId,
'content_id' => $iContentId,
Expand All @@ -239,7 +258,7 @@ public function actionLoadGroupCustom()
))));
}

public function actionSelectGroup()
public function actionSelectMembers()
{
$aValues = array(
'profile_id' => (int)bx_get('profile_id'),
Expand All @@ -258,12 +277,37 @@ public function actionSelectGroup()
if($aValues['profile_id'] != bx_get_logged_profile_id())
return echoJson(array('msg' => _t('_sys_ps_ferr_incorrect_gc_owner')));

if(($mixedResult = $this->isSelectGroupCustom($aValues)) !== true)
if(($mixedResult = $this->isSelectGroupCustomUsers($aValues)) !== true)
return echoJson(array('msg' => $mixedResult));

return echoJson($this->getSelectGroup($aValues, $aParams));
}

public function actionSelectMemberships()
{
$aValues = array(
'profile_id' => (int)bx_get('profile_id'),
'content_id' => (int)bx_get('content_id'),
'object' => $this->_sObject,
'group_id' => (int)bx_get('group_id')
);

$aParams = array(
'popup_only' => (bool)bx_get('popup_only')
);

if(!isset($this->_aGroupsSettings[$aValues['group_id']]))
return echoJson(array());

if($aValues['profile_id'] != bx_get_logged_profile_id())
return echoJson(array('msg' => _t('_sys_ps_ferr_incorrect_gc_owner')));

if(($mixedResult = $this->isSelectGroupCustomMemberships($aValues)) !== true)
return echoJson(array('msg' => $mixedResult));

return echoJson($this->getSelectMemberships($aValues, $aParams));
}

public function actionUsersList()
{
$iGroup = (int)bx_get('group');
Expand Down Expand Up @@ -304,7 +348,7 @@ public function isGroupsCustom()
{
$aGroups = $this->_oDb->getGroupsBy(array('type' => 'active_list'));

return in_array(BX_DOL_PG_FRIENDS_SELECTED, $aGroups) || in_array(BX_DOL_PG_RELATIONS_SELECTED, $aGroups);
return in_array(BX_DOL_PG_FRIENDS_SELECTED, $aGroups) || in_array(BX_DOL_PG_RELATIONS_SELECTED, $aGroups) || in_array(BX_DOL_PG_MEMBERSHIPS_SELECTED, $aGroups);
}

public function getGroupsBy($aParams)
Expand Down Expand Up @@ -378,24 +422,6 @@ public function addDynamicGroups($aValues, $iOwnerId, $aParams)
return $aValues;
}

public function addAclGroups($aValues, $iOwnerId, $aParams)
{
$aCheck = checkActionModule($iOwnerId, 'show membership levels in privacy groups', 'system', false);
if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
return $aValues;

$aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true);
if(empty($aLevels) || !is_array($aLevels))
return $aValues;

$aValues[] = array('type' => 'group_header', 'value' => mb_strtoupper(_t('_sys_privacy_groups_acl')));
foreach($aLevels as $iId => $sTitle)
$aValues[] = array('key' => 'ml' . $iId, 'value' => $sTitle);
$aValues[] = array('type' => 'group_end');

return $aValues;
}

public function addSpaces($aValues, $iOwnerId, $aParams)
{
if (!$this->_aObject['spaces'])
Expand Down Expand Up @@ -599,10 +625,12 @@ public function checkFriendsSelectedByObject($aObject, $iViewerId)
'profile_id' => $aObject['owner_id'],
'content_id' => $aObject['id'],
'object' => $this->_sObject,
'group_id' => $aObject['group_id']
'group_id' => $aObject['group_id'],
'group_items_table' => $this->_aGroupsSettings[$aObject['group_id']]['db_table_items'],
'group_items_field' => $this->_aGroupsSettings[$aObject['group_id']]['db_field_item']
));

return !empty($aGroupCustom['members']) && is_array($aGroupCustom['members']) && in_array($iViewerId, $aGroupCustom['members']);
return !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']) && in_array($iViewerId, $aGroupCustom['items']);
}

public function checkRelations($iOwnerId, $iViewerId)
Expand All @@ -620,10 +648,27 @@ public function checkRelationsSelectedByObject($aObject, $iViewerId)
'profile_id' => $aObject['owner_id'],
'content_id' => $aObject['id'],
'object' => $this->_sObject,
'group_id' => $aObject['group_id']
'group_id' => $aObject['group_id'],
'group_items_table' => $this->_aGroupsSettings[$aObject['group_id']]['db_table_items'],
'group_items_field' => $this->_aGroupsSettings[$aObject['group_id']]['db_field_item']
));

return !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']) && in_array($iViewerId, $aGroupCustom['items']);
}

public function checkMembershipsSelectedByObject($aObject, $iViewerId)
{
$aGroupCustom = $this->getGroupCustom(array(
'type' => 'pcog_ext',
'profile_id' => $aObject['owner_id'],
'content_id' => $aObject['id'],
'object' => $this->_sObject,
'group_id' => $aObject['group_id'],
'group_items_table' => $this->_aGroupsSettings[$aObject['group_id']]['db_table_items'],
'group_items_field' => $this->_aGroupsSettings[$aObject['group_id']]['db_field_item']
));

return !empty($aGroupCustom['members']) && is_array($aGroupCustom['members']) && in_array($iViewerId, $aGroupCustom['members']);
return !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']) && BxDolAcl::getInstance()->isMemberLevelInSet($aGroupCustom['items'], $iViewerId);
}

public function checkCustomByObject($aObject, $iViewerId)
Expand Down Expand Up @@ -688,31 +733,59 @@ protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewer
/**
* get privacy groups for getGroupChooser
*/
protected function getGroups()
protected function getGroups($iOwnerId = 0, $aParams = [])
{
$aValues = array();

$aGroups = $this->_oDb->getGroupsBy(array('type' => 'active'));
foreach($aGroups as $aGroup) {
if((int)$aGroup['active'] == 0 || in_array($aGroup['id'], $this->_aGroupsExclude))
$iGroupId = (int)$aGroup['id'];

if((int)$aGroup['active'] == 0 || in_array($iGroupId, $this->_aGroupsExclude))
continue;

if(isset($this->_aGroupsSettings[$iGroupId]) && !empty($this->_aGroupsSettings[$iGroupId]['is_allowed'])) {
$sMethodIsAllowed = $this->_aGroupsSettings[$iGroupId]['is_allowed'];
if(method_exists($this, $sMethodIsAllowed) && $this->$sMethodIsAllowed($iOwnerId) !== true)
continue;
}

$aValues[] = array('key' => $aGroup['id'], 'value' => _t($aGroup['title']));
}

return $aValues;
}

/**
* Check whethere custom group is allowed in current circumstances.
* Check whethere a custom group (based on users list) is allowed in current circumstances.
* NOTE. Can be overwritten if it's needed.
*
* @param type $aParams an array of parameters.
* @return boolean result of operation.
*/
protected function isSelectGroupCustom($aParams)
protected function isSelectGroupCustomUsers($aParams)
{
return true;
}


/**
* Check whethere a custom group (based on memberships list) is allowed in current circumstances.
* NOTE. Can be overwritten if it's needed.
*
* @param type $aParams an array of parameters.
* @return boolean result of operation.
*/
protected function isSelectGroupCustomMemberships($aParams)
{
return true;
}

public function isAllowedMemberships($iUserId)
{
$aCheck = checkActionModule($iUserId, 'show membership levels in privacy groups', 'system', false);
return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
}

/**
* Check whether the viewer can make requested action.
*
Expand Down
46 changes: 33 additions & 13 deletions inc/classes/BxDolPrivacyQuery.php
Expand Up @@ -193,8 +193,8 @@ public function getGroupCustom($aParams)
'id' => $aParams['id']
);

$sSelectClause .= ", GROUP_CONCAT(`tgm`.`member_id` SEPARATOR '" . $sDiv . "') AS `members`";
$sJoinClause = "LEFT JOIN `sys_privacy_groups_custom_members` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
$sSelectClause .= ", GROUP_CONCAT(`tgm`.`" . $aParams['group_items_field'] . "` SEPARATOR '" . $sDiv . "') AS `items`";
$sJoinClause = "LEFT JOIN `" . $aParams['group_items_table'] . "` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
$sWhereClause = " AND `tg`.`id`=:id";
$sGroupClause = "`tg`.`id`";
break;
Expand All @@ -219,8 +219,8 @@ public function getGroupCustom($aParams)
'group_id' => $aParams['group_id'],
);

$sSelectClause .= ", GROUP_CONCAT(`tgm`.`member_id` SEPARATOR '" . $sDiv . "') AS `members`";
$sJoinClause = "LEFT JOIN `sys_privacy_groups_custom_members` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
$sSelectClause .= ", GROUP_CONCAT(`tgi`.`" . $aParams['group_items_field'] . "` SEPARATOR '" . $sDiv . "') AS `items`";
$sJoinClause = "LEFT JOIN `" . $aParams['group_items_table'] . "` AS `tgi` ON `tg`.`id`=`tgi`.`group_id`";
$sWhereClause = " AND `tg`.`profile_id`=:profile_id AND (`tg`.`content_id`=:content_id" . (!empty($aParams['content_id']) ? " OR `tg`.`content_id`='0'" : "") . ") AND `tg`.`object`=:object AND `tg`.`group_id`=:group_id";
$sGroupClause = "`tg`.`id`";
break;
Expand All @@ -245,7 +245,7 @@ public function getGroupCustom($aParams)

$aResult = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
if(in_array($aParams['type'], array('id_ext', 'pcog_ext')) && !empty($aResult) && is_array($aResult))
$aResult['members'] = explode($sDiv, $aResult['members']);
$aResult['items'] = explode($sDiv, $aResult['items']);

return $aResult;
}
Expand Down Expand Up @@ -289,21 +289,25 @@ public function deleteGroupCustom($aParamsWhere)

public function insertGroupCustomMember($aParamsSet)
{
if(empty($aParamsSet))
return false;

return $this->query("INSERT IGNORE INTO `sys_privacy_groups_custom_members` SET " . $this->arrayToSQL($aParamsSet));
return $this->_insertGroupCustomItem('sys_privacy_groups_custom_members', $aParamsSet);
}

public function deleteGroupCustomMember($aParamsWhere)
{
if(empty($aParamsWhere))
return false;
return $this->_deleteGroupCustomItem('sys_privacy_groups_custom_members', $aParamsWhere);
}

return $this->query("DELETE FROM `sys_privacy_groups_custom_members` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
public function insertGroupCustomMembership($aParamsSet)
{
return $this->_insertGroupCustomItem('sys_privacy_groups_custom_memberships', $aParamsSet);
}

function getContentByGroupAsSQLPart($sField, $mixedGroupId)
public function deleteGroupCustomMembership($aParamsWhere)
{
return $this->_deleteGroupCustomItem('sys_privacy_groups_custom_memberships', $aParamsWhere);
}

public function getContentByGroupAsSQLPart($sField, $mixedGroupId)
{
if(is_array($mixedGroupId))
$sWhere = " AND `" . $this->_sTable . "`.`" . $sField . "` IN (" . $this->implode_escape($mixedGroupId) . ")";
Expand All @@ -330,6 +334,22 @@ protected function _getAction($sModule, $sAction)
$sCacheKey = $this->_sCachePrivacyObject . $sModule . '_' . $sAction;
return $this->fromCache($sCacheKey, 'getRow', $sQuery);
}

protected function _insertGroupCustomItem($sTable, $aParamsSet)
{
if(empty($aParamsSet))
return false;

return $this->query("INSERT IGNORE INTO `" . $sTable . "` SET " . $this->arrayToSQL($aParamsSet));
}

protected function _deleteGroupCustomItem($sTable, $aParamsWhere)
{
if(empty($aParamsWhere))
return false;

return $this->query("DELETE FROM `" . $sTable . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
}
}

/** @} */

0 comments on commit dd411c6

Please sign in to comment.