<commit_before><?php
/**
 * @package deployment
 * @subpackage dragonfly.roles_and_permissions
 * 
 * remove permissions for admin console partner configuration page
 * 
 * No need to re-run after server code deploy
 */


require_once(dirname(__FILE__).'/../../../bootstrap.php');

//remove from all roles other than 'System Administrator' the permission systemPartner.SYSTEM_ADMIN_PUBLISHER_CONFIG_PACKAGES_SERVICE
$permissionRoleMap = array(
	'System Administrator' => 'systemPartner.SYSTEM_ADMIN_PUBLISHER_CONFIG_PACKAGES_SERVICE',
);

foreach ($permissionRoleMap as $roleName => $permissionList)
{
	echo "get  permissions $roleName" . PHP_EOL;
	$role = getByNameAndPartnerId($roleName, -2);
	if (!$role) {
		KalturaLog::err('ERROR - Cannot find role with name ['.$roleName.']');
	}
	else {
		echo "remove permissions to $roleName" . PHP_EOL;
		removePermissionsToRole($role, $permissionList);
	}
}

function getByNameAndPartnerId($roleName, $partnerId)
{
	$c = new Criteria();
	$c->addAnd(UserRolePeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
	$c->addAnd(UserRolePeer::NAME, $roleName, Criteria::NOT_EQUAL);
	UserRolePeer::setUseCriteriaFilter(false);
	$userRole = UserRolePeer::doSelectOne($c);
	UserRolePeer::setUseCriteriaFilter(true);
	return $userRole;
}

function removePermissionsToRole($role, $permissionList)
{
	$currentPermissions = $role->getPermissionNames();
	$currentPermissionsArray = explode(',', $currentPermissions);
	$permissionsToRemoveArray = explode(',', $permissionList);
	$tempArray = array();
	foreach ($currentPermissionsArray as $key => $perm)
	{
		if ($perm == '')
		{
			unset($currentPermissionsArray[$key]);
		}
		elseif (in_array($perm, $permissionsToRemoveArray)) 
		{
			unset($currentPermissionsArray[$key]);
		}
		else 
		{
			KalturaLog::log('Role name ['.$role->getName().'] does nto have permission ['.$perm.']');
		}
	}
	
	$currentPermissions = implode(',', $currentPermissionsArray);
	$role->setPermissionNames($currentPermissions);
	$role->save();
	KalturaLog::log('Added permission ['.$currentPermissions.'] to role name ['.$role->getName().']');
}
<commit_msg>qnd: fixed permission: remove permission to extend free trial from all user (but admin)<commit_after><?php
/**
 * @package deployment
 * @subpackage dragonfly.roles_and_permissions
 * 
 * remove permissions for admin console partner configuration page
 * 
 * No need to re-run after server code deploy
 */


require_once(dirname(__FILE__).'/../../../bootstrap.php');

$permissionToRemove = 'systemPartner.SYSTEM_ADMIN_PUBLISHER_CONFIG_PACKAGES_SERVICE,systemPartner.SYSTEM_ADMIN_PUBLISHER_CONFIG_OPTIONS_MONITOR_USAGE';
$userRoleNameToKeepPermission = 'System Administrator';


$userRoles = getUserRolesToRemovePermission($userRoleNameToKeepPermission, -2);
foreach ($userRoles as $userRole)
{
	echo "remove permissions to " . $userRole->getName() .  ' role id: ' . $userRole->getId() . PHP_EOL;
	removePermissionsToRole($userRole, $permissionToRemove);
}

function getUserRolesToRemovePermission($userRoleNameToKeepPermission, $partnerId)
{
	$c = new Criteria();
	$c->addAnd(UserRolePeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
	$c->addAnd(UserRolePeer::NAME, $userRoleNameToKeepPermission, Criteria::NOT_EQUAL);
	UserRolePeer::setUseCriteriaFilter(false);
	$userRole = UserRolePeer::doSelect($c);
	UserRolePeer::setUseCriteriaFilter(true);
	return $userRole;
}

function removePermissionsToRole($role, $permissionToRemove)
{
	$currentPermissions = $role->getPermissionNames();
	$currentPermissionsArray = explode(',', $currentPermissions);
	$tempArray = array();
	foreach ($currentPermissionsArray as $key => $perm)
	{
		if ($perm == '')
		{
			unset($currentPermissionsArray[$key]);
		}
		elseif ($perm == $permissionToRemove) 
		{
			unset($currentPermissionsArray[$key]);
		}
	}
	
	$currentPermissions = implode(',', $currentPermissionsArray);
	$role->setPermissionNames($currentPermissions);
	$role->save();
	KalturaLog::log('Added permission ['.$currentPermissions.'] to role name ['.$role->getName().']');
}

{'fraction_non_alphanumeric': 0.14867212707867958, 'fraction_numerical': 0.0004964010920824026, 'mean_word_length': 6.476808905380334, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065336', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1275, 'n_words': 244}