Make snip public index available if not logged in

This commit is contained in:
Tim 2025-04-14 23:48:32 +02:00
parent 22c8126cea
commit 5cec259469
5 changed files with 35 additions and 16 deletions

View File

@ -43,7 +43,10 @@ security:
- { path: ^/logout$, role: ROLE_USER }
- { path: ^/admin, role: ROLE_ADMIN }
- { path: ^/$, role: PUBLIC_ACCESS }
- { path: ^/snip/single, role: PUBLIC_ACCESS }
- { path: ^/snip/raw, role: PUBLIC_ACCESS }
- { path: ^/snip/public$, role: PUBLIC_ACCESS }
- { path: ^/, role: ROLE_USER }

View File

@ -11,6 +11,10 @@ class HomeController extends AbstractController
#[Route('/', name: 'home')]
public function home(): Response
{
return $this->redirectToRoute('snip_index');
if ($this->getUser()) {
return $this->redirectToRoute('snip_index');
} else {
return $this->redirectToRoute('snip_public');
}
}
}

View File

@ -44,20 +44,26 @@ class SnipRepository extends ServiceEntityRepository
{
$qb = $this->createQueryBuilder('s');
$qb->where('s.createdBy = :user')
->setParameter('user', $user)
->orderBy('s.createdAt', 'DESC');
->setParameter('user', $user)
->orderBy('s.createdAt', 'DESC')
;
return $qb->getQuery()->getResult();
}
public function findPublic(User $user): array
public function findPublic(?User $user): array
{
$qb = $this->createQueryBuilder('s');
$qb->where('s.public = :public')
->andWhere('s.createdBy != :user')
->setParameter('public', true)
->setParameter('user', $user)
->orderBy('s.createdAt', 'DESC');
$qb = $this->createQueryBuilder('s')
->where('s.public = :public')
->setParameter('public', true)
->orderBy('s.createdAt', 'DESC')
;
if ($user) {
$qb->andWhere('s.createdBy != :user')
->setParameter('user', $user)
;
}
return $qb->getQuery()->getResult();
}

View File

@ -15,10 +15,10 @@
<li class="nav-item">
<a class="nav-link" href="{{ path('snip_new') }}">New snip</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ path('snip_public') }}">Public snips</a>
</li>
{% endif %}
<li class="nav-item">
<a class="nav-link" href="{{ path('snip_public') }}">Public snips</a>
</li>
</ul>
<ul class="navbar-nav my-2 my-lg-0">
{% if app.environment == 'dev' %}

View File

@ -3,9 +3,15 @@
{% set title %}Snip {{ snip }}{% endset %}
{% block body %}
<a href="{{ path('snip_index') }}" class="btn btn-primary">
<i class="fa fa-arrow-left"></i> Back
</a>
{% if app.user %}
<a href="{{ path('snip_index') }}" class="btn btn-primary">
<i class="fa fa-arrow-left"></i> Back
</a>
{% else %}
<a href="{{ path('snip_public') }}" class="btn btn-primary">
<i class="fa fa-arrow-left"></i> Index
</a>
{% endif %}
{% if is_granted('edit', snip) %}
<a class="btn btn-warning" href="{{ path('snip_edit', {snip: snip.id}) }}">
<i class="fa fa-pencil" aria-hidden="true"></i> Edit