Example #1
0
 /**
  *  this function return categoryUser if the user has explicit or implicit (by group) required permissions on the category
  *
  * @param int $categoryId
  * @param int $kuserId
  * @param array $requiredPermissions
  * @param bool $supportGroups
  * @param null $con
  * @return categoryKuser|null
  */
 public static function retrievePermittedKuserInCategory($categoryId, $kuserId = null, $requiredPermissions = null, $supportGroups = true, $con = null)
 {
     $category = categoryPeer::retrieveByPK($categoryId);
     if (!$category) {
         return null;
     }
     if ($category->getInheritedParentId()) {
         $categoryId = $category->getInheritedParentId();
     }
     if (is_null($kuserId)) {
         $kuserId = kCurrentContext::getCurrentKsKuserId();
     }
     if (is_null($requiredPermissions)) {
         $requiredPermissions = array(PermissionName::CATEGORY_VIEW);
     }
     $categoryKuser = self::retrieveByCategoryIdAndActiveKuserId($categoryId, $kuserId, $requiredPermissions, $con);
     if (!is_null($categoryKuser)) {
         return $categoryKuser;
     }
     //check if kuserId has permission in category by a junction group
     if ($supportGroups) {
         $kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId($kuserId);
         if (count($kgroupIds) == 0) {
             return null;
         }
         $criteria = new Criteria();
         $criteria->add(categoryKuserPeer::CATEGORY_ID, $categoryId);
         $criteria->add(categoryKuserPeer::KUSER_ID, $kgroupIds, Criteria::IN);
         $criteria->add(categoryKuserPeer::STATUS, CategoryKuserStatus::ACTIVE);
         $categoryKusers = categoryKuserPeer::doSelect($criteria, $con);
         if (!$categoryKusers) {
             return null;
         }
         foreach ($categoryKusers as $categoryKuser) {
             foreach ($requiredPermissions as $requiredPermission) {
                 if ($categoryKuser->hasPermission($requiredPermission)) {
                     return $categoryKuser;
                 }
             }
         }
     }
     return null;
 }
 public function toObject($obj = null, $props_to_skip = array())
 {
     if (!$obj) {
         $obj = new kCategoryKuserAdvancedFilter();
     }
     if (!$this->memberIdEq && !$this->memberIdIn) {
         throw new KalturaAPIException(KalturaErrors::PROPERTY_VALIDATION_CANNOT_BE_NULL, 'memberIdEq,memberIdIn');
     }
     if (!$this->memberPermissionsMatchOr && !$this->memberPermissionsMatchAnd) {
         throw new KalturaAPIException(KalturaErrors::PROPERTY_VALIDATION_CANNOT_BE_NULL, 'memberIdEq,memberIdIn');
     }
     if ($this->memberIdEq) {
         $kuser = kuserPeer::getKuserByPartnerAndUid(kCurrentContext::getCurrentPartnerId(), $this->memberIdEq);
         if (!$kuser) {
             throw new KalturaAPIException(KalturaErrors::USER_NOT_FOUND);
         }
         $kuserIds = array($kuser->getId());
         // retrieve categories that the user is a member by a group.
         $kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId($kuser->getId());
         if (!is_null($kgroupIds) && is_array($kgroupIds)) {
             $kuserIds = array_merge($kgroupIds, $kuserIds);
         }
         $obj->setMemberIdIn($kuserIds);
     }
     if ($this->memberIdIn) {
         $kusers = kuserPeer::getKuserByPartnerAndUids(kCurrentContext::getCurrentPartnerId(), explode(',', $this->memberIdIn));
         $kuserIds = array();
         if (!$kusers || !count($kusers)) {
             throw new KalturaAPIException(KalturaErrors::USER_NOT_FOUND);
         }
         foreach ($kusers as $kuser) {
             $kuserIds[] = $kuser->getId();
         }
         // retrieve categories that the users are members by a group.
         $kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserIds($kuserIds);
         if (!is_null($kgroupIds) && is_array($kgroupIds)) {
             $kuserIds = array_merge($kgroupIds, $kuserIds);
         }
         $obj->setMemberIdIn($kuserIds);
     }
     return parent::toObject($obj, $props_to_skip);
 }
