Added an mechanism to reset passwords via mail.

This commit is contained in:
Jan Böhmer 2019-11-24 22:49:22 +01:00
parent 0716b8ff93
commit 6a0d027675
20 changed files with 2373 additions and 64 deletions

13
.env
View file

@ -27,14 +27,6 @@ APP_SECRET=7915fd8481d1a52cf42010ebe2caa974
DATABASE_URL=mysql://root:@127.0.0.1:3306/part-db
###< doctrine/doctrine-bundle ###
###> symfony/swiftmailer-bundle ###
# For Gmail as a transport, use: "gmail://username:password@localhost"
# For a generic SMTP server, use: "smtp://localhost:25?encryption=&auth_mode="
# Delivery is disabled by default via "null://localhost"
MAILER_URL=null://localhost
###< symfony/swiftmailer-bundle ###
### Custom vars
FIXER_API_KEY=CHANGEME
@ -50,4 +42,7 @@ BANNER=""
# In demo mode things it is not possible for a user to change his password and his settings.
DEMO_MODE=0
### End custom vars
### End custom vars
###> symfony/mailer ###
# MAILER_DSN=smtp://localhost
###< symfony/mailer ###

28
assets/css/email.css Normal file
View file

@ -0,0 +1,28 @@
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 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 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
*/
body,
html,
.body {
background: #f3f3f3 !important;
}
.header {
background: #f3f3f3;
}

File diff suppressed because it is too large Load diff

View file

@ -13,6 +13,7 @@
"doctrine/annotations": "^1.6",
"florianv/swap": "^4.0",
"friendsofsymfony/ckeditor-bundle": "^2.0",
"gregwar/captcha-bundle": "dev-master",
"league/html-to-markdown": "^4.8",
"liip/imagine-bundle": "^2.2",
"nyholm/psr7": "^1.1",
@ -34,6 +35,7 @@
"symfony/form": "4.4.*",
"symfony/framework-bundle": "4.4.*",
"symfony/http-client": "4.4.*",
"symfony/mailer": "4.4.*",
"symfony/monolog-bundle": "^3.1",
"symfony/orm-pack": "*",
"symfony/process": "4.4.*",
@ -45,7 +47,9 @@
"symfony/web-link": "4.4.*",
"symfony/webpack-encore-bundle": "^1.1",
"symfony/yaml": "4.4.*",
"twig/cssinliner-extra": "^3.0",
"twig/extra-bundle": "^3.0",
"twig/inky-extra": "^3.0",
"twig/intl-extra": "^3.0",
"webmozart/assert": "^1.4"
},

