Материал: Автоматизация управления проектами в студии APPCRAFT

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам
114
Приложение 2
Исходный код программных модулей
<?php
namespace AppBundle\Controller;
use AppBundle\Form\Type\LoginFormType;
use AppBundle\Form\Type\ProfileFormType;
use AppBundle\Controller\InitializableController;
use AppBundle\Entity\Role;
use AppBundle\Entity\User;
use AppBundle\Form\Type\RegistrationFormType;
use AppBundle\Form\Type\RemindPasswordFormType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Config;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use
Symfony\Component\Security\Core\Authentication\Token\UsernamePassw
ordToken;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\Security;
require('autoload.php');
class SecurityController extends InitializableController
{
/**
* @return RedirectResponse|Response
* @Config\Route("/login", name = "site_security_login")
*/
public function loginAction()
115
{
if ($this->authChecker->isGranted(Role::USER)) return
$this->redirectToRoute('homepage');
$error = null;
if ($this->request->attributes-
>has(Security::AUTHENTICATION_ERROR))
$error = $this->request->attributes-
>get(Security::AUTHENTICATION_ERROR);
else {
$error = $this->session-
>get(Security::AUTHENTICATION_ERROR, null);
$this->session-
>remove(Security::AUTHENTICATION_ERROR);
}
if (!is_null($error)) {
$this->addNotice('error', 'security_login.html.twig',
array('notice' => 'auth_error'));
}
$form = $this->createForm(new LoginFormType(), new User());
$this->navigation = array('active' => 'login');
$this->forms = array(
'login' => $form->createView(),
'last_username' => $this->session-
>get(Security::LAST_USERNAME, null)
);
return $this-
>render('AppBundle:Security:login.html.twig');
}
116
/**
* @throws NotFoundHttpException
* @Config\Route("/login-check", name =
"site_security_login_check")
*/
public function loginCheckAction()
{
throw $this->createNotFoundException();
}
/**
* @throws NotFoundHttpException
* @Config\Route("/logout", name = "site_security_logout")
*/
public function logoutAction()
{
throw $this->createNotFoundException();
}
/**
* @return RedirectResponse|Response
* @Config\Route("/profile", name = "site_security_profile")
*/
public function profileAction()
{
$form = $this->createForm(new ProfileFormType(), $this-
>user);
$form->handleRequest($this->request);
if ($form->isSubmitted() && $form->isValid()) {
117
if (!is_null($form->get('password')->getData())) {
/** @var UserPasswordEncoder $encoder */
$encoder = $this-
>get('security.password_encoder');
$this->user->setSalt(User::generateSalt())
->setPassword($encoder->encodePassword($this-
>user, $this->user->getPassword()));
}
$this->manager->persist($this->user);
$this->manager->flush();
$this->addNotice('success',
'security_profile.html.twig',
array('notice' => 'user_changed')
);
return $this->redirectToRoute('homepage');
}
$this->forms['profile'] = $form->createView();
$this->navigation = array('active' => 'profile');
return $this-
>render('AppBundle:Security:profile.html.twig');
}
//регистрация на сайте
/**
* @return Response
* @Config\Route("/registration", name =
"site_general_registration")
*/
118
public function registerAction()
{
$user = new User();
$user->getRolesCollection()->add($this-
>getRepository('Role')->findOneByRole(Role::USER));
$form = $this->createForm(new RegistrationFormType(),
$user);
$form->handleRequest($this->request);
//обработка отправки формы
if ($form->isSubmitted() && $form->isValid()) {
$valid = true;
$samesemail = $this->getRepository('User')-
>createQueryBuilder('u')
->select('COUNT(u.id) AS id')
->where('u.email = :email')
->setParameters(array('email' => $user-
>getEmail()))
->getQuery()->getSingleScalarResult();
if ($samesemail > 0) {
$form->get('email')->addError(new
FormError('Пользователь с таким email уже существует.'));
$valid = false;
}
$sameslogin = $this->getRepository('User')-
>createQueryBuilder('u')
->select('COUNT(u.id) AS id')
->where('u.username = :username')
->setParameters(array('username' => $user-
>getUsername()))
->getQuery()->getSingleScalarResult();
if ($sameslogin > 0) {
Источник: https://baza.diplomsite.ru/previewfile/2299