Example #3
0
 /**
  * Return all categories kuser is entitled to view the content.
  * (User may call category->get to view a category - but not to view its content)
  * 
  * @param int $kuserId
  * @param int $limit
  * @return array<category>
  */
 public static function retrieveEntitledAndNonIndexedByKuser($kuserId, $limit)
 {
     $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
     $partner = PartnerPeer::retrieveByPK($partnerId);
     $categoryGroupSize = kConf::get('max_number_of_memebrs_to_be_indexed_on_entry');
     if ($partner && $partner->getCategoryGroupSize()) {
         $categoryGroupSize = $partner->getCategoryGroupSize();
     }
     $c = KalturaCriteria::create(categoryPeer::OM_CLASS);
     $filteredCategoriesIds = entryPeer::getFilterdCategoriesIds();
     if (count($filteredCategoriesIds)) {
         $c->addAnd(categoryPeer::ID, $filteredCategoriesIds, Criteria::IN);
     }
     $membersCountCrit = $c->getNewCriterion(categoryPeer::MEMBERS_COUNT, $categoryGroupSize, Criteria::GREATER_THAN);
     $membersCountCrit->addOr($c->getNewCriterion(categoryPeer::ENTRIES_COUNT, kConf::get('category_entries_count_limit_to_be_indexed'), Criteria::GREATER_THAN));
     $c->addAnd($membersCountCrit);
     $c->setLimit($limit);
     $c->addDescendingOrderByColumn(categoryPeer::UPDATED_AT);
     //all fields needed from default criteria
     //here we cannot use the default criteria, as we need to get all categories user is entitled to view the content.
     //not deleted or purged
     $c->add(self::STATUS, array(CategoryStatus::DELETED, CategoryStatus::PURGED), Criteria::NOT_IN);
     $c->add(self::PARTNER_ID, $partnerId, Criteria::EQUAL);
     //add privacy context
     $privacyContextCrit = $c->getNewCriterion(self::PRIVACY_CONTEXTS, kEntitlementUtils::getKsPrivacyContext(), KalturaCriteria::IN_LIKE);
     $privacyContextCrit->addTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
     $c->addAnd($privacyContextCrit);
     //set privacy by ks and type
     $crit = $c->getNewCriterion(self::PRIVACY, kEntitlementUtils::getPrivacyForKs($partnerId), Criteria::IN);
     $crit->addTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
     //user is entitled to view all cantent that belong to categoires he is a membr of
     $kuser = null;
     $ksString = kCurrentContext::$ks ? kCurrentContext::$ks : '';
     if ($ksString != '') {
         $kuser = kCurrentContext::getCurrentKsKuser();
     }
     if ($kuser) {
         // get the groups that the user belongs to in case she is not associated to the category directly
         $kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId($kuser->getId());
         $kgroupIds[] = $kuser->getId();
         $membersCrit = $c->getNewCriterion(self::MEMBERS, $kgroupIds, KalturaCriteria::IN_LIKE);
         $membersCrit->addTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY);
         $crit->addOr($membersCrit);
     }
     $c->addAnd($crit);
     $c->applyFilters();
     $categoryIds = $c->getFetchedIds();
     return $categoryIds;
 }
 public function getListResponse(KalturaFilterPager $pager, KalturaDetachedResponseProfile $responseProfile = null)
 {
     if ($this->userIdIn) {
         $usersIds = explode(',', $this->userIdIn);
         $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
         $c = new Criteria();
         $c->add(kuserPeer::PARTNER_ID, $partnerId, Criteria::EQUAL);
         $c->add(kuserPeer::PUSER_ID, $usersIds, Criteria::IN);
         $kusers = kuserPeer::doSelect($c);
         $usersIds = array();
         foreach ($kusers as $kuser) {
             /* @var $kuser kuser */
             $usersIds[] = $kuser->getId();
         }
         $this->userIdIn = implode(',', $usersIds);
     }
     if ($this->relatedGroupsByUserId) {
         $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
         $userIds = array();
         $c = new Criteria();
         $c->add(kuserPeer::PARTNER_ID, $partnerId);
         $c->add(kuserPeer::PUSER_ID, $this->relatedGroupsByUserId);
         $c->add(kuserPeer::TYPE, KuserType::USER);
         $kuser = kuserPeer::doSelectOne($c);
         if (!$kuser) {
             $response = new KalturaCategoryUserListResponse();
             $response->objects = new KalturaCategoryUserArray();
             $response->totalCount = 0;
             return $response;
         }
         $kgroupIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId($kuser->getId());
         if (!is_null($kgroupIds) && is_array($kgroupIds)) {
             $userIds = $kgroupIds;
         }
         $userIds[] = $kuser->getId();
         // if userIdIn is also set in the filter need to intersect the two arrays.
         if (isset($this->userIdIn)) {
             $curUserIds = explode(',', $this->userIdIn);
             $userIds = array_intersect($curUserIds, $userIds);
         }
         $this->userIdIn = implode(',', $userIds);
     }
     if ($this->userIdEqual) {
         $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
         $c = new Criteria();
         $c->add(kuserPeer::PARTNER_ID, $partnerId);
         $c->add(kuserPeer::PUSER_ID, $this->userIdEqual);
         if (kCurrentContext::$ks_partner_id == Partner::BATCH_PARTNER_ID) {
             //batch should be able to get categoryUser of deleted users.
             kuserPeer::setUseCriteriaFilter(false);
         }
         // in case of more than one deleted kusers - get the last one
         $c->addDescendingOrderByColumn(kuserPeer::UPDATED_AT);
         $kuser = kuserPeer::doSelectOne($c);
         kuserPeer::setUseCriteriaFilter(true);
         if (!$kuser) {
             KalturaLog::debug('User not found');
             $response = new KalturaCategoryUserListResponse();
             $response->objects = new KalturaCategoryUserArray();
             $response->totalCount = 0;
             return $response;
         }
         $this->userIdEqual = $kuser->getId();
     }
     $categories = array();
     if ($this->categoryIdEqual) {
         $categories[] = categoryPeer::retrieveByPK($this->categoryIdEqual);
     } elseif ($this->categoryIdIn) {
         $categories = categoryPeer::retrieveByPKs(explode(',', $this->categoryIdIn));
     }
     $categoriesInheritanceRoot = array();
     foreach ($categories as $category) {
         /* @var $category category */
         if (is_null($category)) {
             continue;
         }
         if ($category->getInheritanceType() == InheritanceType::INHERIT) {
             if ($this->categoryDirectMembers && kCurrentContext::$master_partner_id == Partner::BATCH_PARTNER_ID) {
                 $categoriesInheritanceRoot[$category->getId()] = $category->getId();
             } else {
                 //if category inheris members - change filter to -> inherited from parent id = category->getIheritedParent
                 $categoriesInheritanceRoot[$category->getInheritedParentId()] = $category->getInheritedParentId();
             }
         } else {
             $categoriesInheritanceRoot[$category->getId()] = $category->getId();
         }
     }
     $this->categoryDirectMembers = null;
     $this->categoryIdEqual = null;
     $this->categoryIdIn = implode(',', $categoriesInheritanceRoot);
     //if filter had categories that doesn't exists or not entitled - should return 0 objects.
     if (count($categories) && !count($categoriesInheritanceRoot)) {
         $response = new KalturaCategoryUserListResponse();
         $response->totalCount = 0;
         return $response;
     }
     $categoryKuserFilter = $this->toObject();
     $c = KalturaCriteria::create(categoryKuserPeer::OM_CLASS);
     $categoryKuserFilter->attachToCriteria($c);
     $pager->attachToCriteria($c);
     $c->applyFilters();
     $list = categoryKuserPeer::doSelect($c);
     $totalCount = $c->getRecordsCount();
     $newList = KalturaCategoryUserArray::fromDbArray($list, $responseProfile);
     $response = new KalturaCategoryUserListResponse();
     $response->objects = $newList;
     $response->totalCount = $totalCount;
     return $response;
 }
Example #5
0
 public static function getEntitledKuserByPrivacyContext()
 {
     $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id;
     $privacyContextSearch = array();
     $ks = ks::fromSecureString(kCurrentContext::$ks);
     $ksPrivacyContexts = null;
     if ($ks) {
         $ksPrivacyContexts = $ks->getPrivacyContext();
     }
     if (is_null($ksPrivacyContexts) || $ksPrivacyContexts == '') {
         $ksPrivacyContexts = self::DEFAULT_CONTEXT . $partnerId;
     }
     $ksPrivacyContexts = explode(',', $ksPrivacyContexts);
     $privacyContexts = $ksPrivacyContexts;
     $privacyContexts[] = self::ENTRY_PRIVACY_CONTEXT;
     // get the groups that the user belongs to in case she is not associated to the category directly
     $kuserIds = KuserKgroupPeer::retrieveKgroupIdsByKuserId(kCurrentContext::getCurrentKsKuserId());
     $kuserIds[] = kCurrentContext::getCurrentKsKuserId();
     foreach ($privacyContexts as $privacyContext) {
         foreach ($kuserIds as $kuserId) {
             $privacyContextSearch[] = $privacyContext . '_' . $kuserId;
         }
     }
     return $privacyContextSearch;
 }