Merge pull request #1904 from wallabag/feature-public-mode

Share entry with a public URL
This commit is contained in:
Jeremy Benoist 2016-08-25 09:30:51 +02:00 committed by GitHub
commit 60e7220406
33 changed files with 346 additions and 9 deletions

View file

@ -0,0 +1,46 @@
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Version20160410190541 extends AbstractMigration implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
}
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->addSql('ALTER TABLE `'.$this->getTable('entry').'` ADD `uuid` LONGTEXT DEFAULT NULL');
$this->addSql("INSERT INTO `".$this->getTable('craue_config_setting')."` (`name`, `value`, `section`) VALUES ('share_public', '1', 'entry')");
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'sqlite', 'This down migration can\'t be executed on SQLite databases, because SQLite don\'t support DROP COLUMN.');
$this->addSql('ALTER TABLE `'.$this->getTable('entry').'` DROP `uuid`');
$this->addSql("DELETE FROM `".$this->getTable('craue_config_setting')."` WHERE `name` = 'share_public'");
}
}

View file

@ -4,15 +4,32 @@ namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Version20160812120952 extends AbstractMigration
class Version20160812120952 extends AbstractMigration implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
private function getTable($tableName)
{
return $this->container->getParameter('database_table_prefix') . $tableName;
}
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$this->addSql('ALTER TABLE wallabag_oauth2_clients ADD name CLOB DEFAULT NULL COLLATE BINARY');
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' ADD name longtext COLLATE \'utf8_unicode_ci\' DEFAULT NULL');
}
/**
@ -21,7 +38,7 @@ class Version20160812120952 extends AbstractMigration
public function down(Schema $schema)
{
$this->abortIf($this->connection->getDatabasePlatform()->getName() == 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
$this->addSql('ALTER TABLE wallabag_oauth2_clients DROP COLUMN name;
');
$this->addSql('ALTER TABLE '.$this->getTable('oauth2_clients').' DROP COLUMN name');
}
}

View file

@ -27,3 +27,4 @@ piwik_site_id: ID for din side hos Piwik
piwik_enabled: Aktiver Piwik
demo_mode_enabled: "Aktiver demo-indstilling? (anvendes kun til wallabags offentlige demo)"
demo_mode_username: "Demobruger"
# share_public: Allow public url for entries

View file

@ -27,3 +27,4 @@ piwik_site_id: ID deiner Webseite in Piwik
piwik_enabled: Piwik aktivieren
demo_mode_enabled: "Test-Modus aktivieren? (nur für die öffentliche wallabag-Demo genutzt)"
demo_mode_username: "Test-Benutzer"
# share_public: Allow public url for entries

View file

@ -27,3 +27,4 @@ piwik_site_id: ID of your website in Piwik
piwik_enabled: Enable Piwik
demo_mode_enabled: "Enable demo mode ? (only used for the wallabag public demo)"
demo_mode_username: "Demo user"
share_public: Allow public url for entries

View file

@ -27,3 +27,4 @@ piwik_site_id: ID de tu website de Piwik
piwik_enabled: Activar Piwik
demo_mode_enabled: "Activar modo demo (sólo usado para la demo de wallabag)"
demo_mode_username: "Nombre de usuario demo"
# share_public: Allow public url for entries

View file

@ -22,3 +22,9 @@ export: "برون‌سپاری"
import: "درون‌ریزی"
misc: "غیره"
modify_settings: "اعمال"
# piwik_host: Host of your website in Piwik
# piwik_site_id: ID of your website in Piwik
# piwik_enabled: Enable Piwik
# demo_mode_enabled: "Enable demo mode ? (only used for the wallabag public demo)"
# demo_mode_username: "Demo user"
# share_public: Allow public url for entries

View file

@ -27,3 +27,4 @@ piwik_site_id: ID de votre site dans Piwik
piwik_enabled: Activer Piwik
demo_mode_enabled: "Activer le mode démo ? (utiliser uniquement pour la démo publique de wallabag)"
demo_mode_username: "Utilisateur de la démo"
share_public: Autoriser une URL publique pour les articles

View file

@ -27,3 +27,4 @@ piwik_site_id: ID del tuo sito in Piwik
piwik_enabled: Abilita Piwik
demo_mode_enabled: "Abilita modalità demo ? (usato solo per la demo pubblica di wallabag)"
demo_mode_username: "Utente Demo"
# share_public: Allow public url for entries

View file

@ -27,3 +27,4 @@ piwik_site_id: ID de vòstre site dins Piwik
piwik_enabled: Activar Piwik
demo_mode_enabled: "Activar lo mode demostracion ? (utilizar solament per la demostracion publica de wallabag)"
demo_mode_username: "Utilizaire de la demostracion"
# share_public: Allow public url for entries

View file

@ -22,3 +22,9 @@ export: "eksport"
import: "import"
misc: "różne"
modify_settings: "zatwierdz"
# piwik_host: Host of your website in Piwik
# piwik_site_id: ID of your website in Piwik
# piwik_enabled: Enable Piwik
# demo_mode_enabled: "Enable demo mode ? (only used for the wallabag public demo)"
# demo_mode_username: "Demo user"
# share_public: Allow public url for entries

View file

@ -22,3 +22,9 @@ export: "exportă"
import: "importă"
misc: "diverse"
modify_settings: "aplică"
# piwik_host: Host of your website in Piwik
# piwik_site_id: ID of your website in Piwik
# piwik_enabled: Enable Piwik
# demo_mode_enabled: "Enable demo mode ? (only used for the wallabag public demo)"
# demo_mode_username: "Demo user"
# share_public: Allow public url for entries

View file

@ -60,6 +60,7 @@ security:
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: /(unread|starred|archive).xml$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/share, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/settings, roles: ROLE_SUPER_ADMIN }
- { path: ^/annotations, roles: ROLE_USER }
- { path: ^/, roles: ROLE_USER }

View file

@ -241,6 +241,11 @@ class InstallCommand extends ContainerAwareCommand
$em->createQuery('DELETE FROM CraueConfigBundle:Setting')->execute();
$settings = [
[
'name' => 'share_public',
'value' => '1',
'section' => 'entry',
],
[
'name' => 'carrot',
'value' => '1',

View file

@ -12,6 +12,7 @@ use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Form\Type\EntryFilterType;
use Wallabag\CoreBundle\Form\Type\EditEntryType;
use Wallabag\CoreBundle\Form\Type\NewEntryType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
class EntryController extends Controller
{
@ -434,7 +435,7 @@ class EntryController extends Controller
*/
private function checkUserAction(Entry $entry)
{
if ($this->getUser()->getId() != $entry->getUser()->getId()) {
if (null === $this->getUser() || $this->getUser()->getId() != $entry->getUser()->getId()) {
throw $this->createAccessDeniedException('You can not access this entry.');
}
}
@ -450,4 +451,76 @@ class EntryController extends Controller
{
return $this->get('wallabag_core.entry_repository')->findByUrlAndUserId($entry->getUrl(), $this->getUser()->getId());
}
/**
* Get public URL for entry (and generate it if necessary).
*
* @param Entry $entry
*
* @Route("/share/{id}", requirements={"id" = "\d+"}, name="share")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function shareAction(Entry $entry)
{
$this->checkUserAction($entry);
if (null === $entry->getUuid()) {
$entry->generateUuid();
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
}
return $this->redirect($this->generateUrl('share_entry', [
'uuid' => $entry->getUuid(),
]));
}
/**
* Disable public sharing for an entry.
*
* @param Entry $entry
*
* @Route("/share/delete/{id}", requirements={"id" = "\d+"}, name="delete_share")
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function deleteShareAction(Entry $entry)
{
$this->checkUserAction($entry);
$entry->cleanUuid();
$em = $this->getDoctrine()->getManager();
$em->persist($entry);
$em->flush();
return $this->redirect($this->generateUrl('view', [
'id' => $entry->getId(),
]));
}
/**
* Ability to view a content publicly.
*
* @param Entry $entry
*
* @Route("/share/{uuid}", requirements={"uuid" = ".+"}, name="share_entry")
* @Cache(maxage="25200", smaxage="25200", public=true)
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function shareEntryAction(Entry $entry)
{
if (!$this->get('craue_config')->get('share_public')) {
throw $this->createAccessDeniedException('Sharing an entry is disabled for this user.');
}
return $this->render(
'@WallabagCore/themes/share.html.twig',
['entry' => $entry]
);
}
}

View file

@ -15,6 +15,11 @@ class LoadSettingData extends AbstractFixture implements OrderedFixtureInterface
public function load(ObjectManager $manager)
{
$settings = [
[
'name' => 'share_public',
'value' => '1',
'section' => 'entry',
],
[
'name' => 'carrot',
'value' => '1',

View file

@ -37,6 +37,15 @@ class Entry
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="uuid", type="text", nullable=true)
*
* @Groups({"entries_for_user", "export_all"})
*/
private $uuid;
/**
* @var string
*
@ -595,4 +604,37 @@ class Entry
{
return $this->language;
}
/**
* @return string
*/
public function getUuid()
{
return $this->uuid;
}
/**
* @param string $uuid
*
* @return Entry
*/
public function setUuid($uuid)
{
$this->uuid = $uuid;
return $this;
}
public function generateUuid()
{
if (null === $this->uuid) {
// @see http://blog.kevingomez.fr/til/2015/07/26/why-is-uniqid-slow/ for true parameter
$this->uuid = uniqid('', true);
}
}
public function cleanUuid()
{
$this->uuid = null;
}
}

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Tliføj et tag'
share_content: 'Deling'
# share_email_label: 'Email'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Download'
# print: 'Print'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Tag hinzufügen'
share_content: 'Teilen'
share_email_label: 'E-Mail'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Herunterladen'
print: 'Drucken'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Add a tag'
share_content: 'Share'
share_email_label: 'Email'
public_link: 'public link'
delete_public_link: 'delete public link'
download: 'Download'
print: 'Print'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Añadir una etiqueta'
share_content: 'Compartir'
share_email_label: 'Dirección e-mail'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Descargar'
print: 'Imprimir'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'افزودن برچسب'
share_content: 'هم‌رسانی'
share_email_label: 'نشانی ایمیل'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'بارگیری'
print: 'چاپ'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Ajouter un tag'
share_content: 'Partager'
share_email_label: 'Email'
public_link: 'Lien public'
delete_public_link: 'Supprimer lien public'
download: 'Télécharger'
print: 'Imprimer'
problem:

