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
|
|
|
/**
|
2019-11-09 00:47:20 +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;
|
|
|
|
|
2019-09-24 18:28:35 +02:00
|
|
|
use App\Entity\Attachments\UserAttachment;
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\UserSystem\User;
|
2019-09-13 19:38:22 +02:00
|
|
|
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;
|
2019-03-15 18:04:15 +01:00
|
|
|
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;
|
2019-03-15 18:04:15 +01:00
|
|
|
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';
|
2019-09-24 18:28:35 +02:00
|
|
|
protected $attachment_class = UserAttachment::class;
|
2020-03-24 21:49:09 +01:00
|
|
|
//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
|
|
|
|
|
|
|
/**
|
2020-03-04 21:54:03 +01: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
|
|
|
*/
|
2020-03-04 21:54:03 +01:00
|
|
|
public function edit(User $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null)
|
2019-04-28 14:18:11 +02:00
|
|
|
{
|
2019-12-29 20:04:52 +01:00
|
|
|
//Handle 2FA disabling
|
|
|
|
|
2020-01-04 20:24:09 +01:00
|
|
|
if ($request->request->has('reset_2fa')) {
|
2019-12-29 20:04:52 +01:00
|
|
|
//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) {
|
2019-12-29 20:04:52 +01:00
|
|
|
$em->remove($key);
|
|
|
|
}
|
|
|
|
//Invalidate trusted devices
|
|
|
|
$entity->invalidateTrustedDeviceTokens();
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
$this->addFlash('success', 'user.edit.reset_success');
|
|
|
|
} else {
|
|
|
|
$this->addFlash('danger', 'csfr_invalid');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 21:54:03 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-12-29 17:36:41 +01:00
|
|
|
* @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-11-09 00:47:20 +01:00
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
return $this->_delete($request, $entity, $recursionHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/export", name="user_export_all")
|
2019-11-09 00:47:20 +01:00
|
|
|
*
|
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-11-09 00:47:20 +01:00
|
|
|
*
|
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")
|
2020-01-25 20:28:00 +01:00
|
|
|
* @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
|
2019-03-20 23:16:07 +01:00
|
|
|
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;
|
2019-03-19 18:36:05 +01:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2019-03-20 23:16:07 +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
|
|
|
}
|
|
|
|
|
2019-09-13 19:38:22 +02:00
|
|
|
//Show permissions to user
|
2019-11-09 00:47:20 +01:00
|
|
|
$builder = $this->createFormBuilder()->add('permissions', PermissionsType::class, [
|
2019-09-13 19:38:22 +02:00
|
|
|
'mapped' => false,
|
|
|
|
'disabled' => true,
|
|
|
|
'inherit' => true,
|
2019-11-09 00:47:20 +01:00
|
|
|
'data' => $user,
|
2019-09-13 19:38:22 +02:00
|
|
|
]);
|
|
|
|
|
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,
|
2019-11-09 00:47:20 +01:00
|
|
|
'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 ]
|
2019-03-20 23:16:07 +01:00
|
|
|
* @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
|
|
|
{
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null === $email) {
|
|
|
|
return '';
|
2019-11-05 17:09:01 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2019-03-20 23:16:07 +01:00
|
|
|
$url = '<img src="'.$url.'"';
|
2019-03-14 19:10:11 +01:00
|
|
|
foreach ($atts as $key => $val) {
|
2019-03-20 23:16:07 +01:00
|
|
|
$url .= ' '.$key.'="'.$val.'"';
|
2019-03-14 19:10:11 +01:00
|
|
|
}
|
|
|
|
$url .= ' />';
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
|
2019-03-14 19:10:11 +01:00
|
|
|
return $url;
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|