Added user entity and basic login/logout system.

This commit is contained in:
Jan Böhmer 2019-03-14 18:01:41 +01:00
parent 71711bc0ba
commit 62d875d1e5
8 changed files with 925 additions and 191 deletions

View file

@ -0,0 +1,66 @@
<?php
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 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\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="login", methods={"GET", "POST"})
*/
public function login(AuthenticationUtils $authenticationUtils)
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* @Route("/logout", name="logout")
*/
public function logout()
{
throw new \Exception('Will be intercepted before getting here');
}
}

314
src/Entity/User.php Normal file
View file

@ -0,0 +1,314 @@
<?php declare(strict_types=1);
/**
*
* part-db version 0.1
* Copyright (C) 2005 Christoph Lechner
* http://www.cl-projects.de/
*
* part-db version 0.2+
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
* http://code.google.com/p/part-db/
*
* Part-DB Version 0.4+
* Copyright (C) 2016 - 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\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @ORM\Table("users")
*/
class User extends NamedDBElement implements UserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
protected $name;
/**
* //@ORM\Column(type="json")
*/
//protected $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
protected $password;
/**
* @var string The first name of the User
* @ORM\Column(type="string", length=255)
*/
protected $first_name;
/**
* @var string The last name of the User
* @ORM\Column(type="string", length=255)
*/
protected $last_name;
/**
* @var string The department the user is working
* @ORM\Column(type="string", length=255)
*/
protected $department;
/**
* @var string The email address of the user
* @ORM\Column(type="string", length=255)
*/
protected $email;
/**
* @var string The language/locale the user prefers
* @ORM\Column(type="string", name="config_language")
*/
protected $language;
/**
* @var string The timezone the user prefers
* @ORM\Column(type="string", name="config_timezone")
*/
protected $timezone;
/**
* @var string The theme
* @ORM\Column(type="string", name="config_theme")
*/
protected $theme;
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->name;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = [];
//$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
//$this->roles = $roles;
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): string
{
return (string) $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* Returns the ID as an string, defined by the element class.
* This should have a form like P000014, for a part with ID 14.
* @return string The ID as a string;
*/
public function getIDString(): string
{
return "U" . $this->getID();
}
/************************************************
* Getters
************************************************/
/**
* @return string
*/
public function getFirstName(): string
{
return $this->first_name;
}
/**
* @param string $first_name
* @return User
*/
public function setFirstName(string $first_name): User
{
$this->first_name = $first_name;
return $this;
}
/**
* @return string
*/
public function getLastName(): string
{
return $this->last_name;
}
/**
* @param string $last_name
* @return User
*/
public function setLastName(string $last_name): User
{
$this->last_name = $last_name;
return $this;
}
/**
* @return string
*/
public function getDepartment(): string
{
return $this->department;
}
/**
* @param string $department
* @return User
*/
public function setDepartment(string $department): User
{
$this->department = $department;
return $this;
}
/**
* @return string
*/
public function getEmail(): string
{
return $this->email;
}
/**
* @param string $email
* @return User
*/
public function setEmail(string $email): User
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getLanguage(): string
{
return $this->language;
}
/**
* @param string $language
* @return User
*/
public function setLanguage(string $language): User
{
$this->language = $language;
return $this;
}
/**
* @return string
*/
public function getTimezone(): string
{
return $this->timezone;
}
/**
* @param string $timezone
* @return User
*/
public function setTimezone(string $timezone): User
{
$this->timezone = $timezone;
return $this;
}
/**
* @return string
*/
public function getTheme(): string
{
return $this->theme;
}
/**
* @param string $theme
* @return User
*/
public function setTheme(string $theme): User
{
$this->theme = $theme;
return $this;
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* @method User|null find($id, $lockMode = null, $lockVersion = null)
* @method User|null findOneBy(array $criteria, array $orderBy = null)
* @method User[] findAll()
* @method User[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class UserRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, User::class);
}
// /**
// * @return User[] Returns an array of User objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->orderBy('u.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?User
{
return $this->createQueryBuilder('u')
->andWhere('u.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}