View file

@ -185,6 +185,8 @@ entry:
add_a_tag: 'Aggiungi un tag'
share_content: 'Condividi'
share_email_label: 'E-mail'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Download'
print: 'Stampa'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Ajustar una etiqueta'
share_content: 'Partatjar'
share_email_label: 'Corrièl'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Telecargar'
print: 'Imprimir'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Dodaj tag'
share_content: 'Udostępnij'
share_email_label: 'Adres email'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Pobierz'
print: 'Drukuj'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Adaugă un tag'
share_content: 'Dă mai departe'
share_email_label: 'E-mail'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'Descarcă'
# print: 'Print'
problem:

View file

@ -187,6 +187,8 @@ entry:
add_a_tag: 'Bir etiket ekle'
share_content: 'Paylaş'
share_email_label: 'E-posta'
# public_link: 'public link'
# delete_public_link: 'delete public link'
download: 'İndir'
# print: 'Print'
problem:

View file

@ -17,6 +17,7 @@
<li><a title="{{ 'entry.view.left_menu.set_as_starred'|trans }}" class="tool icon icon-star {% if entry.isStarred == 0 %}fav-off{% else %}fav{% endif %}" href="{{ path('star_entry', { 'id': entry.id }) }}"><span>{{ 'entry.view.left_menu.set_as_starred'|trans }}</span></a></li>
<li><a id="nav-btn-add-tag" title="{{ 'entry.view.left_menu.add_a_tag'|trans }}"><span>{{ 'entry.view.left_menu.add_a_tag'|trans }}</span></a></li>
<li><a title="{{ 'entry.view.left_menu.delete'|trans }}" class="tool delete icon icon-trash" href="{{ path('delete_entry', { 'id': entry.id }) }}"><span>{{ 'entry.view.left_menu.delete'|trans }}</span></a></li>
{% if craue_setting('share_public') %}<li><a href="{{ path('share', {'id': entry.id }) }}" target="_blank" class="tool public" title="{{ 'entry.view.left_menu.public_link'|trans }}"><span>{{ 'entry.view.left_menu.public_link'|trans }}</span></a></li> <li><a href="{{ path('delete_share', {'id': entry.id }) }}" class="tool public" title="{{ 'entry.view.left_menu.delete_public_link'|trans }}"><span>{{ 'entry.view.left_menu.delete_public_link'|trans }}</span></a></li>{% endif %}
{% if craue_setting('share_twitter') %}<li><a href="https://twitter.com/home?status={{entry.title|url_encode}}%20{{ entry.url|url_encode }}%20via%20@wallabagapp" target="_blank" class="tool twitter icon icon-twitter" title="Tweet"><span>Tweet</span></a></li>{% endif %}
{% if craue_setting('share_mail') %}<li><a href="mailto:?subject={{ entry.title|url_encode }}&amp;body={{ entry.url|url_encode }}%20via%20@wallabagapp" class="tool email icon icon-mail" title="Email"><span>Email</span></a></li>{% endif %}
{% if craue_setting('share_shaarli') %}<li><a href="{{ craue_setting('shaarli_url') }}/index.php?post={{ entry.url|url_encode }}&amp;title={{ entry.title|url_encode }}" target="_blank" class="tool shaarli" title="shaarli"><span>shaarli</span></a></li>{% endif %}

