Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Ignore Origin rules to database #4026

Merged
merged 8 commits into from Apr 26, 2020
64 changes: 64 additions & 0 deletions app/DoctrineMigrations/Version20190826204730.php
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

/**
* Add tables for the ignore origin rules.
*/
final class Version20190826204730 extends WallabagMigration
{
public function up(Schema $schema): void
{
if (false === $schema->hasTable($this->getTable('ignore_origin_user_rule'))) {
$userTable = $schema->createTable($this->getTable('ignore_origin_user_rule', true));
$userTable->addColumn('id', 'integer', ['autoincrement' => true]);
$userTable->addColumn('config_id', 'integer');
$userTable->addColumn('rule', 'string', ['length' => 255]);
$userTable->addIndex(['config_id'], 'idx_config');
$userTable->setPrimaryKey(['id']);
$userTable->addForeignKeyConstraint($this->getTable('config'), ['config_id'], ['id'], [], 'fk_config');

if ('postgresql' === $this->connection->getDatabasePlatform()->getName()) {
$schema->dropSequence('ignore_origin_user_rule_id_seq');
$schema->createSequence('ignore_origin_user_rule_id_seq');
}
}

if (false === $schema->hasTable($this->getTable('ignore_origin_instance_rule'))) {
$instanceTable = $schema->createTable($this->getTable('ignore_origin_instance_rule', true));
$instanceTable->addColumn('id', 'integer', ['autoincrement' => true]);
$instanceTable->addColumn('rule', 'string', ['length' => 255]);
$instanceTable->setPrimaryKey(['id']);

if ('postgresql' === $this->connection->getDatabasePlatform()->getName()) {
$schema->dropSequence('ignore_origin_instance_rule_id_seq');
$schema->createSequence('ignore_origin_instance_rule_id_seq');
}
}
}

public function postUp(Schema $schema): void
{
foreach ($this->container->getParameter('wallabag_core.default_ignore_origin_instance_rules') as $entity) {
$previous_rule = $this->container
->get('doctrine.orm.default_entity_manager')
->getConnection()
->fetchArray('SELECT * FROM ' . $this->getTable('ignore_origin_instance_rule') . " WHERE rule = '" . $entity['rule'] . "'");

if (false === $previous_rule) {
$this->addSql('INSERT INTO ' . $this->getTable('ignore_origin_instance_rule') . " (rule) VALUES ('" . $entity['rule'] . "');");
}
}
}

public function down(Schema $schema): void
{
$schema->dropTable($this->getTable('ignore_origin_user_rule'));
$schema->dropTable($this->getTable('ignore_origin_instance_rule'));
}
}
1 change: 1 addition & 0 deletions app/config/security.yml
Expand Up @@ -79,4 +79,5 @@ security:
- { path: ^/annotations, roles: ROLE_USER }
- { path: ^/2fa, role: IS_AUTHENTICATED_2FA_IN_PROGRESS }
- { path: ^/users, roles: ROLE_SUPER_ADMIN }
- { path: ^/ignore-origin-instance-rules, roles: ROLE_SUPER_ADMIN }
- { path: ^/, roles: ROLE_USER }
8 changes: 8 additions & 0 deletions app/config/wallabag.yml
Expand Up @@ -165,6 +165,14 @@ wallabag_core:
value: 0
section: entry

default_ignore_origin_instance_rules:
-
rule: host = "feedproxy.google.com"
-
rule: host = "feeds.reuters.com"
-
rule: _all ~ "https?://www\.lemonde\.fr/tiny.*"

wallabag_user:
registration_enabled: "%fosuser_registration%"

Expand Down
8 changes: 8 additions & 0 deletions src/Wallabag/CoreBundle/Command/InstallCommand.php
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
use Wallabag\CoreBundle\Entity\InternalSetting;

class InstallCommand extends ContainerAwareCommand
Expand Down Expand Up @@ -277,6 +278,7 @@ protected function setupConfig()

// cleanup before insert new stuff
$em->createQuery('DELETE FROM WallabagCoreBundle:InternalSetting')->execute();
$em->createQuery('DELETE FROM WallabagCoreBundle:IgnoreOriginInstanceRule')->execute();

foreach ($this->getContainer()->getParameter('wallabag_core.default_internal_settings') as $setting) {
$newSetting = new InternalSetting();
Expand All @@ -286,6 +288,12 @@ protected function setupConfig()
$em->persist($newSetting);
}

