<commit_before><?php


/**
 * Skeleton subclass for performing query and update operations on the 'permission' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package Core
 * @subpackage model
 */
class PermissionPeer extends BasePermissionPeer
{
	protected static $allowedPermissions = array();
	
	public static function checkValidPermissionsForRole($permissionsStr, $partnerId)
	{
		if ($permissionsStr == UserRole::ALL_PARTNER_PERMISSIONS_WILDCARD) {
			return true;
		}
		
		$permissions = array_map('trim', explode(',', $permissionsStr));
		
		foreach ($permissions as $permission)
		{
			if (!$permission)
				continue;
			
			$c = new Criteria();
			$c->addAnd(PermissionPeer::NAME, $permission, Criteria::EQUAL);
			$c->addAnd(PermissionPeer::PARTNER_ID, array(strval($partnerId), strval(PartnerPeer::GLOBAL_PARTNER)), Criteria::IN);
			// a user role can only contain NORMAL permission types - TODO should be changed to perPartner / perUser permissions
			$c->addAnd(PermissionPeer::TYPE, PermissionType::NORMAL, Criteria::EQUAL);
			$c->addAnd(PermissionPeer::STATUS, PermissionStatus::ACTIVE, Criteria::EQUAL);
			
			PermissionPeer::setUseCriteriaFilter(false);
			$hasPermission = PermissionPeer::doSelectOne($c);
			PermissionPeer::setUseCriteriaFilter(true);
			
			if (!$hasPermission || $hasPermission->getStatus() == PermissionStatus::DELETED) {
				throw new kPermissionException('Permission ['.$permission.'] was not found for partner ['.$partnerId.']', kPermissionException::PERMISSION_NOT_FOUND);
			}
		}
	}
			
