/** * Check whether the given policy is valid for the given community and user. * * @param CommunityDao $communityDao community DAO * @param null|UserDao $userDao user DAO * @param int $policy policy * @return bool true if the given policy is valid for the given community and user * @throws Zend_Exception */ public function policyCheck($communityDao, $userDao = null, $policy = MIDAS_POLICY_READ) { if (!$communityDao instanceof CommunityDao || !is_numeric($policy)) { throw new Zend_Exception('Error in param: communityDao should be a CommunityDao and policy should be numeric.'); } if ($userDao == null) { $userId = -1; } elseif (!$userDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } else { $userId = $userDao->getUserId(); if ($userDao->isAdmin()) { return true; } } $privacy = $communityDao->getPrivacy(); switch ($policy) { case MIDAS_POLICY_READ: if ($privacy != MIDAS_COMMUNITY_PRIVATE) { return true; } elseif ($userId == -1) { return false; } else { $user_groups = $userDao->getGroups(); $member_group = $communityDao->getMemberGroup(); foreach ($user_groups as $group) { if ($group->getKey() == $member_group->getKey()) { return true; } } $invitations = $userDao->getInvitations(); foreach ($invitations as $invitation) { if ($invitation->getCommunityId() == $communityDao->getKey()) { return true; } } return false; } break; case MIDAS_POLICY_WRITE: if ($userId == -1) { return false; } else { $user_groups = $userDao->getGroups(); $moderator_group = $communityDao->getModeratorGroup(); $admin_group = $communityDao->getAdminGroup(); foreach ($user_groups as $group) { if ($group->getKey() == $moderator_group->getKey() || $group->getKey() == $admin_group->getKey()) { return true; } } return false; } break; case MIDAS_POLICY_ADMIN: if ($userId == -1) { return false; } else { $user_groups = $userDao->getGroups(); $admin_group = $communityDao->getAdminGroup(); foreach ($user_groups as $group) { if ($group->getKey() == $admin_group->getKey()) { return true; } } return false; } break; default: return false; } }
/** * Get feeds. * * @param UserDao $loggedUserDao * @param null|UserDao $userDao * @param null|CommunityDao $communityDao * @param int $policy * @param int $limit * @return array * @throws Zend_Exception */ protected function getFeeds($loggedUserDao, $userDao = null, $communityDao = null, $policy = 0, $limit = 20) { $isAdmin = false; if ($loggedUserDao == null) { $userId = -1; } elseif (!$loggedUserDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } else { $userId = $loggedUserDao->getUserId(); if ($loggedUserDao->isAdmin()) { $isAdmin = true; } } if ($userDao != null && !$userDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } if ($communityDao != null && !$communityDao instanceof CommunityDao) { throw new Zend_Exception('Should be a community.'); } $sql = $this->database->select()->setIntegrityCheck(false)->from(array('f' => 'feed'))->limit($limit); if (!$isAdmin) { $sql->joinLeft(array('fpu' => 'feedpolicyuser'), ' f.feed_id = fpu.feed_id AND ' . $this->database->getDB()->quoteInto('fpu.policy >= ?', $policy) . ' AND ' . $this->database->getDB()->quoteInto('fpu.user_id = ? ', $userId) . ' ', array('userpolicy' => 'fpu.policy'))->joinLeft(array('fpg' => 'feedpolicygroup'), ' f.feed_id = fpg.feed_id AND ' . $this->database->getDB()->quoteInto('fpg.policy >= ?', $policy) . ' AND ( ' . $this->database->getDB()->quoteInto('fpg.group_id = ? ', MIDAS_GROUP_ANONYMOUS_KEY) . ' OR fpg.group_id IN (' . new Zend_Db_Expr($this->database->select()->setIntegrityCheck(false)->from(array('u2g' => 'user2group'), array('group_id'))->where('u2g.user_id = ?', $userId)) . '))', array('grouppolicy' => 'fpg.policy'))->where('( fpu.feed_id is not null or fpg.feed_id is not null)'); } if ($userDao != null) { $sql->where('f.user_id = ? ', $userDao->getKey()); } if ($communityDao != null) { $sql->join(array('f2c' => 'feed2community'), $this->database->getDB()->quoteInto('f2c.community_id = ? ', $communityDao->getKey()) . ' AND f.feed_id = f2c.feed_id', array()); } $sql->order(array('f.date DESC')); $rowset = $this->database->fetchAll($sql); $rowsetAnalysed = array(); foreach ($rowset as $row) { if (isset($row['userpolicy']) && $row['userpolicy'] == null) { $row['userpolicy'] = 0; } if (isset($row['grouppolicy']) && $row['grouppolicy'] == null) { $row['grouppolicy'] = 0; } if (!isset($rowsetAnalysed[$row['feed_id']]) || $rowsetAnalysed[$row['feed_id']]->policy < $row['userpolicy'] && $rowsetAnalysed[$row['feed_id']]->policy < $row['grouppolicy']) { $tmpDao = $this->initDao('Feed', $row); if (isset($row['userpolicy']) && isset($row['grouppolicy']) && $row['userpolicy'] >= $row['grouppolicy']) { $tmpDao->policy = $row['userpolicy']; } elseif ($isAdmin) { $tmpDao->policy = MIDAS_POLICY_ADMIN; } else { $tmpDao->policy = $row['grouppolicy']; } $rowsetAnalysed[$row['feed_id']] = $tmpDao; unset($tmpDao); } } $this->Component->Sortdao->field = 'date'; $this->Component->Sortdao->order = 'asc'; usort($rowsetAnalysed, array($this->Component->Sortdao, 'sortByDate')); return $rowsetAnalysed; }
/** * Return a list of communities corresponding to the search. * * @param string $search * @param UserDao $userDao * @param int $limit * @param bool $group * @param string $order * @return array * @throws Zend_Exception */ public function getCommunitiesFromSearch($search, $userDao, $limit = 14, $group = true, $order = 'view') { if (Zend_Registry::get('configDatabase')->database->adapter == 'PDO_PGSQL') { $group = false; // PostgreSQL does not like the SQL request with group by } $communities = array(); if ($userDao == null) { $userId = -1; } elseif (!$userDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } else { $userId = $userDao->getUserId(); $userGroups = $userDao->getGroups(); foreach ($userGroups as $userGroup) { $communities[] = $userGroup->getCommunityId(); } } $sql = $this->database->select(); if ($group) { $sql->from(array('c' => 'community'), array('community_id', 'name', 'count(*)')); } else { $sql->from(array('c' => 'community')); } if ($userId != -1 && $userDao->isAdmin()) { $sql->where('c.name LIKE ?', '%' . $search . '%'); } elseif (!empty($communities)) { $sql->where('c.name LIKE ?', '%' . $search . '%'); $sql->where('(c.privacy < ' . MIDAS_COMMUNITY_PRIVATE . ' OR ' . $this->database->getDB()->quoteInto('c.community_id IN (?)', $communities) . ')'); } else { $sql->where('c.name LIKE ?', '%' . $search . '%'); $sql->where('(c.privacy < ' . MIDAS_COMMUNITY_PRIVATE . ')'); } $sql->limit($limit); if ($group) { $sql->group('c.name'); } switch ($order) { case 'name': $sql->order(array('c.name ASC')); break; case 'date': $sql->order(array('c.creation ASC')); break; case 'view': default: $sql->order(array('c.view DESC')); break; } $rowset = $this->database->fetchAll($sql); $return = array(); foreach ($rowset as $row) { $tmpDao = $this->initDao('Community', $row); if (isset($row['count(*)'])) { $tmpDao->count = $row['count(*)']; } $return[] = $tmpDao; unset($tmpDao); } return $return; }
/** * for use in array_map in groupGet. * * @param UserDao $user * @return mixed */ public function getIdFromUser($user) { return $user->getUserId(); }
/** * Duplicate an item in destination folder/community. * * Create a new item (same as old one) in destination folder/community. The new item * have the same metadata and revisions with the old one, but its owner is set as the * input userDao parameter (who run this operation) and access policy is based on * the input folderDao parameter (destination folder) * * @param ItemDao $itemDao the item to be duplicated * @param UserDao $userDao the user who run this operation * @param FolderDao $folderDao destination folder * @return ItemDao * @throws Zend_Exception on invalid input parameters (itemDao, userDao and folderDao) */ public function duplicateItem($itemDao, $userDao, $folderDao) { if (!$itemDao instanceof ItemDao || !$folderDao instanceof FolderDao) { throw new Zend_Exception('Error in ItemDao or FolderDao when duplicating item'); } if (!$userDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } /** @var BitstreamModel $BitstreamModel */ $BitstreamModel = MidasLoader::loadModel('Bitstream'); $name = $itemDao->getName(); $description = $itemDao->getDescription(); $newItem = $this->createItem($name, $description, $folderDao); $newItem->setType($itemDao->getType()); $newItem->setSizebytes($itemDao->getSizebytes()); $newItem->setDateCreation(date('Y-m-d H:i:s')); $newItem->setDateUpdate(date('Y-m-d H:i:s')); $thumbnailId = $itemDao->getThumbnailId(); if ($thumbnailId !== null) { $oldThumb = $BitstreamModel->load($thumbnailId); $newThumb = new BitstreamDao(); $newThumb->setItemrevisionId(-1); $newThumb->setName($oldThumb->getName()); $newThumb->setMimetype($oldThumb->getMimetype()); $newThumb->setSizebytes($oldThumb->getSizebytes()); $newThumb->setChecksum($oldThumb->getChecksum()); $newThumb->setPath($oldThumb->getPath()); $newThumb->setAssetstoreId($oldThumb->getAssetstoreId()); $newThumb->setDate($oldThumb->getDate()); $BitstreamModel->save($newThumb); $newItem->setThumbnailId($newThumb->getKey()); } /** @var ItemRevisionModel $ItemRevisionModel */ $ItemRevisionModel = MidasLoader::loadModel('ItemRevision'); /** @var BitstreamModel $BitstreamModel */ $BitstreamModel = MidasLoader::loadModel('Bitstream'); /** @var MetadataModel $MetadataModel */ $MetadataModel = MidasLoader::loadModel('Metadata'); /** @var ItempolicygroupModel $ItemPolicyGroupModel */ $ItemPolicyGroupModel = MidasLoader::loadModel('Itempolicygroup'); $ItemPolicyGroupModel->computePolicyStatus($newItem); foreach ($itemDao->getRevisions() as $revision) { $dupItemRevision = new ItemRevisionDao(); $dupItemRevision->setItemId($newItem->getItemId()); $dupItemRevision->setRevision($revision->getRevision()); $dupItemRevision->setDate($revision->getDate()); $dupItemRevision->setChanges($revision->getChanges()); $dupItemRevision->setUserId($userDao->getUserId()); $dupItemRevision->setLicenseId($revision->getLicenseId()); $ItemRevisionModel->save($dupItemRevision); // duplicate metadata value $metadatavalues = $ItemRevisionModel->getMetadata($revision); foreach ($metadatavalues as $metadata) { $MetadataModel->addMetadataValue($dupItemRevision, $metadata->getMetadatatype(), $metadata->getElement(), $metadata->getQualifier(), $metadata->getValue(), false); } // duplicate bitstream foreach ($revision->getBitstreams() as $bitstream) { $dupBitstream = new BitstreamDao(); $dupBitstream->setItemrevisionId($dupItemRevision->getItemrevisionId()); $dupBitstream->setName($bitstream->getName()); $dupBitstream->setMimetype($bitstream->getMimetype()); $dupBitstream->setSizebytes($bitstream->getSizebytes()); $dupBitstream->setChecksum($bitstream->getChecksum()); $dupBitstream->setPath($bitstream->getPath()); $dupBitstream->setAssetstoreId($bitstream->getAssetstoreId()); $dupBitstream->setDate($bitstream->getDate()); $BitstreamModel->save($dupBitstream); } } $this->save($newItem, true); // call save with metadata changed flag return $newItem; }
/** * Get random items. * * @param null|UserDao $userDao * @param int $policy * @param int $limit * @param bool $thumbnailFilter * @return array * @throws Zend_Exception */ public function getRandomThumbnails($userDao = null, $policy = 0, $limit = 10, $thumbnailFilter = false) { if ($userDao == null) { $userId = -1; } elseif (!$userDao instanceof UserDao) { throw new Zend_Exception('Should be an user.'); } else { $userId = $userDao->getUserId(); } if (Zend_Registry::get('configDatabase')->database->adapter === 'PDO_MYSQL') { $rand = 'RAND()'; } else { $rand = 'random()'; } if (Zend_Registry::get('configDatabase')->database->adapter == 'PDO_SQLITE') { $floor = 'CAST(tt.maxid*' . $rand . ' AS INTEGER)'; } else { $floor = 'FLOOR(tt.maxid*' . $rand . ')'; } $sql = $this->database->select()->setIntegrityCheck(false)->from(array('i' => 'item'))->join(array('tt' => $this->database->select()->from(array('i' => 'item'), array('maxid' => 'MAX(item_id)'))), ' i.item_id >= ' . $floor)->joinLeft(array('ip' => 'itempolicyuser'), ' i.item_id = ip.item_id AND ' . $this->database->getDB()->quoteInto('ip.policy >= ?', $policy) . ' AND ' . $this->database->getDB()->quoteInto('user_id = ? ', $userId) . ' ', array('userpolicy' => 'ip.policy'))->joinLeft(array('ipg' => 'itempolicygroup'), ' i.item_id = ipg.item_id AND ' . $this->database->getDB()->quoteInto('ipg.policy >= ?', $policy) . ' AND ( ' . $this->database->getDB()->quoteInto('group_id = ? ', MIDAS_GROUP_ANONYMOUS_KEY) . ' OR group_id IN (' . new Zend_Db_Expr($this->database->select()->setIntegrityCheck(false)->from(array('u2g' => 'user2group'), array('group_id'))->where('u2g.user_id = ?', $userId)) . '))', array('grouppolicy' => 'ipg.policy'))->where('( ip.item_id is not null or ipg.item_id is not null)')->limit($limit); if ($thumbnailFilter) { $sql->where('NOT thumbnail_id IS NULL', ''); } $rowset = $this->database->fetchAll($sql); $rowsetAnalysed = array(); foreach ($rowset as $row) { if ($row['userpolicy'] == null) { $row['userpolicy'] = 0; } if ($row['grouppolicy'] == null) { $row['grouppolicy'] = 0; } if (!isset($rowsetAnalysed[$row['item_id']]) || $rowsetAnalysed[$row['item_id']]->policy < $row['userpolicy'] && $rowsetAnalysed[$row['item_id']]->policy < $row['grouppolicy']) { $tmpDao = $this->initDao('Item', $row); if ($row['userpolicy'] >= $row['grouppolicy']) { $tmpDao->policy = $row['userpolicy']; } else { $tmpDao->policy = $row['grouppolicy']; } $rowsetAnalysed[$row['item_id']] = $tmpDao; unset($tmpDao); } } return $rowsetAnalysed; }