Part-DB.Part-DB-server/src/Controller/UserController.php

234 lines
8.3 KiB
PHP
Raw Normal View History

2019-03-14 19:10:11 +01:00
<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
2019-03-14 19:10:11 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
2019-03-14 19:10:11 +01:00
*
2019-11-01 13:40:30 +01:00
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
2019-03-14 19:10:11 +01:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
namespace App\Controller;
use App\Entity\Attachments\UserAttachment;
use App\Entity\UserSystem\User;
use App\Events\SecurityEvent;
use App\Events\SecurityEvents;
use App\Form\Permissions\PermissionsType;
2019-04-28 14:18:11 +02:00
use App\Form\UserAdminForm;
use App\Services\EntityExporter;
use App\Services\EntityImporter;
use App\Services\StructuralElementRecursionHelper;
use Doctrine\ORM\EntityManagerInterface;
2020-01-05 22:49:00 +01:00
use InvalidArgumentException;
2019-03-14 19:10:11 +01:00
use Symfony\Component\Asset\Packages;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
2019-04-28 14:18:11 +02:00
use Symfony\Component\HttpFoundation\Response;
2019-03-14 19:10:11 +01:00
use Symfony\Component\Routing\Annotation\Route;
2019-04-28 14:18:11 +02:00
/**
* @Route("/user")
* Class UserController
*/
class UserController extends AdminPages\BaseAdminController
2019-03-14 19:10:11 +01:00
{
2019-04-28 14:18:11 +02:00
protected $entity_class = User::class;
protected $twig_template = 'AdminPages/UserAdmin.html.twig';
protected $form_class = UserAdminForm::class;
2019-08-20 18:39:57 +02:00
protected $route_base = 'user';
protected $attachment_class = UserAttachment::class;
//Just define a value here to prevent error. It is not used.
2020-03-29 22:16:06 +02:00
protected $parameter_class = 'not used';
2019-04-28 14:18:11 +02:00
/**
* @Route("/{id}/edit/{timestamp}", requirements={"id"="\d+"}, name="user_edit")
2019-04-28 14:18:11 +02:00
* @Route("/{id}/", requirements={"id"="\d+"})
2020-03-15 13:56:31 +01:00
*
2020-02-02 14:05:36 +01:00
* @return Response
2020-03-15 13:56:31 +01:00
*
2020-02-02 14:05:36 +01:00
* @throws \Exception
2019-04-28 14:18:11 +02:00
*/
public function edit(User $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null)
2019-04-28 14:18:11 +02:00
{
//Handle 2FA disabling
2020-01-04 20:24:09 +01:00
if ($request->request->has('reset_2fa')) {
//Check if the admin has the needed permissions
$this->denyAccessUnlessGranted('set_password', $entity);
if ($this->isCsrfTokenValid('reset_2fa'.$entity->getId(), $request->request->get('_token'))) {
//Disable Google authenticator
$entity->setGoogleAuthenticatorSecret(null);
$entity->setBackupCodes([]);
//Remove all U2F keys
2020-01-04 20:24:09 +01:00
foreach ($entity->getU2FKeys() as $key) {
$em->remove($key);
}
//Invalidate trusted devices
$entity->invalidateTrustedDeviceTokens();
$em->flush();
$event = new SecurityEvent($entity);
$this->eventDispatcher->dispatch($event, SecurityEvents::TFA_ADMIN_RESET);
$this->addFlash('success', 'user.edit.reset_success');
} else {
$this->addFlash('danger', 'csfr_invalid');
}
}
return $this->_edit($entity, $request, $em, $timestamp);
2019-04-28 14:18:11 +02:00
}
/**
* @Route("/new", name="user_new")
* @Route("/")
*
2019-08-20 18:39:57 +02:00
* @return Response
2019-04-28 14:18:11 +02:00
*/
2020-02-02 14:05:36 +01:00
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer): Response
2019-04-28 14:18:11 +02:00
{
return $this->_new($request, $em, $importer);
}
/**
* @Route("/{id}", name="user_delete", methods={"DELETE"}, requirements={"id"="\d+"})
2020-03-15 13:56:31 +01:00
*
2020-02-02 14:05:36 +01:00
* @return \Symfony\Component\HttpFoundation\RedirectResponse
2019-04-28 14:18:11 +02:00
*/
public function delete(Request $request, User $entity, StructuralElementRecursionHelper $recursionHelper)
{
2019-11-10 14:00:56 +01:00
if (User::ID_ANONYMOUS === $entity->getID()) {
2020-01-05 22:49:00 +01:00
throw new InvalidArgumentException('You can not delete the anonymous user! It is needed for permission checking without a logged in user');
2019-09-19 13:49:10 +02:00
}
2019-04-28 14:18:11 +02:00
return $this->_delete($request, $entity, $recursionHelper);
}
/**
* @Route("/export", name="user_export_all")
*
2019-04-28 14:18:11 +02:00
* @return Response
*/
2020-02-02 14:05:36 +01:00
public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request): Response
2019-04-28 14:18:11 +02:00
{
return $this->_exportAll($em, $exporter, $request);
}
/**
* @Route("/{id}/export", name="user_export")
*
2019-08-20 18:39:57 +02:00
* @return Response
2019-04-28 14:18:11 +02:00
*/
2020-02-02 14:05:36 +01:00
public function exportEntity(User $entity, EntityExporter $exporter, Request $request): Response
2019-04-28 14:18:11 +02:00
{
return $this->_exportEntity($entity, $exporter, $request);
}
2019-03-14 19:10:11 +01:00
/**
2019-04-28 14:18:11 +02:00
* @Route("/info", name="user_info_self")
* @Route("/{id}/info", name="user_info")
2020-03-15 13:56:31 +01:00
*
2020-02-02 14:05:36 +01:00
* @return Response
2019-03-14 19:10:11 +01:00
*/
2020-02-02 14:05:36 +01:00
public function userInfo(?User $user, Packages $packages): Response
2019-03-14 19:10:11 +01:00
{
//If no user id was passed, then we show info about the current user
if (null === $user) {
2020-02-02 14:05:36 +01:00
$tmp = $this->getUser();
2020-03-15 13:56:31 +01:00
if (! $tmp instanceof User) {
2020-02-02 14:05:36 +01:00
throw new InvalidArgumentException('Userinfo only works for database users!');
}
$user = $tmp;
} else {
//Else we must check, if the current user is allowed to access $user
$this->denyAccessUnlessGranted('read', $user);
2019-03-14 19:10:11 +01:00
}
if ($this->getParameter('use_gravatar')) {
2019-03-14 19:10:11 +01:00
$avatar = $this->getGravatar($user->getEmail(), 200, 'identicon');
} else {
2019-03-20 22:53:06 +01:00
$avatar = $packages->getUrl('/img/default_avatar.png');
2019-03-14 19:10:11 +01:00
}
//Show permissions to user
$builder = $this->createFormBuilder()->add('permissions', PermissionsType::class, [
'mapped' => false,
'disabled' => true,
'inherit' => true,
'data' => $user,
]);
2019-03-14 19:10:11 +01:00
return $this->render('Users/user_info.html.twig', [
2019-04-28 14:18:11 +02:00
'user' => $user,
'avatar' => $avatar,
'form' => $builder->getForm()->createView(),
2019-04-28 14:18:11 +02:00
]);
2019-03-14 19:10:11 +01:00
}
/**
* Get either a Gravatar URL or complete image tag for a specified email address.
*
* @param string $email The email address
2020-02-01 17:00:03 +01:00
* @param int $s Size in pixels, defaults to 80px [ 1 - 2048 ]
* @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
* @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
* @param bool $img True to return a complete IMG tag False for just the URL
* @param array $atts Optional, additional key/value attributes to include in the IMG tag
*
* @return string containing either just a URL or a complete image tag
2019-03-14 19:10:11 +01:00
* @source https://gravatar.com/site/implement/images/php/
*/
2020-02-02 14:05:36 +01:00
public function getGravatar(?string $email, int $s = 80, string $d = 'mm', string $r = 'g', bool $img = false, array $atts = []): string
2019-03-14 19:10:11 +01:00
{
if (null === $email) {
return '';
}
2019-03-14 19:10:11 +01:00
$url = 'https://www.gravatar.com/avatar/';
$url .= md5(strtolower(trim($email)));
2020-01-05 15:46:58 +01:00
$url .= "?s=${s}&d=${d}&r=${r}";
2019-03-14 19:10:11 +01:00
if ($img) {
$url = '<img src="'.$url.'"';
2019-03-14 19:10:11 +01:00
foreach ($atts as $key => $val) {
$url .= ' '.$key.'="'.$val.'"';
2019-03-14 19:10:11 +01:00
}
$url .= ' />';
}
2019-03-14 19:10:11 +01:00
return $url;
}
}