548
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "823d8542bcdaea46c533eedb6148de31",
"content-hash": "c70f8ae93e38b0f830b6d86e6ac1c3fb",
"packages": [
{
"name": "clue/stream-filter",
@ -1274,6 +1274,64 @@
],
"time": "2018-06-14T14:45:07+00:00"
},
{
"name": "egulias/email-validator",
"version": "2.1.11",
"source": {
"type": "git",
"url": "https://github.com/egulias/EmailValidator.git",
"reference": "92dd169c32f6f55ba570c309d83f5209cefb5e23"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/egulias/EmailValidator/zipball/92dd169c32f6f55ba570c309d83f5209cefb5e23",
"reference": "92dd169c32f6f55ba570c309d83f5209cefb5e23",
"shasum": ""
},
"require": {
"doctrine/lexer": "^1.0.1",
"php": ">= 5.5"
},
"require-dev": {
"dominicsayers/isemail": "dev-master",
"phpunit/phpunit": "^4.8.35||^5.7||^6.0",
"satooshi/php-coveralls": "^1.0.1",
"symfony/phpunit-bridge": "^4.4@dev"
},
"suggest": {
"ext-intl": "PHP Internationalization Libraries are required to use the SpoofChecking validation"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.1.x-dev"
}
},
"autoload": {
"psr-4": {
"Egulias\\EmailValidator\\": "EmailValidator"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Eduardo Gulias Davis"
}
],
"description": "A library for validating emails against several RFCs",
"homepage": "https://github.com/egulias/EmailValidator",
"keywords": [
"email",
"emailvalidation",
"emailvalidator",
"validation",
"validator"
],
"time": "2019-08-13T17:33:27+00:00"
},
{
"name": "florianv/exchanger",
"version": "2.3.0",
@ -1473,6 +1531,114 @@
],
"time": "2019-04-15T16:29:43+00:00"
},
{
"name": "gregwar/captcha",
"version": "v1.1.7",
"source": {
"type": "git",
"url": "https://github.com/Gregwar/Captcha.git",
"reference": "cf953dd79748406e0292cea8c565399681e4d345"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Gregwar/Captcha/zipball/cf953dd79748406e0292cea8c565399681e4d345",
"reference": "cf953dd79748406e0292cea8c565399681e4d345",
"shasum": ""
},
"require": {
"ext-gd": "*",
"ext-mbstring": "*",
"php": ">=5.3.0",
"symfony/finder": "*"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
},
"type": "captcha",
"autoload": {
"psr-4": {
"Gregwar\\": "src/Gregwar"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Grégoire Passault",
"email": "g.passault@gmail.com",
"homepage": "http://www.gregwar.com/"
},
{
"name": "Jeremy Livingston",
"email": "jeremy.j.livingston@gmail.com"
}
],
"description": "Captcha generator",
"homepage": "https://github.com/Gregwar/Captcha",
"keywords": [
"bot",
"captcha",
"spam"
],
"time": "2018-08-17T22:57:28+00:00"
},
{
"name": "gregwar/captcha-bundle",
"version": "dev-master",
"source": {
"type": "git",
"url": "https://github.com/Gregwar/CaptchaBundle.git",
"reference": "3ccfdf1c9324acbdfbcccea2c454075115741b3a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Gregwar/CaptchaBundle/zipball/3ccfdf1c9324acbdfbcccea2c454075115741b3a",
"reference": "3ccfdf1c9324acbdfbcccea2c454075115741b3a",
"shasum": ""
},
"require": {
"gregwar/captcha": "~1.1",
"php": ">=5.3.9",
"symfony/form": "~2.8|~3.0|~4.0",
"symfony/framework-bundle": "~2.8|~3.0|~4.0",
"twig/twig": "^1.40|^2.9"
},
"type": "captcha-bundle",
"autoload": {
"psr-4": {
"Gregwar\\CaptchaBundle\\": "/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Grégoire Passault",
"email": "g.passault@gmail.com",
"homepage": "http://www.gregwar.com/"
},
{
"name": "Jeremy Livingston",
"email": "jeremy.j.livingston@gmail.com"
}
],
"description": "Captcha bundle",
"homepage": "https://github.com/Gregwar/CaptchaBundle",
"keywords": [
"Symfony2",
"bot",
"captcha",
"code",
"security",
"spam",
"visual"
],
"time": "2019-09-23T13:07:46+00:00"
},
{
"name": "guzzlehttp/guzzle",
"version": "6.4.1",
@ -1934,6 +2100,53 @@
],
"time": "2019-10-04T05:45:14+00:00"
},
{
"name": "lorenzo/pinky",
"version": "1.0.5",
"source": {
"type": "git",
"url": "https://github.com/lorenzo/pinky.git",
"reference": "2bc1a9d5696d6496df5d5682962929165a823e57"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/lorenzo/pinky/zipball/2bc1a9d5696d6496df5d5682962929165a823e57",
"reference": "2bc1a9d5696d6496df5d5682962929165a823e57",
"shasum": ""
},
"require": {
"ext-xsl": "*",
"php": ">=5.6.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"type": "library",
"autoload": {
"files": [
"src/pinky.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jose Lorenzo Rodriguez",
"email": "jose.zap@gmail.com"
}
],
"description": "A Foundation for Emails (Inky) template transpiler",
"keywords": [
"email",
"foundation",
"inky",
"template",
"zurb"
],
"time": "2019-09-16T21:23:31+00:00"
},
{
"name": "monolog/monolog",
"version": "1.25.2",
@ -3973,6 +4186,59 @@
"homepage": "https://symfony.com",
"time": "2019-11-13T07:39:40+00:00"
},
{
"name": "symfony/css-selector",
"version": "v4.4.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/css-selector.git",
"reference": "64acec7e0d67125e9f4656c68d4a38a42ab5a0b7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/css-selector/zipball/64acec7e0d67125e9f4656c68d4a38a42ab5a0b7",
"reference": "64acec7e0d67125e9f4656c68d4a38a42ab5a0b7",
"shasum": ""
},
"require": {
"php": "^7.1.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.4-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\CssSelector\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Jean-François Simon",
"email": "jeanfrancois.simon@sensiolabs.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony CssSelector Component",
"homepage": "https://symfony.com",
"time": "2019-10-12T00:35:04+00:00"
},
{
"name": "symfony/debug",
"version": "v4.4.0",
@ -5255,6 +5521,74 @@
],
"time": "2019-10-12T00:35:04+00:00"
},
{
"name": "symfony/mailer",
"version": "v4.4.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/mailer.git",
"reference": "050b93ce9d307de9567908aa8ab8d6fa3b970921"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/mailer/zipball/050b93ce9d307de9567908aa8ab8d6fa3b970921",
"reference": "050b93ce9d307de9567908aa8ab8d6fa3b970921",
"shasum": ""
},
"require": {
"egulias/email-validator": "^2.1.10",
"php": "^7.1.3",
"psr/log": "~1.0",
"symfony/event-dispatcher": "^4.3",
"symfony/mime": "^4.4|^5.0",
"symfony/service-contracts": "^1.1|^2"
},
"conflict": {
"symfony/http-kernel": "<4.4",
"symfony/sendgrid-mailer": "<4.4"
},
"require-dev": {
"symfony/amazon-mailer": "^4.4|^5.0",
"symfony/google-mailer": "^4.4|^5.0",
"symfony/http-client-contracts": "^1.1|^2",
"symfony/mailchimp-mailer": "^4.4|^5.0",
"symfony/mailgun-mailer": "^4.4|^5.0",
"symfony/messenger": "^4.4|^5.0",
"symfony/postmark-mailer": "^4.4|^5.0",
"symfony/sendgrid-mailer": "^4.4|^5.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.4-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Mailer\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony Mailer Component",
"homepage": "https://symfony.com",
"time": "2019-11-14T14:24:33+00:00"
},
{
"name": "symfony/mime",
"version": "v4.4.0",
@ -7427,6 +7761,109 @@
"homepage": "https://symfony.com",
"time": "2019-11-12T14:51:11+00:00"
},
{
"name": "tijsverkoyen/css-to-inline-styles",
"version": "2.2.2",
"source": {
"type": "git",
"url": "https://github.com/tijsverkoyen/CssToInlineStyles.git",
"reference": "dda2ee426acd6d801d5b7fd1001cde9b5f790e15"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tijsverkoyen/CssToInlineStyles/zipball/dda2ee426acd6d801d5b7fd1001cde9b5f790e15",
"reference": "dda2ee426acd6d801d5b7fd1001cde9b5f790e15",
"shasum": ""
},
"require": {
"ext-dom": "*",
"ext-libxml": "*",
"php": "^5.5 || ^7.0",
"symfony/css-selector": "^2.7 || ^3.0 || ^4.0 || ^5.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.2.x-dev"
}
},
"autoload": {
"psr-4": {
"TijsVerkoyen\\CssToInlineStyles\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Tijs Verkoyen",
"email": "css_to_inline_styles@verkoyen.eu",
"role": "Developer"
}
],
"description": "CssToInlineStyles is a class that enables you to convert HTML-pages/files into HTML-pages/files with inline styles. This is very useful when you're sending emails.",
"homepage": "https://github.com/tijsverkoyen/CssToInlineStyles",
"time": "2019-10-24T08:53:34+00:00"
},
{
"name": "twig/cssinliner-extra",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/twigphp/cssinliner-extra.git",
"reference": "431402520025e077268de4978a4206e5fb8c0103"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/cssinliner-extra/zipball/431402520025e077268de4978a4206e5fb8c0103",
"reference": "431402520025e077268de4978a4206e5fb8c0103",
"shasum": ""
},
"require": {
"php": "^7.1.3",
"tijsverkoyen/css-to-inline-styles": "^2.0",
"twig/twig": "^2.4|^3.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4@dev"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.12-dev"
}
},
"autoload": {
"psr-4": {
"Twig\\Extra\\CssInliner\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
}
],
"description": "A Twig extension to allow inlining CSS",
"homepage": "https://twig.symfony.com",
"keywords": [
"css",
"inlining",
"twig"
],
"time": "2019-10-17T07:27:07+00:00"
},
{
"name": "twig/extra-bundle",
"version": "v3.0.0",
@ -7486,6 +7923,61 @@
],
"time": "2019-10-17T07:30:08+00:00"
},
{
"name": "twig/inky-extra",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/twigphp/inky-extra.git",
"reference": "7e33cb931f29e8cbc1f68eafa30e0ca7f7c6ad3b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/inky-extra/zipball/7e33cb931f29e8cbc1f68eafa30e0ca7f7c6ad3b",
"reference": "7e33cb931f29e8cbc1f68eafa30e0ca7f7c6ad3b",
"shasum": ""
},
"require": {
"lorenzo/pinky": "^1.0.5",
"php": "^7.1.3",
"twig/twig": "^2.4|^3.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4@dev"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.12-dev"
}
},
"autoload": {
"psr-4": {
"Twig\\Extra\\Inky\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
}
],
"description": "A Twig extension for the inky email templating engine",
"homepage": "https://twig.symfony.com",
"keywords": [
"email",
"emails",
"inky",
"twig"
],
"time": "2019-10-17T07:27:12+00:00"
},
{
"name": "twig/intl-extra",
"version": "v3.0.0",
@ -8751,59 +9243,6 @@
"homepage": "https://symfony.com",
"time": "2019-10-28T20:30:34+00:00"
},
{
"name": "symfony/css-selector",
"version": "v4.4.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/css-selector.git",
"reference": "64acec7e0d67125e9f4656c68d4a38a42ab5a0b7"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/css-selector/zipball/64acec7e0d67125e9f4656c68d4a38a42ab5a0b7",
"reference": "64acec7e0d67125e9f4656c68d4a38a42ab5a0b7",
"shasum": ""
},
"require": {
"php": "^7.1.3"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "4.4-dev"
}
},
"autoload": {
"psr-4": {
"Symfony\\Component\\CssSelector\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Jean-François Simon",
"email": "jeanfrancois.simon@sensiolabs.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony CssSelector Component",
"homepage": "https://symfony.com",
"time": "2019-10-12T00:35:04+00:00"
},
{
"name": "symfony/debug-bundle",
"version": "v4.4.0",
@ -9399,6 +9838,7 @@
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"gregwar/captcha-bundle": 20,
"roave/security-advisories": 20
},
"prefer-stable": false,

View file

@ -20,4 +20,5 @@ return [
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true],
Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
Gregwar\CaptchaBundle\GregwarCaptchaBundle::class => ['all' => true]
];

