Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http://silex.sensiolabs.org/doc/providers/security.html
- http://symfony.com/doc/3.0/cookbook/doctrine/registration_form.html
- $app->register(new SecurityServiceProvider(), [
- 'security.firewalls' => [
- 'foo' => [ 'pattern' => '^/foo' ],
- 'default' => [
- 'pattern' => '^.*$',
- 'anonymous' => true,
- 'form' => [ 'login_path' => '/login','check_path' => 'connexion' ],
- 'logout' => [ 'logout_path' => '/logout' ],
- 'users' => function($app) {
- return new AuthenticateUserProvider( $app['manager.user'] );
- },
- ],
- ],
- 'security.access_rules' => [
- [ '^/.+$','IS_AUTHENTICATED_ANONYMOUSLY' ],
- [ '^/foo$','' ]
- ]
- ]);
- $app->match('/register', function(Request $request) use($app){
- // Form
- $form = $app['form.factory']->createBuilder(FormType::class)
- ->add('email', EmailType::class, array(
- 'required' => true,
- ))
- ->add('plainPassword', RepeatedType::class, array(
- 'type' => PasswordType::class,
- 'first_options' => array('label' => 'Password'),
- 'second_options' => array('label' => 'Repeat Password'),
- 'required' => true,
- ))
- ->add('firstname', TextType::class, array(
- 'required' => true,
- ))
- ->add('lastname', TextType::class, array(
- 'required' => true,
- ))
- ->getForm();
- // Form traitment
- $form->handleRequest($request);
- if($form->isSubmitted() && $form->isValid()) {
- $data = $form->getData();
- $data['salt'] = mt_rand();
- $data['plainPassword'] = $app['security.encoder.bcrypt']->encodePassword($data['plainPassword'], $data['salt']);
- $user = new UserProvider($data);
- $newUser = $user->addUser();
- if($insert) return $app->redirect($app["url_generator"]->generate("login"));
- }
- return $app['twig']->render('register.twig', [
- 'form' => $form->createView()
- ]);
- })->bind('register');
- {% block content %}
- <form action="#" method="post">
- {{ form_widget(form) }}
- <input type="submit" name="submit" value="S'inscrire" />
- </form>
- {% endblock %}
- namespace Authenticate;
- use SymfonyComponentSecurityCoreUserUserProviderInterface;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use SymfonyComponentSecurityCoreUserUser;
- use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
- use SymfonyComponentSecurityCoreExceptionUnsupportedUserException;
- use ModelUser as ModelUser;
- class UserProvider implements UserProviderInterface
- {
- private $m_user;
- public function __construct(ModelUser $user)
- {
- $this->m_user = $user;
- }
- /**
- * {@inheritDoc}
- */
- public function loadUserByUsername($username)
- {
- $user = $this->loadByUsername($username);
- if (!$user) {
- throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
- }
- return new User($user['username'], $user['password'], explode(',', $user['roles']), true, true, true, true);
- }
- /**
- * {@inheritDoc}
- */
- public function refreshUser(UserInterface $user)
- {
- if (!$user instanceof User) {
- throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
- }
- return $this->loadUserByUsername($user->getUsername());
- }
- /**
- * {@inheritDoc}
- */
- public function supportsClass($class)
- {
- return $class === 'SymfonyComponentSecurityCoreUserUser';
- }
- /**
- * Creates a User object based on a DB row.
- *
- * @param array $row The DB row containing User data.
- * @return ModelUser
- */
- public function addUser()
- {
- $user = new User();
- $user->setUsername($this->m_user['email']);
- $user->setFirstname($this->m_user['firstname']);
- $user->setLastname($this->m_user['lastname']);
- $user->setPassword($this->m_user['encode_password']);
- $user->setSalt($this->m_user['salt']);
- $user->setRole("ROLE_USER");
- $user->setAuthorized(false);
- $user->setAdresseFacturation(null);
- $user->setAdresseLivraison(null);
- $user->createUser();
- }
- }
- namespace Model;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use ModelEntity;
- class User extends Entity implements UserInterface {
- /**
- * User id.
- *
- * @var integer
- */
- private $id;
- /**
- * User name. (Email)
- *
- * @var string
- */
- private $username;
- /**
- * User password.
- *
- * @var string
- */
- private $password;
- /**
- * Firstname
- *
- * @var string
- */
- private $firstname;
- /**
- * Lastname
- *
- * @var string
- */
- private $lastname;
- /**
- * Salt that was originally used to encode the password.
- *
- * @var string
- */
- private $salt;
- /**
- * Role.
- * Values : ROLE_USER or ROLE_ADMIN.
- *
- * @var string
- */
- private $role;
- /**
- * Authorized or not
- *
- * @var boolean
- */
- private $authorized;
- /**
- * Adresse de facturation
- *
- * @var string
- */
- private $addf;
- /**
- * Adresse de livraison
- *
- * @var string
- */
- private $addl;
- public function getId() {
- return $this->id;
- }
- public function setId($id) {
- $this->id = $id;
- }
- /**
- * @inheritDoc
- */
- public function getUsername() {
- return $this->username;
- }
- public function setUsername($username) {
- $this->username = $username;
- }
- public function getFirstname()
- {
- return $this->firstname;
- }
- public function setFirstname($firstname) {
- $this->firstname = $firstname;
- }
- public function getLastname()
- {
- return $this->lastname;
- }
- public function setLastname($lastname) {
- $this->lastname = $lastname;
- }
- /**
- * @inheritDoc
- */
- public function getPassword() {
- return $this->password;
- }
- public function setPassword($password) {
- $this->password = $password;
- }
- /**
- * @inheritDoc
- */
- public function getSalt()
- {
- return $this->salt;
- }
- public function setSalt($salt)
- {
- $this->salt = $salt;
- }
- public function getRole()
- {
- return $this->role;
- }
- public function setRole($role) {
- $this->role = $role;
- }
- public function getAuthorized()
- {
- return $this->authorized;
- }
- public function setAuthorized($authorized) {
- $this->authorized = $authorized;
- }
- public function getAdresseFacturation()
- {
- return $this->addf;
- }
- public function setAdresseFacturation($addf) {
- $this->addf = $addf;
- }
- public function getAdresseLivraison()
- {
- return $this->addl;
- }
- public function setAdresseLivraison($addl) {
- $this->addl = $addl;
- }
- /**
- * @inheritDoc
- */
- public function getRoles()
- {
- return array($this->getRole());
- }
- /**
- * @inheritDoc
- */
- public function eraseCredentials() {
- // Nothing to do here
- }
- public function getUsers()
- {
- $connect = $this->connectBDD();
- $q = $connect->prepare("SELECT * FROM users");
- $q->execute();
- $users = $q->fetchAll();
- return $users;
- }
- public function loadByUsername($username)
- {
- $connect = $this->connectBDD();
- $q = $connect->prepare('SELECT * FROM users WHERE username = ?', [strtolower($username)]);
- $q->execute();
- return $q->fetchAll();
- }
- public function createUser($user)
- {
- $connect = $this->connectBDD();
- $rq = " INSERT INTO users (email, password, firstname, lastname, salt, role, authorized, addf, addl)
- VALUES (:email, :password, :firstname, :lastname, :salt, :role, :authorized, :addf, :addl)";
- $t = $connect->prepare($rq);
- $ok = $t->execute([
- ':email' => $this->getUsername(),
- ':password' => $this->getPassword(),
- ':firstname' => $this->getFirstname(),
- ':lastname' => $this->getLastname(),
- ':salt' => $this->getSalt(),
- ':role' => $this->getRole(),
- ':authorized' => $this->getAuthorized(),
- ':addf' => $this->getAdresseFacturation(),
- ':addl' => $this->getAdresseLivraison()
- ]);
- return $ok ? true : false;
- }
- }
- namespace Model;
- use SilexApplication;
- abstract class Entity {
- private $app;
- private $host;
- private $base;
- private $port;
- private $user;
- private $pass;
- private $charset;
- public function __construct(Application $app)
- {
- $this->app = $app;
- $this->host = $app['config']['database']['host'];
- $this->base = $app['config']['database']['base'];
- $this->port = $app['config']['database']['port'];
- $this->user = $app['config']['database']['user'];
- $this->pass = $app['config']['database']['pass'];
- $this->charset = $app['config']['database']['charset'];
- }
- protected function connectBDD()
- {
- $connect = new PDO("pgsql:host=$this->host;dbname=$this->base", $this->user, $this->pass);
- $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $connect->query("SET NAMES '$this->charset'");
- return $connect;
- }
- }
- FatalThrowableError in UserProvider.php line 17:
- Type error: Argument 1 passed to AuthenticateUserProvider::__construct() must be an instance of ModelUser, array given
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement