<commit_before><?php

/**
 * Permission service lets you create and manage user permissions
 * @service permission
 * @package api
 * @subpackage services
 */
class PermissionService extends KalturaBaseService
{
	public function initService($serviceId, $serviceName, $actionName)
	{
		parent::initService($serviceId, $serviceName, $actionName);

		myPartnerUtils::addPartnerToCriteria(new PermissionPeer(), $this->getPartnerId(), $this->private_partner_data, $this->partnerGroup());
		myPartnerUtils::addPartnerToCriteria(new PermissionItemPeer(), $this->getPartnerId(), $this->private_partner_data, $this->partnerGroup());
	}
	
	protected function globalPartnerAllowed($actionName)
	{
		if ($actionName === 'get') {
			return true;
		}
		if ($actionName === 'list') {
			return true;
		}
		return parent::globalPartnerAllowed($actionName);
	}
	
	protected function partnerRequired($actionName)
	{
		if ($actionName === 'getCurrentPermissions') {
			return false;
		}
		return parent::partnerRequired($actionName);
	}

	
	/**
	 * Allows you to add a new KalturaPermission object
	 * 
	 * @action add
	 * @param KalturaPermission $permission
	 * @return KalturaPermission
	 * 
	 * @throws KalturaErrors::PROPERTY_VALIDATION_CANNOT_BE_NULL
	 * @throws KalturaErrors::PROPERTY_VALIDATION_NOT_UPDATABLE
	 */
	public function addAction(KalturaPermission $permission)
	{
		$permission->validateForInsert();
		$permission->validatePropertyNotNull('name');

		if (!$permission->friendlyName) {
			$permission->friendlyName = $permission->name;
		}
		
		if (!$permission->status) {
			$permission->status = KalturaPermissionStatus::ACTIVE;
		}
		
											
		$dbPermission = $permission->toInsertableObject();
		
		$dbPermission->setType(PermissionType::NORMAL);  // only normal permission types are added through this services
		$dbPermission->setPartnerId($this->getPartnerId());
		
		try { PermissionPeer::addToPartner($dbPermission, $this->getPartnerId()); }
		catch (kPermissionException $e) {
			$code = $e->getCode();
			if ($code === kPermissionException::PERMISSION_ALREADY_EXISTS) {
				throw new KalturaAPIException(KalturaErrors::PERMISSION_ALREADY_EXISTS, $permission->getName(), $this->getPartnerId());
			}
			if ($code === kPermissionException::PERMISSION_ITEM_NOT_FOUND) {
				throw new KalturaAPIException(KalturaErrors::PERMISSION_ITEM_NOT_FOUND);
			}			
			throw $e;
		}
		
		$permission = new KalturaPermission();
		$permission->fromObject($dbPermission);
		
		return $permission;
	}
	
	/**
	 * Retrieve a KalturaPermission object by ID
	 * 
	 * @action get
	 * @param string $permissionName 
	 * @return KalturaPermission
	 * 
	 * @throws KalturaErrors::INVALID_OBJECT_ID
	 */		
	public function getAction($permissionName)
	{
		$dbPermission = PermissionPeer::getByNameAndPartner($permissionName, explode(',', $this->partnerGroup()));
		
		if (!$dbPermission) {
			throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $permissionName);
		}
			
		$permission = new KalturaPermission();
		$permission->fromObject($dbPermission);
		
		return $permission;
	}


	/**
	 * Update an existing KalturaPermission object
	 * 
	 * @action update
	 * @param string $permissionName
	 * @param KalturaPermission $permission
	 * @return KalturaPermission
	 *
	 * @throws KalturaErrors::INVALID_OBJECT_ID
	 */	
	public function updateAction($permissionName, KalturaPermission $permission)
	{
		$dbPermission = PermissionPeer::getByNameAndPartner($permissionName, explode(',', $this->partnerGroup()));
		
		if (!$dbPermission) {
			throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $permissionName);
		}
		
		// only normal permission types are allowed for updating through this service
		if ($dbPermission->getType() !== PermissionType::NORMAL)
		{
			throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $permissionName);
		}
		
		if ($permission->name && $permission->name != $permissionName)
		{
			$existingPermission = PermissionPeer::getByNameAndPartner($permission->name, array($dbPermission->getPartnerId(), PartnerPeer::GLOBAL_PARTNER));
			if ($existingPermission)
			{
				throw new KalturaAPIException(KalturaErrors::PERMISSION_ALREADY_EXISTS, $permission->name, $this->getPartnerId());
			}
		}
		
		$dbPermission = $permission->toUpdatableObject($dbPermission);
		try
		{
			$dbPermission->save();
		}<commit_msg>Permission names shouldn't contain commas ","<commit_after><?php

/**
 * Permission service lets you create and manage user permissions
 * @service permission
 * @package api
 * @subpackage services
 */