View file

@ -0,0 +1,3 @@
framework:
mailer:
dsn: '%env(MAILER_DSN)%'

View file

@ -5,6 +5,9 @@ twig:
form_themes: ['bootstrap_4_horizontal_layout.html.twig', 'Form/extendedBootstrap4_layout.html.twig', 'Form/permissionLayout.html.twig' ]
exception_controller: ~
paths:
'%kernel.project_dir%/assets/css': css
globals:
partdb_title: '%partdb_title%'
default_currency: '%default_currency%'

View file

@ -17,6 +17,8 @@ parameters:
# Allow users to download attachments to server. Warning: This can be dangerous, because via that feature attackers maybe can access ressources on your intranet!
allow_attachments_downloads: false
demo_mode: '%env(bool:DEMO_MODE)%' # If set to true, all potentially dangerous things are disabled (like changing passwords of the own user)
sender_email: 'noreply@partdb.changeme'
sender_name: 'Part-DB Mailer'
services:
# default configuration for services in *this* file
@ -36,6 +38,12 @@ services:
resource: '../src/Controller'
tags: ['controller.service_arguments']
App\EventSubscriber\MailFromListener:
tags: ['kernel.event_subscriber']
arguments:
$email: '%sender_email%'
$name: '%sender_name%'
Liip\ImagineBundle\Service\FilterService:
alias: 'liip_imagine.service.filter'

