-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #618 from takeit/garbage
[CS-5238] - Implement a configurable autodelete mechanism for pending us...
- Loading branch information
Showing
14 changed files
with
213 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
newscoop/install/Resources/templates/cron_jobs/_newscoop_user_garbage.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
30 0 * * * {{ bin_directory }}/../application/console user:garbage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
newscoop/library/Newscoop/Services/GarbageCollectionService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/** | ||
* @package Newscoop | ||
* @copyright 2012 Sourcefabric o.p.s. | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop\Services; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Newscoop\Entity\User; | ||
|
||
/** | ||
* Deletes inactive users after defined days | ||
*/ | ||
class GarbageCollectionService | ||
{ | ||
/** | ||
* @var Doctrine\ORM\EntityManager | ||
*/ | ||
protected $em; | ||
|
||
/** | ||
* @param Doctrine\ORM\EntityManager $em | ||
*/ | ||
public function __construct(EntityManager $em) | ||
{ | ||
$this->em = $em; | ||
} | ||
|
||
/** | ||
* Run users garbage collection | ||
* | ||
* @param string $days | ||
* | ||
* @return void | ||
*/ | ||
public function run($days) | ||
{ | ||
$this->gcUsers($days); | ||
$this->gcTokens(); | ||
} | ||
|
||
/** | ||
* Remove obsolete users | ||
* | ||
* @param string $days | ||
* | ||
* @return void | ||
*/ | ||
private function gcUsers($days) | ||
{ | ||
$query = $this->em->createQueryBuilder() | ||
->delete('Newscoop\Entity\User', 'u') | ||
->where('u.created < :ttl') | ||
->andWhere('u.status = :status') | ||
->getQuery(); | ||
|
||
$query->setParameter('ttl', new \DateTime('-'.$days.' days')); | ||
$query->setParameter('status', User::STATUS_INACTIVE); | ||
$query->execute(); | ||
} | ||
|
||
/** | ||
* Remove obsolete tokens | ||
* | ||
* @return void | ||
*/ | ||
private function gcTokens() | ||
{ | ||
$query = $this->em->createQueryBuilder() | ||
->delete('Newscoop\Entity\UserToken', 't') | ||
->where('t.user NOT IN (SELECT u.id FROM Newscoop\Entity\User u)') | ||
->getQuery(); | ||
|
||
$query->execute(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
newscoop/library/Newscoop/Tools/Console/Command/UserGarbageCollectionCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* @package Newscoop | ||
* @author Rafał Muszyński <[email protected]> | ||
* @copyright 2014 Sourcefabric o.p.s. | ||
* @license http://www.gnu.org/licenses/gpl-3.0.txt | ||
*/ | ||
|
||
namespace Newscoop\Tools\Console\Command; | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
|
||
/** | ||
* Removes obsolete pending users data | ||
*/ | ||
class UserGarbageCollectionCommand extends ContainerAwareCommand | ||
{ | ||
/** | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('user:garbage') | ||
->setDescription('Users Garbage Collection') | ||
->setHelp("Removes obsolete pending users data") | ||
->addOption('force', null, InputOption::VALUE_NONE, 'If set, foreces command execution omitting system preferences settings.'); | ||
} | ||
|
||
/** | ||
*/ | ||
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) | ||
{ | ||
$systemPreferences = $this->getContainer()->getService('system_preferences_service'); | ||
$text = '<info>Obsolete pending users successfuly removed.</info>'; | ||
|
||
try { | ||
if ($input->getOption('force')) { | ||
$this->getContainer()->getService('user.garbage')->run($systemPreferences->get('userGarbageDays')); | ||
if ($input->getOption('verbose')) { | ||
$output->writeln($text); | ||
} | ||
} elseif ($systemPreferences->get('userGarbageActive') === 'Y' && !is_null($systemPreferences->get('userGarbageActive'))) { | ||
$this->getContainer()->getService('user.garbage')->run($systemPreferences->get('userGarbageDays')); | ||
if ($input->getOption('verbose')) { | ||
$output->writeln($text); | ||
} | ||
} | ||
} catch (\Exception $e) { | ||
throw new \Exception($e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.