/**
  * Add an api action permission to the local map
  * @param array $map map to fill
  * @param kApiActionPermissionItem $item
  */
 private static function addApiAction(array &$map, kApiActionPermissionItem $item)
 {
     $service = strtolower($item->getService());
     $action = strtolower($item->getAction());
     if (!isset($map[self::API_ACTIONS_ARRAY_NAME][$service])) {
         $map[self::API_ACTIONS_ARRAY_NAME][$service] = array();
         $map[self::API_ACTIONS_ARRAY_NAME][$service][$action] = array();
     } else {
         if (!in_array($action, $map[self::API_ACTIONS_ARRAY_NAME][$service], true)) {
             $map[self::API_ACTIONS_ARRAY_NAME][$service][$action] = array();
         }
     }
 }
function addActionPermissionItem($itemCfg)
{
    // verify obligatory fields
    if (!$itemCfg->service) {
        throw new Exception('Permission item service must be set');
    }
    if (!$itemCfg->action) {
        throw new Exception('Permission item action must be set');
    }
    if (is_null($itemCfg->partnerId) || $itemCfg->partnerId === '') {
        throw new Exception('Permission item partner id must be set');
    }
    // check if item already exists in db
    $c = new Criteria();
    $c->addAnd(kApiActionPermissionItem::SERVICE_COLUMN_NAME, strtolower($itemCfg->service), Criteria::EQUAL);
    $c->addAnd(kApiActionPermissionItem::ACTION_COLUMN_NAME, strtolower($itemCfg->action), Criteria::EQUAL);
    $c->addAnd(PermissionItemPeer::PARTNER_ID, array(PartnerPeer::GLOBAL_PARTNER, $itemCfg->partnerId), Criteria::IN);
    $c->addAnd(PermissionItemPeer::TYPE, PermissionItemType::API_ACTION_ITEM, Criteria::EQUAL);
    $existingItem = PermissionItemPeer::doSelectOne($c);
    $item = null;
    if ($existingItem) {
        $item = $existingItem;
        KalturaLog::log('Permission item for [' . $item->getService() . '->' . $item->getAction() . '] partner id [' . $item->getPartnerId() . '] already exists with id [' . $existingItem->getId() . ']');
    } else {
        // save new permission item object
        $item = new kApiActionPermissionItem();
        foreach ($itemCfg as $key => $value) {
            if ($key === 'permissions') {
                continue;
                // permissions are set later
            }
            $setterCallback = array($item, "set{$key}");
            call_user_func_array($setterCallback, array($value));
        }
        // service and action are always kept in lowercase
        $item->setService(strtolower($item->getService()));
        $item->setAction(strtolower($item->getAction()));
        $item->save();
        KalturaLog::log('New permission item id [' . $item->getId() . '] added for [' . $item->getService() . '->' . $item->getAction() . '] partner id [' . $item->getPartnerId() . ']');
    }
    // add item to each defined permission
    $permissionNames = array_map('trim', explode(',', $itemCfg->permissions));
    addItemToPermissions($item, $permissionNames, $itemCfg->partnerId);
}
 /**
  * Add an api action permission to the local map
  * @param array $map map to fill
  * @param kApiActionPermissionItem $item
  */
 private static function addApiAction(array &$map, kApiActionPermissionItem $item)
 {
     if (!isset($map[self::API_ACTIONS_ARRAY_NAME][$item->getService()])) {
         $map[self::API_ACTIONS_ARRAY_NAME][$item->getService()] = array();
         $map[self::API_ACTIONS_ARRAY_NAME][$item->getService()][$item->getAction()] = array();
     } else {
         if (!in_array($item->getAction(), $map[self::API_ACTIONS_ARRAY_NAME][$item->getService()], true)) {
             $map[self::API_ACTIONS_ARRAY_NAME][$item->getService()][$item->getAction()] = array();
         }
     }
 }