Skip to content

Instantly share code, notes, and snippets.

@bl00m
Last active May 17, 2019 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bl00m/73477fe11c433803201fb5c474186711 to your computer and use it in GitHub Desktop.
Save bl00m/73477fe11c433803201fb5c474186711 to your computer and use it in GitHub Desktop.
Patch wallabag to share credentials between users
This patch allow wallabag users to share credentials.
One user can add creds that only him can manage but that all users can use to unlock articles.
diff --git a/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php b/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php
index 548de74..eb4735c 100644
--- a/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php
+++ b/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php
@@ -26,10 +26,21 @@ class SiteCredentialController extends Controller
{
$this->isSiteCredentialsEnabled();
- $credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser());
+ $credentials = $this->get('wallabag_core.site_credential_repository')->findAll();
+
+ $my_credentials = array();
+ $other_available_credentials = array();
+ foreach($credentials as $creds) {
+ if ($this->getUser()->getId() == $creds->getUser()->getId()) {
+ array_push($my_credentials, $creds);
+ } else {
+ array_push($other_available_credentials, $creds);
+ }
+ }
return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', [
- 'credentials' => $credentials,
+ 'my_credentials' => $my_credentials,
+ 'other_available_credentials' => $other_available_credentials,
]);
}
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
index c7502ba..0a46161 100644
--- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
+++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
@@ -78,7 +78,7 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
$hosts[] = '.' . implode('.', $split);
}
- $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId());
+ $credentials = $this->credentialRepository->findOneByHost($host);
if (null === $credentials) {
$this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
diff --git a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
index aeadd77..92c40a8 100644
--- a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
+++ b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
@@ -44,4 +44,31 @@ class SiteCredentialRepository extends \Doctrine\ORM\EntityRepository
return $res;
}
+
+ /**
+ * Retrieve one username/password for the given host.
+ *
+ * @param string $host
+ *
+ * @return null|array
+ */
+ public function findOneByHost($host)
+ {
+ $res = $this->createQueryBuilder('s')
+ ->select('s.username', 's.password')
+ ->where('s.host = :hostname')->setParameter('hostname', $host)
+ ->setMaxResults(1)
+ ->getQuery()
+ ->getOneOrNullResult();
+
+ if (null === $res) {
+ return;
+ }
+
+ // decrypt user & password before returning them
+ $res['username'] = $this->cryptoProxy->decrypt($res['username']);
+ $res['password'] = $this->cryptoProxy->decrypt($res['password']);
+
+ return $res;
+ }
}
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml
index 598ad58..38854b8 100644
--- a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml
+++ b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml
@@ -548,6 +548,8 @@ site_credential:
yes: Yes
no: No
create_new_one: Create a new credential
+ my_credentials: 'My credentials'
+ other_available_credentials: 'Other available credentials'
form:
username_label: 'Username'
host_label: 'Host (subdomain.example.org, .example.org, etc.)'
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
index b2fa1c5..f8e86f5 100644
--- a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
+++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
@@ -548,6 +548,8 @@ site_credential:
yes: Oui
no: Non
create_new_one: Créer un nouvel accès à un site
+ my_credentials: 'Mes accès'
+ other_available_credentials: 'Autres accès disponibles'
form:
username_label: 'Identifiant'
host_label: 'Domaine (subdomain.example.org, .example.org, etc.)'
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/SiteCredential/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/SiteCredential/index.html.twig
index 324854a..8ac38a2 100644
--- a/src/Wallabag/CoreBundle/Resources/views/themes/baggy/SiteCredential/index.html.twig
+++ b/src/Wallabag/CoreBundle/Resources/views/themes/baggy/SiteCredential/index.html.twig
@@ -11,6 +11,7 @@
<div class="input-field col s12">
<p class="help">{{ 'site_credential.description'|trans|raw }}</p>
+ <h3>{{ 'site_credential.list.my_credentials'|trans }}</h3>
<table class="bordered">
<thead>
<tr>
@@ -19,7 +20,7 @@
</tr>
</thead>
<tbody>
- {% for credential in credentials %}
+ {% for credential in my_credentials %}
<tr>
<td>{{ credential.host }}</td>
<td>
@@ -33,6 +34,25 @@
<p>
<a href="{{ path('site_credentials_new') }}" class="waves-effect waves-light btn">{{ 'site_credential.list.create_new_one'|trans }}</a>
</p>
+ <br />
+
+ <h3>{{ 'site_credential.list.other_available_credentials'|trans }}</h3>
+ <table class="bordered">
+ <thead>
+ <tr>
+ <th>{{ 'site_credential.form.host_label'|trans }}</th>
+ <th>{{ 'site_credential.list.actions'|trans }}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for credential in other_available_credentials %}
+ <tr>
+ <td>{{ credential.host }}</td>
+ <td></td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
</div>
</div>
</div>
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/SiteCredential/index.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/SiteCredential/index.html.twig
index 324854a..8ac38a2 100644
--- a/src/Wallabag/CoreBundle/Resources/views/themes/material/SiteCredential/index.html.twig
+++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/SiteCredential/index.html.twig
@@ -11,6 +11,7 @@
<div class="input-field col s12">
<p class="help">{{ 'site_credential.description'|trans|raw }}</p>
+ <h3>{{ 'site_credential.list.my_credentials'|trans }}</h3>
<table class="bordered">
<thead>
<tr>
@@ -19,7 +20,7 @@
</tr>
</thead>
<tbody>
- {% for credential in credentials %}
+ {% for credential in my_credentials %}
<tr>
<td>{{ credential.host }}</td>
<td>
@@ -33,6 +34,25 @@
<p>
<a href="{{ path('site_credentials_new') }}" class="waves-effect waves-light btn">{{ 'site_credential.list.create_new_one'|trans }}</a>
</p>
+ <br />
+
+ <h3>{{ 'site_credential.list.other_available_credentials'|trans }}</h3>
+ <table class="bordered">
+ <thead>
+ <tr>
+ <th>{{ 'site_credential.form.host_label'|trans }}</th>
+ <th>{{ 'site_credential.list.actions'|trans }}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for credential in other_available_credentials %}
+ <tr>
+ <td>{{ credential.host }}</td>
+ <td></td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment