Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.64 KB | None | 0 0
  1. http://silex.sensiolabs.org/doc/providers/security.html
  2. http://symfony.com/doc/3.0/cookbook/doctrine/registration_form.html
  3.  
  4. $app->register(new SecurityServiceProvider(), [
  5. 'security.firewalls' => [
  6. 'foo' => [ 'pattern' => '^/foo' ],
  7. 'default' => [
  8. 'pattern' => '^.*$',
  9. 'anonymous' => true,
  10. 'form' => [ 'login_path' => '/login','check_path' => 'connexion' ],
  11. 'logout' => [ 'logout_path' => '/logout' ],
  12. 'users' => function($app) {
  13. return new AuthenticateUserProvider( $app['manager.user'] );
  14. },
  15. ],
  16. ],
  17. 'security.access_rules' => [
  18. [ '^/.+$','IS_AUTHENTICATED_ANONYMOUSLY' ],
  19. [ '^/foo$','' ]
  20. ]
  21. ]);
  22.  
  23. $app->match('/register', function(Request $request) use($app){
  24.  
  25. // Form
  26. $form = $app['form.factory']->createBuilder(FormType::class)
  27. ->add('email', EmailType::class, array(
  28. 'required' => true,
  29. ))
  30. ->add('plainPassword', RepeatedType::class, array(
  31. 'type' => PasswordType::class,
  32. 'first_options' => array('label' => 'Password'),
  33. 'second_options' => array('label' => 'Repeat Password'),
  34. 'required' => true,
  35. ))
  36. ->add('firstname', TextType::class, array(
  37. 'required' => true,
  38. ))
  39. ->add('lastname', TextType::class, array(
  40. 'required' => true,
  41. ))
  42. ->getForm();
  43.  
  44.  
  45. // Form traitment
  46. $form->handleRequest($request);
  47.  
  48. if($form->isSubmitted() && $form->isValid()) {
  49. $data = $form->getData();
  50.  
  51. $data['salt'] = mt_rand();
  52. $data['plainPassword'] = $app['security.encoder.bcrypt']->encodePassword($data['plainPassword'], $data['salt']);
  53.  
  54. $user = new UserProvider($data);
  55. $newUser = $user->addUser();
  56.  
  57. if($insert) return $app->redirect($app["url_generator"]->generate("login"));
  58. }
  59.  
  60. return $app['twig']->render('register.twig', [
  61. 'form' => $form->createView()
  62. ]);
  63. })->bind('register');
  64.  
  65. {% block content %}
  66. <form action="#" method="post">
  67. {{ form_widget(form) }}
  68.  
  69. <input type="submit" name="submit" value="S'inscrire" />
  70. </form>
  71. {% endblock %}
  72.  
  73. namespace Authenticate;
  74.  
  75. use SymfonyComponentSecurityCoreUserUserProviderInterface;
  76. use SymfonyComponentSecurityCoreUserUserInterface;
  77. use SymfonyComponentSecurityCoreUserUser;
  78. use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
  79. use SymfonyComponentSecurityCoreExceptionUnsupportedUserException;
  80.  
  81. use ModelUser as ModelUser;
  82.  
  83. class UserProvider implements UserProviderInterface
  84. {
  85. private $m_user;
  86.  
  87. public function __construct(ModelUser $user)
  88. {
  89. $this->m_user = $user;
  90. }
  91.  
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function loadUserByUsername($username)
  96. {
  97.  
  98. $user = $this->loadByUsername($username);
  99.  
  100. if (!$user) {
  101. throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
  102. }
  103.  
  104. return new User($user['username'], $user['password'], explode(',', $user['roles']), true, true, true, true);
  105. }
  106.  
  107.  
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function refreshUser(UserInterface $user)
  112. {
  113. if (!$user instanceof User) {
  114. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
  115. }
  116.  
  117. return $this->loadUserByUsername($user->getUsername());
  118. }
  119.  
  120.  
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function supportsClass($class)
  125. {
  126. return $class === 'SymfonyComponentSecurityCoreUserUser';
  127. }
  128.  
  129.  
  130. /**
  131. * Creates a User object based on a DB row.
  132. *
  133. * @param array $row The DB row containing User data.
  134. * @return ModelUser
  135. */
  136. public function addUser()
  137. {
  138. $user = new User();
  139.  
  140. $user->setUsername($this->m_user['email']);
  141. $user->setFirstname($this->m_user['firstname']);
  142. $user->setLastname($this->m_user['lastname']);
  143. $user->setPassword($this->m_user['encode_password']);
  144. $user->setSalt($this->m_user['salt']);
  145. $user->setRole("ROLE_USER");
  146. $user->setAuthorized(false);
  147. $user->setAdresseFacturation(null);
  148. $user->setAdresseLivraison(null);
  149.  
  150. $user->createUser();
  151. }
  152.  
  153. }
  154.  
  155. namespace Model;
  156.  
  157. use SymfonyComponentSecurityCoreUserUserInterface;
  158. use ModelEntity;
  159.  
  160. class User extends Entity implements UserInterface {
  161.  
  162. /**
  163. * User id.
  164. *
  165. * @var integer
  166. */
  167. private $id;
  168.  
  169. /**
  170. * User name. (Email)
  171. *
  172. * @var string
  173. */
  174. private $username;
  175.  
  176. /**
  177. * User password.
  178. *
  179. * @var string
  180. */
  181. private $password;
  182.  
  183. /**
  184. * Firstname
  185. *
  186. * @var string
  187. */
  188. private $firstname;
  189.  
  190. /**
  191. * Lastname
  192. *
  193. * @var string
  194. */
  195. private $lastname;
  196.  
  197. /**
  198. * Salt that was originally used to encode the password.
  199. *
  200. * @var string
  201. */
  202. private $salt;
  203.  
  204. /**
  205. * Role.
  206. * Values : ROLE_USER or ROLE_ADMIN.
  207. *
  208. * @var string
  209. */
  210. private $role;
  211.  
  212. /**
  213. * Authorized or not
  214. *
  215. * @var boolean
  216. */
  217. private $authorized;
  218.  
  219. /**
  220. * Adresse de facturation
  221. *
  222. * @var string
  223. */
  224. private $addf;
  225.  
  226. /**
  227. * Adresse de livraison
  228. *
  229. * @var string
  230. */
  231. private $addl;
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238. public function getId() {
  239. return $this->id;
  240. }
  241.  
  242. public function setId($id) {
  243. $this->id = $id;
  244. }
  245.  
  246.  
  247. /**
  248. * @inheritDoc
  249. */
  250. public function getUsername() {
  251. return $this->username;
  252. }
  253.  
  254. public function setUsername($username) {
  255. $this->username = $username;
  256. }
  257.  
  258.  
  259. public function getFirstname()
  260. {
  261. return $this->firstname;
  262. }
  263.  
  264. public function setFirstname($firstname) {
  265. $this->firstname = $firstname;
  266. }
  267.  
  268.  
  269. public function getLastname()
  270. {
  271. return $this->lastname;
  272. }
  273.  
  274. public function setLastname($lastname) {
  275. $this->lastname = $lastname;
  276. }
  277.  
  278.  
  279. /**
  280. * @inheritDoc
  281. */
  282. public function getPassword() {
  283. return $this->password;
  284. }
  285.  
  286. public function setPassword($password) {
  287. $this->password = $password;
  288. }
  289.  
  290.  
  291. /**
  292. * @inheritDoc
  293. */
  294. public function getSalt()
  295. {
  296. return $this->salt;
  297. }
  298.  
  299. public function setSalt($salt)
  300. {
  301. $this->salt = $salt;
  302. }
  303.  
  304.  
  305. public function getRole()
  306. {
  307. return $this->role;
  308. }
  309.  
  310. public function setRole($role) {
  311. $this->role = $role;
  312. }
  313.  
  314.  
  315. public function getAuthorized()
  316. {
  317. return $this->authorized;
  318. }
  319.  
  320. public function setAuthorized($authorized) {
  321. $this->authorized = $authorized;
  322. }
  323.  
  324.  
  325. public function getAdresseFacturation()
  326. {
  327. return $this->addf;
  328. }
  329.  
  330. public function setAdresseFacturation($addf) {
  331. $this->addf = $addf;
  332. }
  333.  
  334.  
  335. public function getAdresseLivraison()
  336. {
  337. return $this->addl;
  338. }
  339.  
  340. public function setAdresseLivraison($addl) {
  341. $this->addl = $addl;
  342. }
  343.  
  344.  
  345. /**
  346. * @inheritDoc
  347. */
  348. public function getRoles()
  349. {
  350. return array($this->getRole());
  351. }
  352.  
  353.  
  354. /**
  355. * @inheritDoc
  356. */
  357. public function eraseCredentials() {
  358. // Nothing to do here
  359. }
  360.  
  361.  
  362.  
  363. public function getUsers()
  364. {
  365. $connect = $this->connectBDD();
  366.  
  367. $q = $connect->prepare("SELECT * FROM users");
  368. $q->execute();
  369. $users = $q->fetchAll();
  370.  
  371. return $users;
  372. }
  373.  
  374.  
  375.  
  376. public function loadByUsername($username)
  377. {
  378. $connect = $this->connectBDD();
  379.  
  380. $q = $connect->prepare('SELECT * FROM users WHERE username = ?', [strtolower($username)]);
  381. $q->execute();
  382.  
  383. return $q->fetchAll();
  384. }
  385.  
  386.  
  387.  
  388. public function createUser($user)
  389. {
  390. $connect = $this->connectBDD();
  391.  
  392. $rq = " INSERT INTO users (email, password, firstname, lastname, salt, role, authorized, addf, addl)
  393. VALUES (:email, :password, :firstname, :lastname, :salt, :role, :authorized, :addf, :addl)";
  394.  
  395. $t = $connect->prepare($rq);
  396. $ok = $t->execute([
  397. ':email' => $this->getUsername(),
  398. ':password' => $this->getPassword(),
  399. ':firstname' => $this->getFirstname(),
  400. ':lastname' => $this->getLastname(),
  401. ':salt' => $this->getSalt(),
  402. ':role' => $this->getRole(),
  403. ':authorized' => $this->getAuthorized(),
  404. ':addf' => $this->getAdresseFacturation(),
  405. ':addl' => $this->getAdresseLivraison()
  406. ]);
  407.  
  408. return $ok ? true : false;
  409. }
  410. }
  411.  
  412. namespace Model;
  413.  
  414. use SilexApplication;
  415.  
  416. abstract class Entity {
  417.  
  418. private $app;
  419. private $host;
  420. private $base;
  421. private $port;
  422. private $user;
  423. private $pass;
  424. private $charset;
  425.  
  426. public function __construct(Application $app)
  427. {
  428. $this->app = $app;
  429.  
  430. $this->host = $app['config']['database']['host'];
  431. $this->base = $app['config']['database']['base'];
  432. $this->port = $app['config']['database']['port'];
  433. $this->user = $app['config']['database']['user'];
  434. $this->pass = $app['config']['database']['pass'];
  435. $this->charset = $app['config']['database']['charset'];
  436. }
  437.  
  438. protected function connectBDD()
  439. {
  440. $connect = new PDO("pgsql:host=$this->host;dbname=$this->base", $this->user, $this->pass);
  441. $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  442. $connect->query("SET NAMES '$this->charset'");
  443.  
  444. return $connect;
  445. }
  446. }
  447.  
  448. FatalThrowableError in UserProvider.php line 17:
  449. 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