From 4c2886b16484756a7966ef86bcef6ae41e080391 Mon Sep 17 00:00:00 2001 From: Bennet Matschullat Date: Sun, 18 Dec 2016 17:59:38 +0100 Subject: [PATCH] inital --- Command/AppCommand.php | 64 +++ Controller/SecurityController.php | 71 +++ DataFixtures/ORM/LoadUserData.php | 59 +++ DependencyInjection/Configuration.php | 29 ++ DependencyInjection/DreimwebUserExtension.php | 28 ++ DreimwebUserBundle.php | 9 + Entity/Email.php | 213 +++++++++ Entity/User.php | 430 ++++++++++++++++++ Repository/EmailRepository.php | 13 + Repository/UserRepository.php | 78 ++++ Resources/config/services.yml | 4 + Resources/views/Default/index.html.twig | 23 + Resources/views/Security/login.html.twig | 95 ++++ SessionRequestProcessor.php | 38 ++ Tests/Controller/DefaultControllerTest.php | 17 + 15 files changed, 1171 insertions(+) create mode 100644 Command/AppCommand.php create mode 100644 Controller/SecurityController.php create mode 100644 DataFixtures/ORM/LoadUserData.php create mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/DreimwebUserExtension.php create mode 100644 DreimwebUserBundle.php create mode 100644 Entity/Email.php create mode 100644 Entity/User.php create mode 100644 Repository/EmailRepository.php create mode 100644 Repository/UserRepository.php create mode 100644 Resources/config/services.yml create mode 100644 Resources/views/Default/index.html.twig create mode 100644 Resources/views/Security/login.html.twig create mode 100644 SessionRequestProcessor.php create mode 100644 Tests/Controller/DefaultControllerTest.php diff --git a/Command/AppCommand.php b/Command/AppCommand.php new file mode 100644 index 0000000..3e27001 --- /dev/null +++ b/Command/AppCommand.php @@ -0,0 +1,64 @@ + + * @date 23.06.2016 - 09:36 + * @github + * @project - PhpStorm + */ + +namespace Dreimweb\UserBundle\Command; + + + +use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +class AppCommand extends ContainerAwareCommand +{ + protected function configure() + { + $this + ->setName('app:sendmails') + ->setDescription('Send all Mails from que') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + //$this->denyAccessUnlessGranted(['ROLE_DEVELOPER'], null, 'Unzureichende Rechte fuer diese Seite'); + $repo = $this->getContainer()->get('doctrine'); + $logger = $this->getContainer()->get('logger'); + $manager = $repo->getManager(); + $mails = $repo->getRepository('DreimwebUserBundle:Email')->findBy(['flagstate' => 0]); + + /** @var Email $mail */ + foreach ($mails as $mail) { + $mail->setFlagstate(true); + $message = \Swift_Message::newInstance() + ->setSubject($mail->getSubject()) + ->setFrom('noreply@appname.de', 'appname') + ->setReplyTo('info@appname.de') + ->setTo($mail->getReceiver()) + ->setBody($mail->getBodyMessage(), 'text/html'); + $type = $message->getHeaders()->get('Content-Type'); + + $output->writeln(sprintf('mail "%s" to "%s"', $mail->getSubject(), $mail->getReceiver())); + $type->setValue('text/html'); + $type->setParameter('charset', 'utf-8'); + + // push to the logger + $logger->info(sprintf('mail "%s" to "%s"', $mail->getSubject(), $mail->getReceiver())); + + $this->getContainer()->get('mailer')->send($message); + $manager->persist($mail); + } + + + $manager->flush(); + $output->writeln('OK'); + } +} \ No newline at end of file diff --git a/Controller/SecurityController.php b/Controller/SecurityController.php new file mode 100644 index 0000000..234a3b0 --- /dev/null +++ b/Controller/SecurityController.php @@ -0,0 +1,71 @@ +getSession(); + // get the login error if there is one + $error = $session->get(Security::AUTHENTICATION_ERROR); + $session->remove(Security::AUTHENTICATION_ERROR); + return array( + // last username entered by the user + 'last_username' => $session->get(Security::LAST_USERNAME), + 'error' => $error, + ); + } + /** + * @Route("/auth/login_check", name="login_check") + * @Template() + */ + public function loginCheckAction() + { + return array(// ... + ); + } + /** + * @Route("/auth/logout", name="_logout") + */ + public function logoutAction(Request $request) + { + $this->container->get('security.context')->setToken(null); + $uri = $this->get('router')->generate('homepage'); + return $this->redirect($uri); + } + /** + * @Route("/auth/activation/{ident_key}", name="_user_activation") + */ + public function activationAction(Request $request, $ident_key) + { + // load session manager + $session = $this->get('session'); + // load doctrine manager + $em = $this->getDoctrine()->getManager(); + $userRepo = $em->getRepository('DreimwebUserBundle:User'); + /** @var User $user */ + $user = $userRepo->findOneBy(['ident_key' => $ident_key]); + if ($user) { + $user->setFlagstate(true); + $user->setIsActive(true); + $em->persist($user); + $em->flush(); + $session->getFlashBag()->add('notice', 'Ihr Benutzerkonto wurde erfolgreich aktiviert'); + } else { + $session->getFlashBag()->add('error', 'Kein Benutzer mit diesen Daten gefunden'); + } + $uri = $this->get('router')->generate('_login'); + return $this->redirect($uri); + } +} \ No newline at end of file diff --git a/DataFixtures/ORM/LoadUserData.php b/DataFixtures/ORM/LoadUserData.php new file mode 100644 index 0000000..4f070fe --- /dev/null +++ b/DataFixtures/ORM/LoadUserData.php @@ -0,0 +1,59 @@ + + * @date 05.11.15 - 10:44 + * @github + * @project - user bundle + */ + +namespace Dreimweb\UserBundle\DataFixtures\ORM; + +use Doctrine\Common\DataFixtures\FixtureInterface; +use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\ORM\Id\UuidGenerator; +use Dreimweb\UserBundle\Entity\User; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; + +class LoadUserData implements FixtureInterface, ContainerAwareInterface +{ + + // - - - + private $container; + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + public function load(ObjectManager $manager) + { + $user = new User(); + $user->setUsername('developer'); + $user->setPassword($this->encodePassword($user, 'darthpass')); + $user->setEmail('developer@appname.de'); + $user->setRoles(['ROLE_USER', 'ROLE_DEVELOPER', 'ROLE_ADMIN']); + $user->setFirstname('Vorname'); + $user->setLastname('Nachname'); + $user->setIsActive(true); + + $manager->persist($user); + $manager->flush(); + } + + private function encodePassword(User $user, $plainPassword) + { + $encoder = $this->container->get('security.encoder_factory') + ->getEncoder($user); + + return $encoder->encodePassword($plainPassword, $user->getSalt()); + } + + + public function getOrder() + { + return 1; + } + + +} \ No newline at end of file diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php new file mode 100644 index 0000000..e9a3f86 --- /dev/null +++ b/DependencyInjection/Configuration.php @@ -0,0 +1,29 @@ +root('dreimweb_user'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/DependencyInjection/DreimwebUserExtension.php b/DependencyInjection/DreimwebUserExtension.php new file mode 100644 index 0000000..6dc85fd --- /dev/null +++ b/DependencyInjection/DreimwebUserExtension.php @@ -0,0 +1,28 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } +} diff --git a/DreimwebUserBundle.php b/DreimwebUserBundle.php new file mode 100644 index 0000000..2e8b527 --- /dev/null +++ b/DreimwebUserBundle.php @@ -0,0 +1,9 @@ +updatedAt = date('Y-m-d H:i:s'); + } + + + /** + * @ORM\PrePersist() + */ + public function prePersist() + { + $this->updatedAt = date('Y-m-d H:i:s'); + $this->createdAt = date('Y-m-d H:i:s'); + } + + + + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set subject + * + * @param string $subject + * + * @return Email + */ + public function setSubject($subject) + { + $this->subject = $subject; + + return $this; + } + + /** + * Get subject + * + * @return string + */ + public function getSubject() + { + return $this->subject; + } + + + + /** + * Set bodyMessage + * + * @param string $bodyMessage + * + * @return Email + */ + public function setBodyMessage($bodyMessage) + { + $this->bodyMessage = $bodyMessage; + + return $this; + } + + /** + * Get bodyMessage + * + * @return string + */ + public function getBodyMessage() + { + return $this->bodyMessage; + } + + /** + * @return string + */ + public function getReceiver() + { + return $this->receiver; + } + + /** + * @param string $receiver + */ + public function setReceiver($receiver) + { + $this->receiver = $receiver; + } + + /** + * @return string + */ + public function getFlagstate() + { + return $this->flagstate; + } + + /** + * @param string $flagstate + */ + public function setFlagstate($flagstate) + { + $this->flagstate = $flagstate; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param mixed $createdAt + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + } + + /** + * @return mixed + */ + public function getUpdatedAt() + { + return $this->updatedAt; + } + + /** + * @param mixed $updatedAt + */ + public function setUpdatedAt($updatedAt) + { + $this->updatedAt = $updatedAt; + } + + + + +} + diff --git a/Entity/User.php b/Entity/User.php new file mode 100644 index 0000000..90fed86 --- /dev/null +++ b/Entity/User.php @@ -0,0 +1,430 @@ +getIsActive(); + } + + public function isCredentialsNonExpired() + { + return true; + } + + public function isEnabled() + { + return $this->getIsActive(); + } + + + /** + * @ORM\PrePersist() + * @ORM\PreUpdate() + */ + public function preUpdate() + { + $this->updatedAt = date('Y-m-d H:i:s'); + } + + + /** + * @ORM\PrePersist() + */ + public function prePersist() + { + $this->createdAt = date('Y-m-d H:i:s'); + } + + + /** + * Get id + * + * @return int + */ + public function getId() + { + return $this->id; + } + + /** + * Set username + * + * @param string $username + * + * @return User + */ + public function setUsername($username) + { + $this->username = $username; + + return $this; + } + + /** + * Get username + * + * @return string + */ + public function getUsername() + { + return $this->username; + } + + /** + * Set password + * + * @param string $password + * + * @return User + */ + public function setPassword($password) + { + $this->password = $password; + + return $this; + } + + /** + * Get password + * + * @return string + */ + public function getPassword() + { + return $this->password; + } + + /** + * Set email + * + * @param string $email + * + * @return User + */ + public function setEmail($email) + { + $this->email = $email; + + return $this; + } + + /** + * Get email + * + * @return string + */ + public function getEmail() + { + return $this->email; + } + + public function getRoles() + { + return $this->roles; + } + + public function setRoles(array $roles) + { + $this->roles = $roles; + + // allows for chaining + return $this; + } + + + public function eraseCredentials() + { + // blank for now + } + + + public function getSalt() + { + return null; + } + + + /** + * generate random string + * + * @param int $length + * @return string + */ + public static function generatePassword($length = 8) + { + $possibleChars = "abcdefghijklmnopqrstuvwxyz0123456789*!+"; + $password = ''; + + for ($i = 0; $i < $length; $i++) { + $rand = rand(0, strlen($possibleChars) - 1); + $password .= substr($possibleChars, $rand, 1); + } + + return $password; + } + + /** + * @return string + */ + public function getFlagstate() + { + return $this->flagstate; + } + + /** + * @param string $flagstate + */ + public function setFlagstate($flagstate) + { + $this->flagstate = $flagstate; + } + + /** + * @return string + */ + public function getFirstname() + { + return $this->firstname; + } + + /** + * @param string $firstname + */ + public function setFirstname($firstname) + { + $this->firstname = $firstname; + } + + /** + * @return string + */ + public function getLastname() + { + return $this->lastname; + } + + /** + * @param string $lastname + */ + public function setLastname($lastname) + { + $this->lastname = $lastname; + } + + /** + * @return string + */ + public function getGender() + { + return $this->gender; + } + + /** + * @param string $gender + */ + public function setGender($gender) + { + $this->gender = $gender; + } + + /** + * @return string + */ + public function getAttributes() + { + return $this->attributes; + } + + /** + * @param string $attributes + */ + public function setAttributes($attributes) + { + $this->attributes = $attributes; + } + + /** + * @return mixed + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param mixed $createdAt + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + } + + /** + * @return mixed + */ + public function getUpdatedAt() + { + return $this->updatedAt; + } + + /** + * @param mixed $updatedAt + */ + public function setUpdatedAt($updatedAt) + { + $this->updatedAt = $updatedAt; + } + + + /** + * @return mixed + */ + public function getIdentKey() + { + return $this->ident_key; + } + + /** + * @param mixed $ident_key + */ + public function setIdentKey($ident_key) + { + $this->ident_key = $ident_key; + } + + /** + * @return mixed + */ + public function getIsActive() + { + return $this->isActive; + } + + /** + * @param mixed $isActive + */ + public function setIsActive($isActive) + { + $this->isActive = $isActive; + } + +} + diff --git a/Repository/EmailRepository.php b/Repository/EmailRepository.php new file mode 100644 index 0000000..1e6dfc9 --- /dev/null +++ b/Repository/EmailRepository.php @@ -0,0 +1,13 @@ +createQueryBuilder('u') + ->andWhere('u.username = :username OR u.email = :email') + ->setParameter('username', $username) + ->setParameter('email', $username) + ->getQuery() + ->getOneOrNullResult(); + } + + + public function allCount() + { + $all = parent::findAll(); + return count($all); + } + + /* public function findAll() + { + die('N0!'); + }*/ + + public function loadUserByUsername($username) + { + $user = $this->findOneByUsernameOrEmail($username); + + if (!$user) { + throw new UsernameNotFoundException('No user found for username ' . $username); + } + + return $user; + + } + + public function refreshUser(UserInterface $user) + { + $class = get_class($user); + if (!$this->supportsClass($class)) { + throw new UnsupportedUserException(sprintf( + 'Instances of "%s" are not supported.', + $class + )); + } + + if (!$refreshedUser = $this->find($user->getId())) { + throw new UsernameNotFoundException(sprintf('User with id %s not found', json_encode($user->getId()))); + } + + return $refreshedUser; + } + + public function supportsClass($class) + { + return $this->getEntityName() === $class + || is_subclass_of($class, $this->getEntityName()); + + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml new file mode 100644 index 0000000..1dd3b63 --- /dev/null +++ b/Resources/config/services.yml @@ -0,0 +1,4 @@ +services: +# dreimweb_user.example: +# class: Dreimweb\UserBundle\Example +# arguments: ["@service_id", "plain_value", "%parameter%"] diff --git a/Resources/views/Default/index.html.twig b/Resources/views/Default/index.html.twig new file mode 100644 index 0000000..3d80ee2 --- /dev/null +++ b/Resources/views/Default/index.html.twig @@ -0,0 +1,23 @@ +{% extends "::base.html.twig" %} + +{% block title %}Dashboard{% endblock %} + + +{% block body %} + +
+
+
+

Dashboard

+
+
+
+ + + +
+

Hier muss Inhalt hin

+ +
+ +{% endblock %} \ No newline at end of file diff --git a/Resources/views/Security/login.html.twig b/Resources/views/Security/login.html.twig new file mode 100644 index 0000000..44fb573 --- /dev/null +++ b/Resources/views/Security/login.html.twig @@ -0,0 +1,95 @@ + + + + + + + + + + Inveus - MIM Backend + + + + + + + + + + + + + + + + + + + + +
+ +
+
+
+
+
+
+

inveusmim Backend

+
+
+ + + {% if error %} + {% if error.messageKey == 'Account is locked.' %} +
Ihr Benutzerkonto ist momentan gesperrrt!
Haben + Sie Ihr Benutzerkonto schon aktiviert? +
+ {% elseif error.messageKey == 'Invalid credentials.' %} +
Email Adresse oder Passwort falsch!
+ {% else %} +
{{ error.messageKey }}
+ {% endif %} + + {% endif %} + {% for flash_message in app.session.flashbag.get('notice') %} +

{{ flash_message }}

+ {% endfor %} + {% for flash_message in app.session.flashbag.get('error') %} +

{{ flash_message }}

+ {% endfor %} +
+
+ +
+
+ +
+ +
+
+ +
+

inveusmim + {{ version }}-{{ server }} + © {{ "now"|date('Y') }}. Made by Hamburg, Germany

+
+
+
+ +
+ + +
+ + + + \ No newline at end of file diff --git a/SessionRequestProcessor.php b/SessionRequestProcessor.php new file mode 100644 index 0000000..09110ed --- /dev/null +++ b/SessionRequestProcessor.php @@ -0,0 +1,38 @@ + + * @date 11.09.16 - 08:26 + * @github + * @project - PhpStorm + */ + + +use Symfony\Component\HttpFoundation\Session\Session; + +class SessionRequestProcessor +{ + private $session; + private $token; + + public function __construct(Session $session) + { + $this->session = $session; + } + + public function processRecord(array $record) + { + if (null === $this->token) { + try { + $this->token = substr($this->session->getId(), 0, 8); + } catch (\RuntimeException $e) { + $this->token = '????????'; + } + $this->token .= '-' . substr(uniqid(), -8); + } + $record['extra']['token'] = $this->token; + + return $record; + } +} \ No newline at end of file diff --git a/Tests/Controller/DefaultControllerTest.php b/Tests/Controller/DefaultControllerTest.php new file mode 100644 index 0000000..0cb1653 --- /dev/null +++ b/Tests/Controller/DefaultControllerTest.php @@ -0,0 +1,17 @@ +request('GET', '/'); + + $this->assertContains('Hello World', $client->getResponse()->getContent()); + } +}