function listUsersNotEditors(Application $app, Request $request)
 {
     $repo = new UserHasNoEditorPermissionsInSiteRepository();
     if ($request->request->get('action') == "add" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $ur = new UserAccountRepository();
         $user = $ur->loadByUserName($request->request->get('username'));
         if ($user) {
             $repo->addUserToSite($user, $app['currentSite'], $app['currentUser']);
             return $app->redirect('/admin/usernoteditor/');
         }
     } else {
         if ($request->request->get('action') == "remove" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
             $ur = new UserAccountRepository();
             $user = $ur->loadByID($request->request->get('id'));
             if ($user) {
                 $repo->removeUserFromSite($user, $app['currentSite'], $app['currentUser']);
                 return $app->redirect('/admin/usernoteditor/');
             }
         }
     }
     $userAccountRepoBuilder = new UserAccountRepositoryBuilder();
     $userAccountRepoBuilder->setUserHasNoEditorPermissionsInSite($app['currentSite']);
     return $app['twig']->render('site/admin/listUsersNotEditors.html.twig', array('users' => $userAccountRepoBuilder->fetchAll()));
 }
 function testAddAndRemove()
 {
     $this->addCountriesToTestDB();
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $siteModel = new \models\SiteModel();
     $siteModel->setTitle("Test");
     $siteModel->setSlug("test");
     $siteRepository = new \repositories\SiteRepository();
     $countryRepository = new \repositories\CountryRepository();
     $siteRepository->create($siteModel, $user, array($countryRepository->loadByTwoCharCode("GB")), $this->getSiteQuotaUsedForTesting(), true);
     // ########################################## Not there
     $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository();
     $this->assertFalse($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $siteModel));
     $userAccountRepoBuilder = new \repositories\builders\UserAccountRepositoryBuilder();
     $userAccountRepoBuilder->setUserHasNoEditorPermissionsInSite($siteModel);
     $this->assertEquals(0, count($userAccountRepoBuilder->fetchAll()));
     // ########################################## Add
     $userHasNoEditorPermissionsInSiteRepo->addUserToSite($user, $siteModel);
     // ########################################## There
     $this->assertTrue($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $siteModel));
     $userAccountRepoBuilder = new \repositories\builders\UserAccountRepositoryBuilder();
     $userAccountRepoBuilder->setUserHasNoEditorPermissionsInSite($siteModel);
     $this->assertEquals(1, count($userAccountRepoBuilder->fetchAll()));
     // ########################################## Remove
     $userHasNoEditorPermissionsInSiteRepo->removeUserFromSite($user, $siteModel);
     // ########################################## There
     $this->assertFalse($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $siteModel));
     $userAccountRepoBuilder = new \repositories\builders\UserAccountRepositoryBuilder();
     $userAccountRepoBuilder->setUserHasNoEditorPermissionsInSite($siteModel);
     $this->assertEquals(0, count($userAccountRepoBuilder->fetchAll()));
 }
 public static function run(Application $app, $verbose = false)
 {
     global $CONFIG;
     if ($verbose) {
         print "Starting " . date("c") . "\n";
     }
     $userRepo = new UserAccountRepository();
     $siteRepo = new SiteRepository();
     $groupRepo = new GroupRepository();
     $eventRepo = new EventRepository();
     $userWatchesGroupRepository = new UserWatchesGroupRepository();
     $userWatchesGroupStopRepository = new UserWatchesGroupStopRepository();
     $userAccountGeneralSecurityKeyRepository = new UserAccountGeneralSecurityKeyRepository();
     $userNotificationRepo = new UserNotificationRepository();
     $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository();
     $userPermissionsRepo = new UserPermissionsRepository($app['extensions']);
     /** @var usernotifications/UserWatchesGroupPromptNotificationType **/
     $userNotificationType = $app['extensions']->getCoreExtension()->getUserNotificationType('UserWatchesGroupPrompt');
     $b = new UserWatchesGroupRepositoryBuilder();
     foreach ($b->fetchAll() as $userWatchesGroup) {
         $user = $userRepo->loadByID($userWatchesGroup->getUserAccountId());
         $group = $groupRepo->loadById($userWatchesGroup->getGroupId());
         $site = $siteRepo->loadById($group->getSiteID());
         // This is not the most efficient as it involves DB access and the results might not be used. But it'll do for now.
         $userPermissions = $userPermissionsRepo->getPermissionsForUserInSite($user, $site, false, true);
         if ($verbose) {
             print date("c") . " User " . $user->getEmail() . " Site " . $site->getTitle() . " Group " . $group->getTitle() . "\n";
         }
         // UserWatchesGroupRepositoryBuilder() should only return instances where site is not also watched
         if ($site->getIsClosedBySysAdmin()) {
             if ($verbose) {
                 print " ... site is closed\n";
             }
         } else {
             if ($group->getIsDeleted()) {
                 if ($verbose) {
                     print " ... group is deleted\n";
                 }
             } else {
                 if ($userHasNoEditorPermissionsInSiteRepo->isUserInSite($user, $site)) {
                     if ($verbose) {
                         print " ... user does not have edit permissions allowed in site\n";
                     }
                 } else {
                     if (!$userPermissions->hasPermission("org.openacalendar", "CALENDAR_CHANGE")) {
                         if ($verbose) {
                             print " ... user does not have org.openacalendar/CALENDAR_CHANGE permission in site\n";
                         }
                         // Technically UserWatchesSiteRepositoryBuilder() should only return getIsWatching() == true but lets double check
                     } else {
                         if ($userWatchesGroup->getIsWatching()) {
                             if ($verbose) {
                                 print " ... searching for data\n";
                             }
                             $lastEvent = $eventRepo->loadLastNonDeletedNonImportedByStartTimeInGroupId($group->getId());
                             $data = $userWatchesGroup->getPromptEmailData($site, $lastEvent);
                             if ($data['moreEventsNeeded']) {
                                 if ($verbose) {
                                     print " ... found data\n";
                                 }
                                 ///// Notification Class
                                 $userNotification = $userNotificationType->getNewNotification($user, $site);
                                 $userNotification->setGroup($group);
                                 ////// Save Notification Class
                                 $userNotificationRepo->create($userNotification);
                                 ////// Send Email
                                 if ($userNotification->getIsEmail()) {
                                     $userWatchesGroupStop = $userWatchesGroupStopRepository->getForUserAndGroup($user, $group);
                                     configureAppForSite($site);
                                     configureAppForUser($user);
                                     $userAccountGeneralSecurityKey = $userAccountGeneralSecurityKeyRepository->getForUser($user);
                                     $unsubscribeURL = $CONFIG->getWebIndexDomainSecure() . '/you/emails/' . $user->getId() . '/' . $userAccountGeneralSecurityKey->getAccessKey();
                                     $lastEventsBuilder = new EventRepositoryBuilder();
                                     $lastEventsBuilder->setSite($site);
                                     $lastEventsBuilder->setGroup($group);
                                     $lastEventsBuilder->setOrderByStartAt(true);
                                     $lastEventsBuilder->setIncludeDeleted(false);
                                     $lastEventsBuilder->setIncludeImported(false);
                                     $lastEventsBuilder->setLimit($CONFIG->userWatchesGroupPromptEmailShowEvents);
                                     $lastEvents = $lastEventsBuilder->fetchAll();
                                     $message = \Swift_Message::newInstance();
                                     $message->setSubject("Any news about " . $group->getTitle() . "?");
                                     $message->setFrom(array($CONFIG->emailFrom => $CONFIG->emailFromName));
                                     $message->setTo($user->getEmail());
                                     $messageText = $app['twig']->render('email/userWatchesGroupPromptEmail.txt.twig', array('group' => $group, 'user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                     if ($CONFIG->isDebug) {
                                         file_put_contents('/tmp/userWatchesGroupPromptEmail.txt', $messageText);
                                     }
                                     $message->setBody($messageText);
                                     $messageHTML = $app['twig']->render('email/userWatchesGroupPromptEmail.html.twig', array('group' => $group, 'user' => $user, 'lastEvents' => $lastEvents, 'stopCode' => $userWatchesGroupStop->getAccessKey(), 'generalSecurityCode' => $userAccountGeneralSecurityKey->getAccessKey(), 'unsubscribeURL' => $unsubscribeURL));
                                     if ($CONFIG->isDebug) {
                                         file_put_contents('/tmp/userWatchesGroupPromptEmail.html', $messageHTML);
                                     }
                                     $message->addPart($messageHTML, 'text/html');
                                     $headers = $message->getHeaders();
                                     $headers->addTextHeader('List-Unsubscribe', $unsubscribeURL);
                                     if ($verbose) {
                                         print " ... sending\n";
                                     }
                                     if (!$CONFIG->isDebug) {
                                         $app['mailer']->send($message);
                                     }
                                     $userNotificationRepo->markEmailed($userNotification);
                                 }
                                 $userWatchesGroupRepository->markPromptEmailSent($userWatchesGroup, $data['checkTime']);
                             }
                         }
                     }
                 }
             }
         }
     }
     if ($verbose) {
         print "Finished " . date("c") . "\n";
     }
 }
        header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + 30 * 60));
        $data = array();
        // TODO would like to depreceate httpDomain and get scripts to just use httpDomainIndex & httpDomainSite for clarity
        $data['httpDomain'] = $site->getSlug() . "." . $CONFIG->webSiteDomain;
        $data['httpDomainIndex'] = $CONFIG->webIndexDomain;
        if ($CONFIG->hasSSL) {
            $data['hasSSL'] = true;
            $data['httpsDomain'] = $site->getSlug() . "." . $CONFIG->webSiteDomainSSL;
            $data['httpsDomainIndex'] = $CONFIG->webIndexDomainSSL;
        } else {
            $data['hasSSL'] = false;
        }
        $data['twitter'] = $CONFIG->contactTwitter;
        $data['isSingleSiteMode'] = false;
        $user = userGetCurrent();
        if ($user) {
            $data['currentUser'] = array('username' => $user->getUsername());
        } else {
            $data['currentUser'] = false;
        }
        $removeEditorPermissions = false;
        $userHasNoEditorPermissionsInSiteRepo = new UserHasNoEditorPermissionsInSiteRepository();
        if ($app['currentUser'] && $userHasNoEditorPermissionsInSiteRepo->isUserInSite($app['currentUser'], $site)) {
            $removeEditorPermissions = true;
        }
        $userPermissionsRepo = new \repositories\UserPermissionsRepository($app['extensions']);
        $currentUserPermissions = $userPermissionsRepo->getPermissionsForUserInSite($user, $site, $removeEditorPermissions, true);
        $data['currentUserPermissions'] = $currentUserPermissions->getAsArrayForJSON();
        print "var config = " . json_encode($data);
    }
}