View file

@ -99,6 +99,18 @@
</a>
<div class="collapsible-body">
<ul>
{% if craue_setting('share_public') %}
<li>
<a href="{{ path('share', {'id': entry.id }) }}" target="_blank" class="tool public" title="{{ 'entry.view.left_menu.public_link'|trans }}">
<span>{{ 'entry.view.left_menu.public_link'|trans }}</span>
</a>
</li>
<li>
<a href="{{ path('delete_share', {'id': entry.id }) }}" class="tool public" title="{{ 'entry.view.left_menu.delete_public_link'|trans }}">
<span>{{ 'entry.view.left_menu.delete_public_link'|trans }}</span>
</a>
</li>
{% endif %}
{% if craue_setting('share_twitter') %}
<li>
<a href="https://twitter.com/home?status={{entry.title|url_encode}}%20{{ entry.url|url_encode }}%20via%20@wallabagapp" target="_blank" class="tool twitter icon icon-twitter" title="twitter">
@ -149,7 +161,6 @@
</li>
{% endif %}
<li class="bold">
<a class="waves-effect collapsible-header">
<i class="material-icons small">file_download</i>

View file

@ -0,0 +1,39 @@
<html>
<head>
<title>{{ entry.title | raw }}</title>
<style>
body {
margin: 10px;
font-family: 'Roboto',Verdana,Geneva,sans-serif;
font-size: 16px;
color: #000;
}
header {
text-align: center;
}
header h1 {
font-size: 1.3em;
}
a,
a:hover,
a:visited {
color: #000;
}
article {
margin: 0 auto;
width: 600px;
}
</style>
</head>
<body>
<header>
<h1>{{ entry.title | raw }}</h1>
</header>
<article>
{{ entry.content | raw }}
</article>
</body>
</html>

