/**
  * 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;
     }
 }
Exemple #2
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;
 }