$partnerId = $argv[1] == 'null' ? null : $argv[1];
$roleName = $argv[2];
$parmissionNames = explode(',', $argv[3]);
$criteria = new Criteria();
if (!is_null($partnerId)) {
    $criteria->addAnd(UserRolePeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
}
$criteria->addAnd(UserRolePeer::NAME, $roleName, Criteria::EQUAL);
$criteria->addAscendingOrderByColumn(UserRolePeer::ID);
$criteria->setLimit($page);
$userRoles = UserRolePeer::doSelect($criteria);
while (count($userRoles)) {
    KalturaLog::info("[" . count($userRoles) . "] user roles .");
    foreach ($userRoles as $userRole) {
        foreach ($parmissionNames as $parmissionName) {
            addPermissionsToRole($userRole, $parmissionName);
        }
    }
    kMemoryManager::clearMemory();
    $nextCriteria = clone $criteria;
    $nextCriteria->add(UserRolePeer::ID, $userRole->getId(), Criteria::GREATER_THAN);
    $userRoles = UserRolePeer::doSelect($nextCriteria);
    usleep(100);
}
KalturaLog::info("Done");
function addPermissionsToRole($role, $permissionList)
{
    $currentPermissions = $role->getPermissionNames(false, true);
    if (UserRole::ALL_PARTNER_PERMISSIONS_WILDCARD == $currentPermissions) {
        return;
    }
 * reports (due to permissions * on his role)
 * 
 * This is a temporary solution as there is no differentiation between backend, ps & projects
 * 
 * No need to re-run after server code deploy
 */
require_once dirname(__FILE__) . '/../../../bootstrap.php';
$permissionRoleMap = array('PS Engineer' => 'SYSTEM_ADMIN_REPORTS_READ');
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 "add permissions to {$roleName}" . PHP_EOL;
        addPermissionsToRole($role, $permissionList);
    }
}
function getByNameAndPartnerId($roleName, $partnerId)
{
    $c = new Criteria();
    $c->addAnd(UserRolePeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
    $c->addAnd(UserRolePeer::NAME, $roleName, Criteria::EQUAL);
    UserRolePeer::setUseCriteriaFilter(false);
    $userRole = UserRolePeer::doSelectOne($c);
    UserRolePeer::setUseCriteriaFilter(true);
    return $userRole;
}
function addPermissionsToRole($role, $permissionList)
{
    $currentPermissions = $role->getPermissionNames();