	public static function addToPartner($permission, $partnerId, $checkGlobalPartner = true)
	{
		$permission->setPartnerId($partnerId);
		$c = new Criteria();
		if ($checkGlobalPartner) {
			$c->addAnd(PermissionPeer::PARTNER_ID, array(strval($partnerId), strval(PartnerPeer::GLOBAL_PARTNER)), Criteria::IN);
			$c->addDescendingOrderByColumn(PermissionPeer::PARTNER_ID);
		}
		else {
			$c->addAnd(PermissionPeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
		}
		$c->addAnd(PermissionPeer::NAME, $permission->getName(), Criteria::EQUAL);
		$existingPermission = PermissionPeer::doSelectOne($c);
		if (!$existingPermission) {
			$permission->save();
			KalturaLog::log('Adding permission ['.$permission->getName().'] to partner ['.$partnerId.'].');
			return $permission;
		}
		else {
			throw new kPermissionException('Permission ['.$permission->getName().'] already exists for partner ['.$partnerId.']', kPermissionException::PERMISSION_ALREADY_EXISTS);
		}
	}
	
	public static function setDefaultCriteriaFilter ()
	{
		if(is_null(self::$s_criteria_filter))
			self::$s_criteria_filter = new criteriaFilter();
		
		$c = new Criteria(); 
		$c->add(self::STATUS, PermissionStatus::DELETED,criteria::NOT_EQUAL);
		self::$s_criteria_filter->setFilter($c);
	}
	
	
	public static function removePermissionFromPartner($permissionName, $partnerId)
	{
		$c = new Criteria();
		$c->addAnd(PermissionPeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
		$c->addAnd(PermissionPeer::NAME, $permissionName, Criteria::EQUAL);
		$existingPermission = PermissionPeer::doSelectOne($c);
		if (!$existingPermission) {
			throw new kPermissionException('Permission ['.$permissionName.'] does not exist for partner ['.$partnerId.']', kPermissionException::PERMISSION_NOT_FOUND);
		}
		KalturaLog::log('Removing permission ['.$permissionName.'] from partner ['.$partnerId.'].');
		$existingPermission->setStatus(PermissionStatus::DELETED);
	}
	
	
	public static function enableForPartner($permissionName, $permissionType, $partnerId = null, $friendlyName = null, $description = null)
	{
		if(is_null($partnerId))
			$partnerId = kCurrentContext::$ks_partner_id;
			
		$permission = new Permission();
		$permission->setName($permissionName);
		$permission->setFriendlyName($friendlyName ? $friendlyName : $permissionName);
		$permission->setDescription($description);<commit_msg>qnd: use impersonated partner if there is any<commit_after><?php


/**
 * Skeleton subclass for performing query and update operations on the 'permission' table.
 *
 * 
 *
 * You should add additional methods to this class to meet the
 * application requirements.  This class will only be generated as
 * long as it does not already exist in the output directory.
 *
 * @package Core
 * @subpackage model
 */
class PermissionPeer extends BasePermissionPeer
{
	protected static $allowedPermissions = array();
	
	public static function checkValidPermissionsForRole($permissionsStr, $partnerId)
	{
		if ($permissionsStr == UserRole::ALL_PARTNER_PERMISSIONS_WILDCARD) {
			return true;
		}
		
		$permissions = array_map('trim', explode(',', $permissionsStr));
		
		foreach ($permissions as $permission)
		{
			if (!$permission)
				continue;
			
			$c = new Criteria();
			$c->addAnd(PermissionPeer::NAME, $permission, Criteria::EQUAL);
			$c->addAnd(PermissionPeer::PARTNER_ID, array(strval($partnerId), strval(PartnerPeer::GLOBAL_PARTNER)), Criteria::IN);
			// a user role can only contain NORMAL permission types - TODO should be changed to perPartner / perUser permissions
			$c->addAnd(PermissionPeer::TYPE, PermissionType::NORMAL, Criteria::EQUAL);
			$c->addAnd(PermissionPeer::STATUS, PermissionStatus::ACTIVE, Criteria::EQUAL);
			
			PermissionPeer::setUseCriteriaFilter(false);
			$hasPermission = PermissionPeer::doSelectOne($c);
			PermissionPeer::setUseCriteriaFilter(true);
			
			if (!$hasPermission || $hasPermission->getStatus() == PermissionStatus::DELETED) {
				throw new kPermissionException('Permission ['.$permission.'] was not found for partner ['.$partnerId.']', kPermissionException::PERMISSION_NOT_FOUND);
			}
		}
	}
			
	public static function addToPartner($permission, $partnerId, $checkGlobalPartner = true)
	{
		$permission->setPartnerId($partnerId);
		$c = new Criteria();
		if ($checkGlobalPartner) {
			$c->addAnd(PermissionPeer::PARTNER_ID, array(strval($partnerId), strval(PartnerPeer::GLOBAL_PARTNER)), Criteria::IN);
			$c->addDescendingOrderByColumn(PermissionPeer::PARTNER_ID);
		}
		else {
			$c->addAnd(PermissionPeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
		}
		$c->addAnd(PermissionPeer::NAME, $permission->getName(), Criteria::EQUAL);
		$existingPermission = PermissionPeer::doSelectOne($c);
		if (!$existingPermission) {
			$permission->save();
			KalturaLog::log('Adding permission ['.$permission->getName().'] to partner ['.$partnerId.'].');
			return $permission;
		}
		else {
			throw new kPermissionException('Permission ['.$permission->getName().'] already exists for partner ['.$partnerId.']', kPermissionException::PERMISSION_ALREADY_EXISTS);
		}
	}
	
	public static function setDefaultCriteriaFilter ()
	{
		if(is_null(self::$s_criteria_filter))
			self::$s_criteria_filter = new criteriaFilter();
		
		$c = new Criteria(); 
		$c->add(self::STATUS, PermissionStatus::DELETED,criteria::NOT_EQUAL);
		self::$s_criteria_filter->setFilter($c);
	}
	
	
	public static function removePermissionFromPartner($permissionName, $partnerId)
	{
		$c = new Criteria();
		$c->addAnd(PermissionPeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
		$c->addAnd(PermissionPeer::NAME, $permissionName, Criteria::EQUAL);
		$existingPermission = PermissionPeer::doSelectOne($c);
		if (!$existingPermission) {
			throw new kPermissionException('Permission ['.$permissionName.'] does not exist for partner ['.$partnerId.']', kPermissionException::PERMISSION_NOT_FOUND);
		}
		KalturaLog::log('Removing permission ['.$permissionName.'] from partner ['.$partnerId.'].');
		$existingPermission->setStatus(PermissionStatus::DELETED);
	}
	
	
	public static function enableForPartner($permissionName, $permissionType, $partnerId = null, $friendlyName = null, $description = null)
	{
		if(is_null($partnerId))
			$partnerId = (kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id);
			
		$permission = new Permission();
		$permission->setName($permissionName);
		$permission->setFriendlyName($friendlyName ? $friendlyName : $permissionName);
		$permission->setDescription($description);
{'fraction_non_alphanumeric': 0.14179468422336017, 'fraction_numerical': 0.0, 'mean_word_length': 5.9516949152542376, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '2471444', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2428, 'n_words': 508}