class PermissionService extends KalturaBaseService
{
	public function initService($serviceId, $serviceName, $actionName)
	{
		parent::initService($serviceId, $serviceName, $actionName);

		myPartnerUtils::addPartnerToCriteria(new PermissionPeer(), $this->getPartnerId(), $this->private_partner_data, $this->partnerGroup());
		myPartnerUtils::addPartnerToCriteria(new PermissionItemPeer(), $this->getPartnerId(), $this->private_partner_data, $this->partnerGroup());
	}
	
	protected function globalPartnerAllowed($actionName)
	{
		if ($actionName === 'get') {
			return true;
		}
		if ($actionName === 'list') {
			return true;
		}
		return parent::globalPartnerAllowed($actionName);
	}
	
	protected function partnerRequired($actionName)
	{
		if ($actionName === 'getCurrentPermissions') {
			return false;
		}
		return parent::partnerRequired($actionName);
	}

	
	/**
	 * Allows you to add a new KalturaPermission object
	 * 
	 * @action add
	 * @param KalturaPermission $permission
	 * @return KalturaPermission
	 * 
	 * @throws KalturaErrors::PROPERTY_VALIDATION_CANNOT_BE_NULL
	 * @throws KalturaErrors::PROPERTY_VALIDATION_NOT_UPDATABLE
	 */
	public function addAction(KalturaPermission $permission)
	{
		$permission->validateForInsert();
		$permission->validatePropertyNotNull('name');
		
		if (strpos($permission->name, ',') !== false) {
			throw new KalturaAPIException(KalturaErrors::INVALID_FIELD_VALUE, 'name');
		}

		if (!$permission->friendlyName) {
			$permission->friendlyName = $permission->name;
		}
		
		if (!$permission->status) {
			$permission->status = KalturaPermissionStatus::ACTIVE;
		}
		
											
		$dbPermission = $permission->toInsertableObject();
		
		$dbPermission->setType(PermissionType::NORMAL);  // only normal permission types are added through this services
		$dbPermission->setPartnerId($this->getPartnerId());
		
		try { PermissionPeer::addToPartner($dbPermission, $this->getPartnerId()); }
		catch (kPermissionException $e) {
			$code = $e->getCode();
			if ($code === kPermissionException::PERMISSION_ALREADY_EXISTS) {
				throw new KalturaAPIException(KalturaErrors::PERMISSION_ALREADY_EXISTS, $permission->getName(), $this->getPartnerId());
			}
			if ($code === kPermissionException::PERMISSION_ITEM_NOT_FOUND) {
				throw new KalturaAPIException(KalturaErrors::PERMISSION_ITEM_NOT_FOUND);
			}			
			throw $e;
		}
		
		$permission = new KalturaPermission();
		$permission->fromObject($dbPermission);
		
		return $permission;
	}
	
	/**
	 * Retrieve a KalturaPermission object by ID
	 * 
	 * @action get
	 * @param string $permissionName 
	 * @return KalturaPermission
	 * 
	 * @throws KalturaErrors::INVALID_OBJECT_ID
	 */		
	public function getAction($permissionName)
	{
		$dbPermission = PermissionPeer::getByNameAndPartner($permissionName, explode(',', $this->partnerGroup()));
		
		if (!$dbPermission) {
			throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $permissionName);
		}
			
		$permission = new KalturaPermission();
		$permission->fromObject($dbPermission);
		
		return $permission;
	}


	/**
	 * Update an existing KalturaPermission object
	 * 
	 * @action update
	 * @param string $permissionName
	 * @param KalturaPermission $permission
	 * @return KalturaPermission
	 *
	 * @throws KalturaErrors::INVALID_OBJECT_ID
	 */	
	public function updateAction($permissionName, KalturaPermission $permission)
	{
		$dbPermission = PermissionPeer::getByNameAndPartner($permissionName, explode(',', $this->partnerGroup()));
		
		if (!$dbPermission) {
			throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $permissionName);
		}
		
		// only normal permission types are allowed for updating through this service
		if ($dbPermission->getType() !== PermissionType::NORMAL)
		{
			throw new KalturaAPIException(KalturaErrors::INVALID_OBJECT_ID, $permissionName);
		}
		
		if ($permission->name && $permission->name != $permissionName)
		{
			if (strpos($permission->name, ',') !== false) {
				throw new KalturaAPIException(KalturaErrors::INVALID_FIELD_VALUE, 'name');
			}
			
			$existingPermission = PermissionPeer::getByNameAndPartner($permission->name, array($dbPermission->getPartnerId(), PartnerPeer::GLOBAL_PARTNER));
			if ($existingPermission)
			{
				throw new KalturaAPIException(KalturaErrors::PERMISSION_ALREADY_EXISTS, $permission->name, $this->getPartnerId());
			}
		}
		
		$dbPermission = $permission->toUpdatableObject($dbPermission);
		try
		{
			$dbPermission->save();
		}
{'fraction_non_alphanumeric': 0.1282392026578073, 'fraction_numerical': 0.0, 'mean_word_length': 5.2026098901098905, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, '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': '805451', 'n_tokens_mistral': 3033, 'n_tokens_neox': 2653, 'n_words': 503}