/**
  * Deletes all new user invitations corresponding to a community.
  *
  * @param CommunityDao $community
  * @throws Zend_Exception
  */
 public function deleteByCommunity($community)
 {
     if (!$community instanceof CommunityDao) {
         throw new Zend_Exception('Must pass a community dao');
     }
     Zend_Registry::get('dbAdapter')->delete($this->_name, 'community_id = ' . $community->getKey());
 }
 /**
  * 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;
     }
 }
 /**
  * Remove invitation.
  *
  * @param CommunityDao $communityDao
  * @param UserDao $userDao
  * @return bool
  * @throws Zend_Exception
  */
 public function removeInvitation($communityDao, $userDao)
 {
     if ($userDao == null) {
         return false;
     }
     $invitations = $userDao->getInvitations();
     foreach ($invitations as $invitation) {
         if ($invitation->getCommunityId() == $communityDao->getKey()) {
             /** @var FeedModel $feedModel */
             $feedModel = MidasLoader::loadModel('Feed');
             $feeds = $feedModel->getFeedByResourceAndType(array(MIDAS_FEED_COMMUNITY_INVITATION), $invitation);
             foreach ($feeds as $feed) {
                 $feedModel->delete($feed);
             }
             $this->delete($invitation);
             return true;
         }
     }
     return false;
 }
Exemple #4
0
 /**
  * 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;
 }
Exemple #5
0
 /**
  * Create or find an item with the given name in the given community.
  *
  * @param string $itemName item name
  * @param CommunityDao $community community DAO
  * @return ItemDao item DAO
  * @throws Exception
  */
 private function _createOrFindByName($itemName, $community)
 {
     /** @var ItemModel $itemModel */
     $itemModel = MidasLoader::loadModel('Item');
     $items = $itemModel->getByName($itemName);
     if (count($items) === 0) {
         $folders = $community->getFolder()->getFolders();
         $privateFolder = null;
         /** @var FolderDao $folder */
         foreach ($folders as $folder) {
             if ($folder->getName() === 'Private' && $folder->getPrivacyStatus() === MIDAS_PRIVACY_PRIVATE) {
                 $privateFolder = $folder;
                 break;
             }
         }
         if (is_null($privateFolder)) {
             throw new Exception('No private folder in the given community in which to create an item', -1);
         }
         return $itemModel->createItem($itemName, '', $privateFolder);
     }
     return $items[0];
 }
Exemple #6
0
 /**
  * Get items shared to the given community.
  *
  * @param CommunityDao $communityDao
  * @param int $limit
  * @return array
  * @throws Zend_Exception
  */
 public function getSharedToCommunity($communityDao, $limit = 20)
 {
     $groupId = $communityDao->getMemberGroup()->getKey();
     if (!is_numeric($groupId)) {
         throw new Zend_Exception('Error in parameter groupId when getting items shared to community.');
     }
     $sql = $this->database->select()->setIntegrityCheck(false)->from(array('i' => 'item'))->join(array('p' => 'itempolicygroup'), 'i.item_id = p.item_id', array('p.policy', 'policy_date' => 'p.date'))->where('group_id = ? ', $groupId)->order(array('p.date DESC'))->limit($limit);
     $rowset = $this->database->fetchAll($sql);
     $results = array();
     foreach ($rowset as $row) {
         $tmp = $this->initDao('Item', $row);
         $tmp->policy = $row['policy'];
         $tmp->policy_date = $row['policy_date'];
         $results[] = $tmp;
     }
     return $results;
 }