foreach ($this->getContainer()->getParameter('wallabag_core.default_ignore_origin_instance_rules') as $ignore_origin_instance_rule) {
$newIgnoreOriginInstanceRule = new IgnoreOriginInstanceRule();
$newIgnoreOriginInstanceRule->setRule($ignore_origin_instance_rule['rule']);
$em->persist($newIgnoreOriginInstanceRule);
}

$em->flush();

$this->io->text('<info>Config successfully setup.</info>');
Expand Down
79 changes: 77 additions & 2 deletions src/Wallabag/CoreBundle/Controller/ConfigController.php
Expand Up @@ -14,10 +14,13 @@
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\IgnoreOriginUserRule;
use Wallabag\CoreBundle\Entity\RuleInterface;
use Wallabag\CoreBundle\Entity\TaggingRule;
use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
use Wallabag\CoreBundle\Form\Type\ConfigType;
use Wallabag\CoreBundle\Form\Type\FeedType;
use Wallabag\CoreBundle\Form\Type\IgnoreOriginUserRuleType;
use Wallabag\CoreBundle\Form\Type\TaggingRuleImportType;
use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
use Wallabag\CoreBundle\Form\Type\UserInformationType;
Expand Down Expand Up @@ -173,6 +176,40 @@ public function indexAction(Request $request)
return $this->redirect($this->generateUrl('config') . '#set5');
}

// handle ignore origin rules
$ignoreOriginUserRule = new IgnoreOriginUserRule();
$action = $this->generateUrl('config') . '#set6';

if ($request->query->has('ignore-origin-user-rule')) {
$ignoreOriginUserRule = $this->getDoctrine()
->getRepository('WallabagCoreBundle:IgnoreOriginUserRule')
->find($request->query->get('ignore-origin-user-rule'));

if ($this->getUser()->getId() !== $ignoreOriginUserRule->getConfig()->getUser()->getId()) {
return $this->redirect($action);
}

$action = $this->generateUrl('config', [
'ignore-origin-user-rule' => $ignoreOriginUserRule->getId(),
]) . '#set6';
}

$newIgnoreOriginUserRule = $this->createForm(IgnoreOriginUserRuleType::class, $ignoreOriginUserRule, ['action' => $action]);
$newIgnoreOriginUserRule->handleRequest($request);

if ($newIgnoreOriginUserRule->isSubmitted() && $newIgnoreOriginUserRule->isValid()) {
$ignoreOriginUserRule->setConfig($config);
$em->persist($ignoreOriginUserRule);
$em->flush();

$this->addFlash(
'notice',
'flashes.config.notice.ignore_origin_rules_updated'
);

return $this->redirect($this->generateUrl('config') . '#set6');
}

