basic user list for admins

This commit is contained in:
overflowerror 2021-01-07 23:46:32 +01:00
parent 417e60b296
commit afec700b7e
8 changed files with 136 additions and 13 deletions

10
public/css/admin.css Normal file
View file

@ -0,0 +1,10 @@
.customChip {
display: inline-block;
background-color: darkred;
font-size: 1vw;
padding: 0 5% 0 5%;
height: 1.5vw;
border-radius: 0.75vw;
font-weight: bold;
color: lightgrey;
}

View file

@ -0,0 +1,39 @@
<?php
namespace App\Controller;
use App\Service\UserService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
class UserController extends AbstractController
{
private $userService;
public function __construct(UserService $userService)
{
$this->userService = $userService;
}
/**
* @Route("/admin/users", name="app_user_list")
*/
public function userList(): Response
{
if (!$this->isGranted("ROLE_ADMIN")) {
// not logged in
throw new AccessDeniedHttpException();
}
$users = $this->userService->getUsers();
return $this->render("user/users.html.twig", [
"users" => $users
]);
}
}

View file

@ -24,6 +24,7 @@ class UserFixtures extends Fixture
$admin = new User();
$admin->setName("admin");
$admin->setPassword($this->passwordEncoder->encodePassword($admin, "password"));
$admin->setRoles(["ROLE_ADMIN"]);
$manager->persist($admin);
$manager->flush();

View file

@ -156,4 +156,16 @@ class User implements UserInterface
return $this;
}
public function getReadableRoles(): array
{
return array_map(function ($role) {
$i = strrpos($role, "_");
if ($i) {
// $i === false it's probably a custom name
return substr($role, $i + 1);
}
return $role;
}, $this->getRoles());
}
}

View file

@ -31,4 +31,9 @@ class UserService
return $user;
}
public function getUsers(): array
{
return $this->userRepository->findAll();
}
}

View file

@ -33,18 +33,29 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link {% if route_name == "app_dashboard" %} active {% endif %}"
aria-current="page" href="{{ path("app_dashboard") }}">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link {% if route_name == "app_upload" %} active {% endif %}" aria-current="page"
href="{{ path("app_upload") }}">Upload</a>
</li>
<li class="nav-item">
<a class="nav-link {% if route_name == "app_links" %} active {% endif %}" aria-current="page"
href="{{ path("app_links") }}">Links</a>
</li>
{% if is_granted('ROLE_USER') %}
<li class="nav-item">
<a class="nav-link {% if route_name == "app_dashboard" %} active {% endif %}"
aria-current="page" href="{{ path("app_dashboard") }}">Dashboard</a>
</li>
<li class="nav-item">
<a class="nav-link {% if route_name == "app_upload" %} active {% endif %}"
aria-current="page"
href="{{ path("app_upload") }}">Upload</a>
</li>
<li class="nav-item">
<a class="nav-link {% if route_name == "app_links" %} active {% endif %}"
aria-current="page"
href="{{ path("app_links") }}">Links</a>
</li>
{% endif %}
{% if is_granted('ROLE_ADMIN') %}
<li class="nav-item">
<a class="nav-link {% if route_name == "app_user_list" %} active {% endif %}"
aria-current="page"
href="{{ path("app_user_list") }}">Users</a>
</li>
{% endif %}
</ul>
<form class="d-flex input-group w-auto">
<input

View file

@ -79,7 +79,7 @@
</td>
<td>
{% if link.viewableFor %}
{{ link.viewableForLeft != null ? link.viewableForLeft : "-" }} / {{ link.viewableFor }}
{{ link.viewableForLeft != null ? link.viewableForLeft : "-" }} / {{ link.viewableFor }} h
{% else %}
-
{% endif %}

View file

@ -0,0 +1,45 @@
{% extends 'base.html.twig' %}
{% block title %}Links{% endblock %}
{% block stylesheets %}
<link rel="stylesheet" href="{{ asset("css/admin.css") }}">
{% endblock %}
{% block body %}
<div class="links bg-light shadow-5">
<table class="table">
<tr>
<th>
<input type="checkbox">
</th>
<th>
Username
</th>
<th>
Email
</th>
<th>
Roles
</th>
</tr>
{% for user in users %}
<tr>
<td>
<input type="checkbox">
</td>
<td>
{{ user.name }}
</td>
<td>
[not yet implemented]
</td>
<td>
{% for role in user.getReadableRoles() %}
<div class="customChip">{{ role }}</div>
{% endfor %}
</td>
</tr>
{% endfor %}
</table>
</div>
{% endblock %}