2019-03-14 19:10:11 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2019-11-01 13:40:30 +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
|
2019-11-01 13:40:30 +01:00
|
|
|
*
|
2019-03-14 19:10:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2019-08-12 15:47:57 +02:00
|
|
|
use App\Entity\Attachments\AttachmentType;
|
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;
|
2019-03-15 18:04:15 +01:00
|
|
|
use App\Form\UserSettingsType;
|
2019-04-28 14:18:11 +02:00
|
|
|
use App\Services\EntityExporter;
|
|
|
|
use App\Services\EntityImporter;
|
|
|
|
use App\Services\StructuralElementRecursionHelper;
|
2019-03-15 18:04:15 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-03-14 19:10:11 +01:00
|
|
|
use Symfony\Component\Asset\Packages;
|
2019-03-15 18:38:45 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
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-03-15 18:38:45 +01:00
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
|
|
|
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
|
2019-04-28 14:18:11 +02:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2019-03-15 18:38:45 +01:00
|
|
|
use Symfony\Component\Validator\Constraints\Length;
|
2019-03-14 19:10:11 +01:00
|
|
|
|
2019-04-28 14:18:11 +02:00
|
|
|
/**
|
|
|
|
* @Route("/user")
|
|
|
|
* Class UserController
|
|
|
|
* @package App\Controller
|
|
|
|
*/
|
|
|
|
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;
|
2019-04-28 14:18:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/{id}/edit", requirements={"id"="\d+"}, name="user_edit")
|
|
|
|
* @Route("/{id}/", requirements={"id"="\d+"})
|
|
|
|
*/
|
|
|
|
public function edit(User $entity, Request $request, EntityManagerInterface $em)
|
|
|
|
{
|
|
|
|
return $this->_edit($entity, $request, $em);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/new", name="user_new")
|
|
|
|
* @Route("/")
|
|
|
|
*
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return Response
|
2019-04-28 14:18:11 +02:00
|
|
|
*/
|
|
|
|
public function new(Request $request, EntityManagerInterface $em, EntityImporter $importer)
|
|
|
|
{
|
|
|
|
return $this->_new($request, $em, $importer);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/{id}", name="user_delete", methods={"DELETE"})
|
|
|
|
*/
|
|
|
|
public function delete(Request $request, User $entity, StructuralElementRecursionHelper $recursionHelper)
|
|
|
|
{
|
2019-09-19 13:49:10 +02:00
|
|
|
if ($entity->getID() == User::ID_ANONYMOUS) {
|
|
|
|
throw new \InvalidArgumentException('You can not delete the anonymous user! It is needed for permission checking without a logged in user');
|
|
|
|
}
|
2019-04-28 14:18:11 +02:00
|
|
|
return $this->_delete($request, $entity, $recursionHelper);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/export", name="user_export_all")
|
|
|
|
* @param Request $request
|
|
|
|
* @param SerializerInterface $serializer
|
|
|
|
* @param EntityManagerInterface $em
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function exportAll(EntityManagerInterface $em, EntityExporter $exporter, Request $request)
|
|
|
|
{
|
|
|
|
return $this->_exportAll($em, $exporter, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Route("/{id}/export", name="user_export")
|
|
|
|
* @param Request $request
|
|
|
|
* @param AttachmentType $entity
|
2019-08-20 18:39:57 +02:00
|
|
|
* @return Response
|
2019-04-28 14:18:11 +02:00
|
|
|
*/
|
|
|
|
public function exportEntity(User $entity, EntityExporter $exporter, Request $request)
|
|
|
|
{
|
|
|
|
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")
|
2019-03-14 19:10:11 +01:00
|
|
|
*/
|
|
|
|
public function userInfo(?User $user, Packages $packages)
|
|
|
|
{
|
|
|
|
//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) {
|
2019-03-14 19:10:11 +01:00
|
|
|
$user = $this->getUser();
|
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
|
|
|
|
$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,
|
2019-09-13 19:38:22 +02:00
|
|
|
'form' => $builder->getForm()->createView()
|
2019-04-28 14:18:11 +02:00
|
|
|
]);
|
2019-03-14 19:10:11 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 18:04:15 +01:00
|
|
|
/**
|
2019-04-28 14:18:11 +02:00
|
|
|
* @Route("/settings", name="user_settings")
|
2019-03-15 18:04:15 +01:00
|
|
|
*/
|
2019-03-15 18:38:45 +01:00
|
|
|
public function userSettings(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder)
|
2019-03-15 18:04:15 +01:00
|
|
|
{
|
2019-03-15 18:38:45 +01:00
|
|
|
/**
|
|
|
|
* @var User
|
|
|
|
*/
|
2019-03-15 18:04:15 +01:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
2019-10-13 18:01:13 +02:00
|
|
|
$page_need_reload = false;
|
|
|
|
|
2019-09-19 14:48:49 +02:00
|
|
|
if(!$user instanceof User) {
|
|
|
|
return new \RuntimeException("This controller only works only for Part-DB User objects!");
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:04:15 +01:00
|
|
|
//When user change its settings, he should be logged in fully.
|
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
|
|
|
|
|
2019-03-15 18:38:45 +01:00
|
|
|
/***************************
|
|
|
|
* User settings form
|
|
|
|
***************************/
|
2019-03-15 18:04:15 +01:00
|
|
|
|
|
|
|
$form = $this->createForm(UserSettingsType::class, $user);
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2019-10-13 18:01:13 +02:00
|
|
|
//Check if user theme setting has changed
|
|
|
|
if ($user->getTheme() !== $em->getUnitOfWork()->getOriginalEntityData($user)['theme']) {
|
|
|
|
$page_need_reload = true;
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:04:15 +01:00
|
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', 'user.settings.saved_flash');
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:38:45 +01:00
|
|
|
/*****************************
|
|
|
|
* Password change form
|
|
|
|
****************************/
|
|
|
|
|
2019-10-20 00:01:06 +02:00
|
|
|
$demo_mode = $this->getParameter('demo_mode');
|
|
|
|
|
2019-03-15 18:38:45 +01:00
|
|
|
$pw_form = $this->createFormBuilder()
|
|
|
|
->add('old_password', PasswordType::class, [
|
|
|
|
'label' => 'user.settings.pw_old.label',
|
2019-10-20 00:01:06 +02:00
|
|
|
'disabled' => $demo_mode,
|
2019-03-20 23:16:07 +01:00
|
|
|
'constraints' => [new UserPassword()], ]) //This constraint checks, if the current user pw was inputted.
|
2019-03-15 18:38:45 +01:00
|
|
|
->add('new_password', RepeatedType::class, [
|
2019-10-20 00:01:06 +02:00
|
|
|
'disabled' => $demo_mode,
|
2019-03-15 18:38:45 +01:00
|
|
|
'type' => PasswordType::class,
|
2019-03-20 23:16:07 +01:00
|
|
|
'first_options' => ['label' => 'user.settings.pw_new.label'],
|
|
|
|
'second_options' => ['label' => 'user.settings.pw_confirm.label'],
|
2019-03-15 18:38:45 +01:00
|
|
|
'invalid_message' => 'password_must_match',
|
|
|
|
'constraints' => [new Length([
|
|
|
|
'min' => 6,
|
2019-03-20 23:16:07 +01:00
|
|
|
'max' => 128,
|
|
|
|
])],
|
2019-03-15 18:38:45 +01:00
|
|
|
])
|
2019-03-15 18:59:07 +01:00
|
|
|
->add('submit', SubmitType::class, ['label' => 'save'])
|
2019-03-15 18:38:45 +01:00
|
|
|
->getForm();
|
|
|
|
|
|
|
|
$pw_form->handleRequest($request);
|
|
|
|
|
|
|
|
//Check if password if everything was correct, then save it to User and DB
|
2019-03-20 23:16:07 +01:00
|
|
|
if ($pw_form->isSubmitted() && $pw_form->isValid()) {
|
2019-03-15 18:38:45 +01:00
|
|
|
$password = $passwordEncoder->encodePassword($user, $pw_form['new_password']->getData());
|
|
|
|
$user->setPassword($password);
|
2019-09-19 14:48:49 +02:00
|
|
|
|
|
|
|
//After the change reset the password change needed setting
|
|
|
|
$user->setNeedPwChange(false);
|
|
|
|
|
2019-03-15 18:38:45 +01:00
|
|
|
$em->persist($user);
|
|
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', 'user.settings.pw_changed_flash');
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************
|
|
|
|
* Output both forms
|
|
|
|
*****************************/
|
|
|
|
|
2019-03-15 18:04:15 +01:00
|
|
|
return $this->render('Users/user_settings.html.twig', [
|
2019-03-20 22:53:06 +01:00
|
|
|
'settings_form' => $form->createView(),
|
2019-03-20 23:16:07 +01:00
|
|
|
'pw_form' => $pw_form->createView(),
|
2019-10-13 18:01:13 +02:00
|
|
|
'page_need_reload' => $page_need_reload
|
2019-03-15 18:04:15 +01: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
|
2019-03-20 23:16:07 +01:00
|
|
|
* @param string $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/
|
|
|
|
*/
|
2019-11-05 17:09:01 +01:00
|
|
|
public function getGravatar(?string $email, int $s = 80, string $d = 'mm', string $r = 'g', bool $img = false, array $atts = array())
|
2019-03-14 19:10:11 +01:00
|
|
|
{
|
2019-11-05 17:09:01 +01:00
|
|
|
if ($email === null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:10:11 +01:00
|
|
|
$url = 'https://www.gravatar.com/avatar/';
|
|
|
|
$url .= md5(strtolower(trim($email)));
|
|
|
|
$url .= "?s=$s&d=$d&r=$r";
|
|
|
|
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
|
|
|
}
|