return $this->render('WallabagCoreBundle:Config:index.html.twig', [
'form' => [
'config' => $configForm->createView(),
Expand All @@ -181,6 +218,7 @@ public function indexAction(Request $request)
'user' => $userForm->createView(),
'new_tagging_rule' => $newTaggingRule->createView(),
'import_tagging_rule' => $taggingRulesImportform->createView(),
'new_ignore_origin_user_rule' => $newIgnoreOriginUserRule->createView(),
],
'feed' => [
'username' => $user->getUsername(),
Expand Down Expand Up @@ -447,6 +485,43 @@ public function editTaggingRuleAction(TaggingRule $rule)
return $this->redirect($this->generateUrl('config') . '?tagging-rule=' . $rule->getId() . '#set5');
}

/**
* Deletes an ignore origin rule and redirect to the config homepage.
*
* @Route("/ignore-origin-user-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_ignore_origin_rule")
*
* @return RedirectResponse
*/
public function deleteIgnoreOriginRuleAction(IgnoreOriginUserRule $rule)
{
$this->validateRuleAction($rule);

$em = $this->getDoctrine()->getManager();
$em->remove($rule);
$em->flush();

$this->addFlash(
'notice',
'flashes.config.notice.ignore_origin_rules_deleted'
);

return $this->redirect($this->generateUrl('config') . '#set6');
}

/**
* Edit an ignore origin rule.
*
* @Route("/ignore-origin-user-rule/edit/{id}", requirements={"id" = "\d+"}, name="edit_ignore_origin_rule")
*
* @return RedirectResponse
*/
public function editIgnoreOriginRuleAction(IgnoreOriginUserRule $rule)
{
$this->validateRuleAction($rule);

return $this->redirect($this->generateUrl('config') . '?ignore-origin-user-rule=' . $rule->getId() . '#set6');
}

/**
* Remove all annotations OR tags OR entries for the current user.
*
Expand Down Expand Up @@ -659,10 +734,10 @@ private function removeAnnotationsForArchivedByUserId($userId)
/**
* Validate that a rule can be edited/deleted by the current user.
*/
private function validateRuleAction(TaggingRule $rule)
private function validateRuleAction(RuleInterface $rule)
{
if ($this->getUser()->getId() !== $rule->getConfig()->getUser()->getId()) {
throw $this->createAccessDeniedException('You can not access this tagging rule.');
throw $this->createAccessDeniedException('You can not access this rule.');
}
}

Expand Down
@@ -0,0 +1,138 @@
<?php

namespace Wallabag\CoreBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;

/**
* IgnoreOriginInstanceRuleController controller.
*
* @Route("/ignore-origin-instance-rules")
*/
class IgnoreOriginInstanceRuleController extends Controller
{
/**
* Lists all IgnoreOriginInstanceRule entities.
*
* @Route("/", name="ignore_origin_instance_rules_index", methods={"GET"})
*/
public function indexAction()
{
$rules = $this->get('wallabag_core.ignore_origin_instance_rule_repository')->findAll();

return $this->render('WallabagCoreBundle:IgnoreOriginInstanceRule:index.html.twig', [
'rules' => $rules,
]);
}

/**
* Creates a new ignore origin instance rule entity.
*
* @Route("/new", name="ignore_origin_instance_rules_new", methods={"GET", "POST"})
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function newAction(Request $request)
{
$ignoreOriginInstanceRule = new IgnoreOriginInstanceRule();

$form = $this->createForm('Wallabag\CoreBundle\Form\Type\IgnoreOriginInstanceRuleType', $ignoreOriginInstanceRule);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($ignoreOriginInstanceRule);
$em->flush();

$this->get('session')->getFlashBag()->add(
'notice',
$this->get('translator')->trans('flashes.ignore_origin_instance_rule.notice.added')
);

return $this->redirectToRoute('ignore_origin_instance_rules_index');
}

return $this->render('WallabagCoreBundle:IgnoreOriginInstanceRule:new.html.twig', [
'rule' => $ignoreOriginInstanceRule,
'form' => $form->createView(),
]);
}

/**
* Displays a form to edit an existing ignore origin instance rule entity.
*
* @Route("/{id}/edit", name="ignore_origin_instance_rules_edit", methods={"GET", "POST"})
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function editAction(Request $request, IgnoreOriginInstanceRule $ignoreOriginInstanceRule)
{
$deleteForm = $this->createDeleteForm($ignoreOriginInstanceRule);
$editForm = $this->createForm('Wallabag\CoreBundle\Form\Type\IgnoreOriginInstanceRuleType', $ignoreOriginInstanceRule);
$editForm->handleRequest($request);

if ($editForm->isSubmitted() && $editForm->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($ignoreOriginInstanceRule);
$em->flush();

$this->get('session')->getFlashBag()->add(
'notice',
$this->get('translator')->trans('flashes.ignore_origin_instance_rule.notice.updated')
);

return $this->redirectToRoute('ignore_origin_instance_rules_index');
}

return $this->render('WallabagCoreBundle:IgnoreOriginInstanceRule:edit.html.twig', [
'rule' => $ignoreOriginInstanceRule,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
]);
}

/**
* Deletes a site credential entity.
*
* @Route("/{id}", name="ignore_origin_instance_rules_delete", methods={"DELETE"})
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function deleteAction(Request $request, IgnoreOriginInstanceRule $ignoreOriginInstanceRule)
{
$form = $this->createDeleteForm($ignoreOriginInstanceRule);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$this->get('session')->getFlashBag()->add(
'notice',
$this->get('translator')->trans('flashes.ignore_origin_instance_rule.notice.deleted')
);

$em = $this->getDoctrine()->getManager();
$em->remove($ignoreOriginInstanceRule);
$em->flush();
}

return $this->redirectToRoute('ignore_origin_instance_rules_index');
}

/**
* Creates a form to delete a ignore origin instance rule entity.
*
* @param IgnoreOriginInstanceRule $ignoreOriginInstanceRule The ignore origin instance rule entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(IgnoreOriginInstanceRule $ignoreOriginInstanceRule)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('ignore_origin_instance_rules_delete', ['id' => $ignoreOriginInstanceRule->getId()]))
->setMethod('DELETE')
->getForm()
;
}
}