/** * Index CategoryUser by userid and category id * * @action index * @param string $userId * @param int $categoryId * @param bool $shouldUpdate * @throws KalturaErrors::INVALID_CATEGORY_USER_ID * @return int */ public function indexAction($userId, $categoryId, $shouldUpdate = true) { if (kEntitlementUtils::getEntitlementEnforcement()) { throw new KalturaAPIException(KalturaErrors::CANNOT_INDEX_OBJECT_WHEN_ENTITLEMENT_IS_ENABLE); } $partnerId = kCurrentContext::$partner_id ? kCurrentContext::$partner_id : kCurrentContext::$ks_partner_id; $kuser = kuserPeer::getActiveKuserByPartnerAndUid($partnerId, $userId); if (!$kuser) { throw new KalturaAPIException(KalturaErrors::INVALID_USER_ID); } $dbCategoryKuser = categoryKuserPeer::retrievePermittedKuserInCategory($categoryId, $kuser->getId(), null, false); if (!$dbCategoryKuser) { throw new KalturaAPIException(KalturaErrors::INVALID_CATEGORY_USER_ID); } if (!$shouldUpdate) { $dbCategoryKuser->setUpdatedAt(time()); $dbCategoryKuser->save(); return $dbCategoryKuser->getId(); } $dbCategoryKuser->reSetCategoryFullIds(); $dbCategoryKuser->reSetScreenName(); $dbCategoryKuser->save(); return $dbCategoryKuser->getId(); }
public function getListResponse(KalturaFilterPager $pager, KalturaDetachedResponseProfile $responseProfile = null) { if ($this->entryIdEqual == null && $this->categoryIdIn == null && $this->categoryIdEqual == null && (kEntitlementUtils::getEntitlementEnforcement() || !kCurrentContext::$is_admin_session)) { throw new KalturaAPIException(KalturaErrors::MUST_FILTER_ON_ENTRY_OR_CATEGORY); } if (kEntitlementUtils::getEntitlementEnforcement()) { //validate entitl for entry if ($this->entryIdEqual != null) { $entry = entryPeer::retrieveByPK($this->entryIdEqual); if (!$entry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $this->entryIdEqual); } } //validate entitl for entryIn if ($this->entryIdIn != null) { $entry = entryPeer::retrieveByPKs($this->entryIdIn); if (!$entry) { throw new KalturaAPIException(KalturaErrors::ENTRY_ID_NOT_FOUND, $this->entryIdIn); } } //validate entitl categories if ($this->categoryIdIn != null) { $categoryIdInArr = explode(',', $this->categoryIdIn); if (!categoryKuserPeer::areCategoriesAllowed($categoryIdInArr)) { $categoryIdInArr = array_unique($categoryIdInArr); } $entitledCategories = categoryPeer::retrieveByPKs($categoryIdInArr); if (!count($entitledCategories) || count($entitledCategories) != count($categoryIdInArr)) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $this->categoryIdIn); } $categoriesIdsUnlisted = array(); foreach ($entitledCategories as $category) { if ($category->getDisplayInSearch() == DisplayInSearchType::CATEGORY_MEMBERS_ONLY) { $categoriesIdsUnlisted[] = $category->getId(); } } if (count($categoriesIdsUnlisted)) { if (!categoryKuserPeer::areCategoriesAllowed($categoriesIdsUnlisted)) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $this->categoryIdIn); } } } //validate entitl category if ($this->categoryIdEqual != null) { $category = categoryPeer::retrieveByPK($this->categoryIdEqual); if (!$category && kCurrentContext::$master_partner_id != Partner::BATCH_PARTNER_ID) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $this->categoryIdEqual); } if ($category->getDisplayInSearch() == DisplayInSearchType::CATEGORY_MEMBERS_ONLY && !categoryKuserPeer::retrievePermittedKuserInCategory($category->getId(), kCurrentContext::getCurrentKsKuserId())) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $this->categoryIdEqual); } } } $categoryEntryFilter = $this->toObject(); $c = KalturaCriteria::create(categoryEntryPeer::OM_CLASS); $categoryEntryFilter->attachToCriteria($c); if (!kEntitlementUtils::getEntitlementEnforcement() || $this->entryIdEqual == null) { $pager->attachToCriteria($c); } $dbCategoriesEntry = categoryEntryPeer::doSelect($c); if (kEntitlementUtils::getEntitlementEnforcement() && count($dbCategoriesEntry) && $this->entryIdEqual != null) { //remove unlisted categories: display in search is set to members only $categoriesIds = array(); foreach ($dbCategoriesEntry as $dbCategoryEntry) { $categoriesIds[] = $dbCategoryEntry->getCategoryId(); } $c = KalturaCriteria::create(categoryPeer::OM_CLASS); $c->add(categoryPeer::ID, $categoriesIds, Criteria::IN); $pager->attachToCriteria($c); $c->applyFilters(); $categoryIds = $c->getFetchedIds(); foreach ($dbCategoriesEntry as $key => $dbCategoryEntry) { if (!in_array($dbCategoryEntry->getCategoryId(), $categoryIds)) { KalturaLog::info('Category [' . print_r($dbCategoryEntry->getCategoryId(), true) . '] is not listed to user'); unset($dbCategoriesEntry[$key]); } } $totalCount = $c->getRecordsCount(); } else { $resultCount = count($dbCategoriesEntry); if ($resultCount && $resultCount < $pager->pageSize) { $totalCount = ($pager->pageIndex - 1) * $pager->pageSize + $resultCount; } else { KalturaFilterPager::detachFromCriteria($c); $totalCount = categoryEntryPeer::doCount($c); } } $categoryEntrylist = KalturaCategoryEntryArray::fromDbArray($dbCategoriesEntry, $responseProfile); $response = new KalturaCategoryEntryListResponse(); $response->objects = $categoryEntrylist; $response->totalCount = $totalCount; // no pager since category entry is limited to ENTRY::MAX_CATEGORIES_PER_ENTRY return $response; }
/** * activate CategoryEntry when it is pending moderation * * @action reject * @param string $entryId * @param int $categoryId * @throws KalturaErrors::INVALID_ENTRY_ID * @throws KalturaErrors::CATEGORY_NOT_FOUND * @throws KalturaErrors::ENTRY_IS_NOT_ASSIGNED_TO_CATEGORY * @throws KalturaErrors::CANNOT_ACTIVATE_CATEGORY_ENTRY */ function rejectAction($entryId, $categoryId) { $entry = entryPeer::retrieveByPK($entryId); if (!$entry) { throw new KalturaAPIException(KalturaErrors::INVALID_ENTRY_ID, $entryId); } $category = categoryPeer::retrieveByPK($categoryId); if (!$category) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $categoryId); } $dbCategoryEntry = categoryEntryPeer::retrieveByCategoryIdAndEntryId($categoryId, $entryId); if (!$dbCategoryEntry) { throw new KalturaAPIException(KalturaErrors::ENTRY_IS_NOT_ASSIGNED_TO_CATEGORY); } //validate user is entiteld to reject entry from category if (kEntitlementUtils::getEntitlementEnforcement()) { $categoryKuser = categoryKuserPeer::retrievePermittedKuserInCategory($categoryId, kCurrentContext::getCurrentKsKuserId()); if (!$categoryKuser || $categoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MANAGER && $categoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MODERATOR) { throw new KalturaAPIException(KalturaErrors::CANNOT_REJECT_CATEGORY_ENTRY); } } if ($dbCategoryEntry->getStatus() != CategoryEntryStatus::PENDING) { throw new KalturaAPIException(KalturaErrors::CANNOT_REJECT_CATEGORY_ENTRY_SINCE_IT_IS_NOT_PENDING); } $dbCategoryEntry->setStatus(CategoryEntryStatus::REJECTED); $dbCategoryEntry->save(); }
public static function syncEntriesCategories(entry $entry, $isCategoriesModified) { self::$skipEntrySave = true; if ($entry->getNewCategories() != null && $entry->getNewCategories() !== "") { $newCats = explode(entry::ENTRY_CATEGORY_SEPARATOR, $entry->getNewCategories()); } else { $newCats = array(); } if (!$isCategoriesModified) { if ($entry->getNewCategoriesIds() != null && $entry->getNewCategoriesIds() !== "") { $newCatsIds = explode(entry::ENTRY_CATEGORY_SEPARATOR, $entry->getNewCategoriesIds()); } else { $newCatsIds = array(); } KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); $dbCategories = categoryPeer::retrieveByPKs($newCatsIds); KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); foreach ($dbCategories as $dbCategory) { //skip categoy with privacy contexts. if ($dbCategory->getPrivacyContexts() != null && $dbCategory->getPrivacyContexts() != '') { continue; } $newCats[] = $dbCategory->getFullName(); } } $newCats = array_unique($newCats); $allIds = array(); $allCats = array(); $addedCats = array(); $removedCats = array(); $remainingCats = array(); $oldCats = array(); $oldCatsIds = array(); $dbOldCategoriesEntry = categoryEntryPeer::selectByEntryId($entry->getId()); foreach ($dbOldCategoriesEntry as $dbOldCategoryEntry) { $oldCatsIds[] = $dbOldCategoryEntry->getCategoryId(); } $oldCategoris = categoryPeer::retrieveByPKsNoFilter($oldCatsIds); foreach ($oldCategoris as $category) { if ($category->getPrivacyContexts() != '' && $category->getPrivacyContexts() != null) { continue; } $oldCats[] = $category->getFullName(); } foreach ($oldCats as $cat) { if (array_search($cat, $newCats) === false) { $removedCats[] = $cat; } } foreach ($newCats as $cat) { if (array_search($cat, $oldCats) === false) { $addedCats[] = $cat; } else { $remainingCats[] = $cat; } } foreach ($remainingCats as $cat) { KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); $category = categoryPeer::getByFullNameExactMatch($cat); KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); if ($category) { if ($category->getPrivacyContext() == '' || $category->getPrivacyContext() == null) { $allCats[] = $category->getFullName(); $allIds[] = $category->getId(); } } } foreach ($addedCats as $cat) { $category = categoryPeer::getByFullNameExactMatch($cat); if (!$category) { KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); $unentitedCategory = categoryPeer::getByFullNameExactMatch($cat); KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); if (!$unentitedCategory) { $category = category::createByPartnerAndFullName($entry->getPartnerId(), $cat); //it is possible to add on an entry a few new categories on the same new parent - //and we need to sync sphinx once we add so the category will not be duplicated kEventsManager::flushEvents(); } } else { $categoryKuser = categoryKuserPeer::retrievePermittedKuserInCategory($category->getId(), kCurrentContext::getCurrentKsKuserId()); if (kEntitlementUtils::getEntitlementEnforcement() && $category->getContributionPolicy() != ContributionPolicyType::ALL && (!$categoryKuser || $categoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::MEMBER)) { //user is not entitled to add entry to this category $category = null; } } if (!$category) { continue; } //when use caetgoryEntry->add categoryEntry object was alreay created - and no need to create it. //when using baseEntry->categories = 'my category' will need to add the new category. $categoryEntry = categoryEntryPeer::retrieveByCategoryIdAndEntryId($category->getId(), $entry->getId()); if (!$categoryEntry) { $categoryEntry = new categoryEntry(); $categoryEntry->setEntryId($entry->getId()); $categoryEntry->setCategoryId($category->getId()); $categoryEntry->setPartnerId($entry->getPartnerId()); $categoryEntry->setStatus(CategoryEntryStatus::ACTIVE); $categoryEntry->save(); } if ($category->getPrivacyContext() == '' || $category->getPrivacyContext() == null) { // only categories with no context should be set on entry->categories and entry->categoriesIds $allCats[] = $category->getFullName(); $allIds[] = $category->getId(); } $alreadyAddedCatIds[] = $category->getId(); $alreadyAddedCatIds = array_merge($alreadyAddedCatIds, $category->getAllParentsIds()); } foreach ($removedCats as $cat) { $category = categoryPeer::getByFullNameExactMatch($cat); if ($category) { $categoryEntryToDelete = categoryEntryPeer::retrieveByCategoryIdAndEntryId($category->getId(), $entry->getId()); if ($categoryEntryToDelete) { $categoryKuser = categoryKuserPeer::retrievePermittedKuserInCategory($categoryEntryToDelete->getCategoryId(), kCurrentContext::getCurrentKsKuserId()); if ($category->getPrivacyContexts() && (!$categoryKuser || $categoryKuser->getPermissionLevel() == CategoryKuserPermissionLevel::MEMBER)) { //not entiteld to delete - should be set back on the entry. $allCats[] = $category->getFullName(); $allIds[] = $category->getId(); } else { $categoryEntryToDelete->setStatus(CategoryEntryStatus::DELETED); $categoryEntryToDelete->save(); } } } else { //category was not found - it could be that user is not entitled to remove it KalturaCriterion::disableTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); $category = categoryPeer::getByFullNameExactMatch($cat); KalturaCriterion::restoreTag(KalturaCriterion::TAG_ENTITLEMENT_CATEGORY); if ($category) { $allCats[] = $category->getFullName(); $allIds[] = $category->getId(); } } } self::$skipEntrySave = false; $entry->parentSetCategories(implode(",", $allCats)); $entry->parentSetCategoriesIds(implode(',', $allIds)); }
public function validateForUpdate($sourceObject, $propertiesToSkip = null) { /* @var $sourceObject categoryKuser */ $category = categoryPeer::retrieveByPK($sourceObject->getCategoryId()); if (!$category) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $sourceObject->getCategoryId()); } if ($this->permissionNames && $this->permissionNames != $sourceObject->getPermissionNames()) { if ($sourceObject->getKuserId() == $category->getKuserId()) { if (strpos($this->permissionNames, PermissionName::CATEGORY_EDIT) === false) { throw new KalturaAPIException(KalturaErrors::CANNOT_UPDATE_CATEGORY_USER_OWNER); } } } $currentKuserCategoryKuser = categoryKuserPeer::retrievePermittedKuserInCategory($sourceObject->getCategoryId(), kCurrentContext::getCurrentKsKuserId()); if (kEntitlementUtils::getEntitlementEnforcement() && (!$currentKuserCategoryKuser || !$currentKuserCategoryKuser->hasPermission(PermissionName::CATEGORY_EDIT))) { throw new KalturaAPIException(KalturaErrors::CANNOT_UPDATE_CATEGORY_USER, $sourceObject->getCategoryId()); } return parent::validateForUpdate($sourceObject, $propertiesToSkip); }
/** * Delete a Category * * @action delete * @param int $id * @param KalturaNullableBoolean $moveEntriesToParentCategory */ function deleteAction($id, $moveEntriesToParentCategory = KalturaNullableBoolean::TRUE_VALUE) { if ($this->getPartner()->getFeaturesStatusByType(IndexObjectType::LOCK_CATEGORY)) { throw new KalturaAPIException(KalturaErrors::CATEGORIES_LOCKED); } $categoryDb = categoryPeer::retrieveByPK($id); if (!$categoryDb) { throw new KalturaAPIException(KalturaErrors::CATEGORY_NOT_FOUND, $id); } if (kEntitlementUtils::getEntitlementEnforcement()) { $currentKuserCategoryKuser = categoryKuserPeer::retrievePermittedKuserInCategory($categoryDb->getId(), kCurrentContext::getCurrentKsKuserId()); if (!$currentKuserCategoryKuser || $currentKuserCategoryKuser->getPermissionLevel() != CategoryKuserPermissionLevel::MANAGER) { throw new KalturaAPIException(KalturaErrors::NOT_ENTITLED_TO_UPDATE_CATEGORY); } } $this->getPartner()->addFeaturesStatus(IndexObjectType::LOCK_CATEGORY); try { if ($moveEntriesToParentCategory) { $categoryDb->setDeletedAt(time()); } else { $categoryDb->setDeletedAt(time(), 0); } $categoryDb->save(); $this->getPartner()->removeFeaturesStatus(IndexObjectType::LOCK_CATEGORY); } catch (Exception $ex) { $this->getPartner()->removeFeaturesStatus(IndexObjectType::LOCK_CATEGORY); throw $ex; } }