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