View file

@ -698,4 +698,47 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data);
$this->assertCount(2, $crawler->filter('div[class=entry]'));
}
public function testCache()
{
$this->logInAs('admin');
$client = $this->getClient();
$content = $client->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findOneByUser($this->getLoggedInUserId());
// no uuid
$client->request('GET', '/share/'.$content->getUuid());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
// generating the uuid
$client->request('GET', '/share/'.$content->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
// follow link with uuid
$crawler = $client->followRedirect();
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertContains('max-age=25200', $client->getResponse()->headers->get('cache-control'));
$this->assertContains('public', $client->getResponse()->headers->get('cache-control'));
$this->assertContains('s-maxage=25200', $client->getResponse()->headers->get('cache-control'));
$this->assertNotContains('no-cache', $client->getResponse()->headers->get('cache-control'));
// sharing is now disabled
$client->getContainer()->get('craue_config')->set('share_public', 0);
$client->request('GET', '/share/'.$content->getUuid());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
$client->request('GET', '/view/'.$content->getId());
$this->assertContains('no-cache', $client->getResponse()->headers->get('cache-control'));
// removing the share
$client->request('GET', '/share/delete/'.$content->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode());
// share is now disable
$client->request('GET', '/share/'.$content->getUuid());
$this->assertEquals(404, $client->getResponse()->getStatusCode());
}
}

View file

@ -681,10 +681,17 @@ class SymfonyRequirements extends RequirementCollection
if (class_exists('Symfony\Component\Intl\Intl')) {
$this->addRecommendation(
\Symfony\Component\Intl\Intl::getIcuDataVersion() === \Symfony\Component\Intl\Intl::getIcuVersion(),
sprintf('intl ICU version installed on your system (%s) should match the ICU data bundled with Symfony (%s)', \Symfony\Component\Intl\Intl::getIcuVersion(), \Symfony\Component\Intl\Intl::getIcuDataVersion()),
'In most cases you should be fine, but please verify there is no inconsistencies between data provided by Symfony and the intl extension. See https://github.com/symfony/symfony/issues/15007 for an example of inconsistencies you might run into.'
\Symfony\Component\Intl\Intl::getIcuDataVersion() <= \Symfony\Component\Intl\Intl::getIcuVersion(),
sprintf('intl ICU version installed on your system is outdated (%s) and does not match the ICU data bundled with Symfony (%s)', \Symfony\Component\Intl\Intl::getIcuVersion(), \Symfony\Component\Intl\Intl::getIcuDataVersion()),
'To get the latest internationalization data upgrade the ICU system package and the intl PHP extension.'
);
if (\Symfony\Component\Intl\Intl::getIcuDataVersion() <= \Symfony\Component\Intl\Intl::getIcuVersion()) {
$this->addRecommendation(
\Symfony\Component\Intl\Intl::getIcuDataVersion() === \Symfony\Component\Intl\Intl::getIcuVersion(),
sprintf('intl ICU version installed on your system (%s) does not match the ICU data bundled with Symfony (%s)', \Symfony\Component\Intl\Intl::getIcuVersion(), \Symfony\Component\Intl\Intl::getIcuDataVersion()),
'To avoid internationalization data incosistencies upgrade the symfony/intl component.'
);
}
}
$this->addPhpIniRecommendation(