View file

@ -21,12 +21,31 @@
namespace App\Controller;
use App\Services\PasswordResetManager;
use Doctrine\ORM\EntityManagerInterface;
use Gregwar\CaptchaBundle\Type\CaptchaType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Contracts\Translation\TranslatorInterface;
class SecurityController extends AbstractController
{
protected $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @Route("/login", name="login", methods={"GET", "POST"})
*/
@ -44,6 +63,88 @@ class SecurityController extends AbstractController
]);
}
/**
* @Route("/pw_reset/request", name="pw_reset_request")
*/
public function requestPwReset(PasswordResetManager $passwordReset, Request $request)
{
$builder = $this->createFormBuilder();
$builder->add('user', TextType::class, [
'label' => $this->translator->trans('pw_reset.user_or_password'),
'constraints' => [new NotBlank()]
]);
$builder->add('captcha', CaptchaType::class, [
'width' => 200,
'height' => 50,
'length' => 6,
]);
$builder->add('submit', SubmitType::class, [
'label' => 'pw_reset.submit'
]);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$passwordReset->request($form->getData()['user']);
$this->addFlash('success', $this->translator->trans('pw_reset.request.success'));
//return $this->redirectToRoute('login');
}
return $this->render('security/pw_reset_request.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/pw_reset/new_pw/{user}/{token}", name="pw_reset_new_pw")
*/
public function pwResetNewPw(PasswordResetManager $passwordReset, Request $request, string $user = null, string $token = null)
{
$data = ['username' => $user, 'token' => $token];
$builder = $this->createFormBuilder($data);
$builder->add('username', TextType::class, [
'label' => $this->translator->trans('pw_reset.username')
]);
$builder->add('token', TextType::class, [
'label' => $this->translator->trans('pw_reset.token')
]);
$builder->add('new_password', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => ['label' => 'user.settings.pw_new.label'],
'second_options' => ['label' => 'user.settings.pw_confirm.label'],
'invalid_message' => 'password_must_match',
'constraints' => [new Length([
'min' => 6,
'max' => 128,
])],
]);
$builder->add('submit', SubmitType::class, [
'label' => 'pw_reset.submit'
]);
$form = $builder->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
//Try to set the new password
$success = $passwordReset->setNewPassword($data['username'], $data['token'], $data['new_password']);
if (!$success) {
$this->addFlash('error', $this->translator->trans('pw_reset.new_pw.error'));
} else {
$this->addFlash('success', $this->translator->trans('pw_reset.new_pw.success'));
return $this->redirectToRoute('login');
}
}
return $this->render('security/pw_reset_new_pw.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/logout", name="logout")
*/

View file

@ -394,6 +394,48 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
return $this;
}
/**
* Returns the encrypted password reset token
* @return string|null
*/
public function getPwResetToken(): ?string
{
return $this->pw_reset_token;
}
/**
* Sets the encrypted password reset token
* @param string|null $pw_reset_token
* @return User
*/
public function setPwResetToken(?string $pw_reset_token): User
{
$this->pw_reset_token = $pw_reset_token;
return $this;
}
/**
* Gets the datetime when the password reset token expires
* @return \DateTime
*/
public function getPwResetExpires(): \DateTime
{
return $this->pw_reset_expires;
}
/**
* Sets the datetime when the password reset token expires
* @param \DateTime $pw_reset_expires
* @return User
*/
public function setPwResetExpires(\DateTime $pw_reset_expires): User
{
$this->pw_reset_expires = $pw_reset_expires;
return $this;
}
/************************************************
* Getters
************************************************/

View file

@ -0,0 +1,58 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 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 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\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\Event\MessageEvent;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
class MailFromListener implements EventSubscriberInterface
{
protected $email;
protected $name;
public function __construct(string $email, string $name)
{
$this->email = $email;
$this->name = $name;
}
public function onMessage(MessageEvent $event): void
{
$address = new Address($this->email, $this->name);
$event->getEnvelope()->setSender($address);
$email = $event->getMessage();
if ($email instanceof Email) {
$email->from($address);
}
}
public static function getSubscribedEvents()
{
return [
// should be the last one to allow header changes by other listeners first
MessageEvent::class => ['onMessage'],
];
}
}

View file

@ -26,6 +26,7 @@ use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping;
use Doctrine\ORM\NonUniqueResultException;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
@ -44,7 +45,7 @@ class UserRepository extends EntityRepository
*
* @return User|null
*/
public function getAnonymousUser()
public function getAnonymousUser() : ?User
{
if ($this->anonymous_user === null) {
$this->anonymous_user = $this->findOneBy([
@ -54,4 +55,29 @@ class UserRepository extends EntityRepository
return $this->anonymous_user;
}
/**
* Find a user by its name or its email. Useful for login or password reset purposes.
* @param string $name_or_password The username or the email of the user that should be found
* @return User|null The user if it is existing, null if no one matched the criteria
*/
public function findByEmailOrName(string $name_or_password) : ?User
{
if (empty($name_or_password)) {
return null;
}
$qb = $this->createQueryBuilder('u');
$qb->select('u')
->where('u.name = (:name)')
->orWhere('u.email = (:email)');
$qb->setParameters(['email' => $name_or_password, 'name' => $name_or_password]);
try {
return $qb->getQuery()->getOneOrNullResult();
} catch (NonUniqueResultException $exception) {
return null;
}
}
}

View file

@ -0,0 +1,135 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 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 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\Services;
use App\Entity\UserSystem\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class PasswordResetManager
{
protected $mailer;
protected $em;
protected $passwordEncoder;
protected $translator;
protected $userPasswordEncoder;
public function __construct(MailerInterface $mailer, EntityManagerInterface $em,
TranslatorInterface $translator, UserPasswordEncoderInterface $userPasswordEncoder,
EncoderFactoryInterface $encoderFactory)
{
$this->em = $em;
$this->mailer = $mailer;
/** @var PasswordEncoderInterface passwordEncoder */
$this->passwordEncoder = $encoderFactory->getEncoder(User::class);
$this->translator = $translator;
$this->userPasswordEncoder = $userPasswordEncoder;
}
public function request(string $name_or_email) : void
{
$repo = $this->em->getRepository(User::class);
//Try to find a user by the given string
$user = $repo->findByEmailOrName($name_or_email);
//Do nothing if no user was found
if ($user === null) {
return;
}
$unencrypted_token = md5(random_bytes(32));
$user->setPwResetToken($this->passwordEncoder->encodePassword($unencrypted_token, null));
//Determine the expiration datetime of
$expiration_date = new \DateTime();
$expiration_date->add(date_interval_create_from_date_string('1 day'));
$user->setPwResetExpires($expiration_date);
if (!empty($user->getEmail())) {
$address = new Address($user->getEmail(), $user->getFullName());
$mail = new TemplatedEmail();
$mail->to($address);
$mail->subject($this->translator->trans('pw_reset.email.subject'));
$mail->htmlTemplate("mail/pw_reset.html.twig");
$mail->context([
'expiration_date' => $expiration_date,
'token' => $unencrypted_token,
'user' => $user
]);
//Send email
$this->mailer->send($mail);
}
//Save changes to DB
$this->em->flush();
}
/**
* Sets the new password of the user with the given name, if the token is valid.
* @param string $user The name of the user, which password should be reset
* @param string $token The token that should be used to reset the password
* @param string $new_password The new password that should be applied to user
* @return bool Returns true, if the new password was applied. False, if either the username is unknown or the
* token is invalid or expired.
*/
public function setNewPassword(string $user, string $token, string $new_password) : bool
{
//Try to find the user
$repo = $this->em->getRepository(User::class);
/** @var User $user */
$user = $repo->findOneBy(['name' => $user]);
//If no user matching the name, show an error message
if ($user === null) {
return false;
}
//Check if token is expired yet
if ($user->getPwResetExpires() < new \DateTime()) {
return false;
}
//Check if token is valid
if (!$this->passwordEncoder->isPasswordValid($user->getPwResetToken(), $token, null)) {
return false;
}
//When everything was valid, apply the new password
$user->setPassword($this->userPasswordEncoder->encodePassword($user, $new_password));
//Remove token
$user->setPwResetToken(null);
$user->setPwResetExpires(new \DateTime());
//Save to DB
$this->em->flush();
return true;
}
}

View file

@ -129,6 +129,9 @@
"./config/packages/dev/easy_log_handler.yaml"
]
},
"egulias/email-validator": {
"version": "2.1.11"
},
"felixfbecker/advanced-json-rpc": {
"version": "v3.0.4"
},
@ -153,6 +156,12 @@
"./config/packages/fos_ckeditor.yaml"
]
},
"gregwar/captcha": {
"version": "v1.1.7"
},
"gregwar/captcha-bundle": {
"version": "v2.0.6"
},
"guzzlehttp/guzzle": {
"version": "6.3.3"
},
@ -184,6 +193,9 @@
"./config/routes/liip_imagine.yaml"
]
},
"lorenzo/pinky": {
"version": "1.0.5"
},
"monolog/monolog": {
"version": "1.24.0"
},
@ -465,6 +477,18 @@
"symfony/intl": {
"version": "v4.2.3"
},
"symfony/mailer": {
"version": "4.3",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "master",
"version": "4.3",
"ref": "15658c2a0176cda2e7dba66276a2030b52bd81b2"
},
"files": [
"./config/packages/mailer.yaml"
]
},
"symfony/maker-bundle": {
"version": "1.0",
"recipe": {
@ -688,9 +712,18 @@
"symfony/yaml": {
"version": "v4.2.3"
},
"tijsverkoyen/css-to-inline-styles": {
"version": "2.2.2"
},
"twig/cssinliner-extra": {
"version": "v3.0.0"
},
"twig/extra-bundle": {
"version": "v3.0.0"
},
"twig/inky-extra": {
"version": "v3.0.0"
},
"twig/intl-extra": {
"version": "3.x-dev"
},

View file

@ -0,0 +1,30 @@
{% apply inky_to_html|inline_css(source('@css/foundation-emails.css'), source('@css/email.css')) %}
<container>
<spacer size="16"></spacer>
<row class="header">
<columns>
<spacer size="16"></spacer>
<h4 class="text-center"><a href="{{ url('homepage') }}">{{ partdb_title }}</a></h4>
</columns>
</row>
<row>
<columns>
<spacer size="32"></spacer>
{% block content %}
{% endblock %}
</columns>
</row>
<row>
<columns>
<p><small>This email was send automatically by <a href="{{ url('homepage') }}">Part-DB</a>. Dont answer to this email.</small></p>
</columns>
</row>
<spacer size="16"></spacer>
</container>
{% endapply %}

View file

@ -0,0 +1,30 @@
{% extends "mail/base.html.twig" %}
{% block content %}
<row>
<columns>
<h4>Hi {{ user.fullName }},</h4>
somebody (hopefully you) requested an reset of your password.
If this request was not made by you, ignore this email.
<br>
<button class="large expand" href="{{ url('pw_reset_new_pw', {user: user.name, token: token}) }}">Click here to reset password</button>
<br>
If this dont work for you. Go to <a href="{{ url('pw_reset_new_pw') }}">{{ url('pw_reset_new_pw') }}</a>
and enter the following info:
<callout class="secondary">
<row>
<columns>
<p>
<b>Username: </b> {{ user.name }}
</p>
<p>
<b>Token:</b> {{ token }}
</p>
</columns>
</row>
</callout>
The reset token will be valid until <i>{{ expiration_date|format_datetime }}</i>.
</columns>
</row>
{% endblock %}

View file

@ -59,4 +59,6 @@
</div>
</div>
</form>
<a class="offset-2" href="{{ url('pw_reset_request') }}">{% trans %}pw_reset.password_forget{% endtrans %}</a>
{% endblock %}

View file

@ -0,0 +1,11 @@
{% extends "main_card.html.twig" %}
{% block card_title %}
<i class="fas fa-key"></i>
{% trans %}pw_reset.new_pw.header.title{% endtrans %}
{% endblock %}
{% block card_content %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}

View file

@ -0,0 +1,11 @@
{% extends "main_card.html.twig" %}
{% block card_title %}
<i class="fas fa-key"></i>
{% trans %}pw_reset.request.header.title{% endtrans %}
{% endblock %}
{% block card_content %}
{{ form_start(form) }}
{{ form_end(form) }}
{% endblock %}