Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.97 KB | None | 0 0
  1. imports:
  2. - { resource: parameters.yml }
  3. - { resource: security.yml }
  4. - { resource: services.yml }
  5. - { resource: "@MedBundle/Resources/config/services.yml" }
  6.  
  7. # Put parameters here that don't need to change on each machine where the app is deployed
  8. # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
  9. parameters:
  10. locale: en
  11.  
  12. framework:
  13. secret: "%secret%"
  14. csrf_protection: ~
  15. form: ~
  16. session:
  17. handler_id: ~
  18. fos_user:
  19. db_driver: orm
  20. firewall_name: main
  21. user_class: AppBundleEntityUser
  22. from_email:
  23. address: "test@test.com"
  24. sender_name: "Demo String"
  25. service:
  26. mailer: fos_user.mailer.twig_swift
  27.  
  28. lexik_jwt_authentication:
  29. private_key_path: %jwt_private_key_path%
  30. public_key_path: %jwt_public_key_path%
  31. pass_phrase: %jwt_key_pass_phrase%
  32. token_ttl: %jwt_token_ttl%
  33.  
  34. nelmio_cors:
  35. defaults:
  36. allow_origin: ["%cors_allow_origin%"]
  37. allow_methods: ["POST", "PUT", "GET", "DELETE", "OPTIONS"]
  38. allow_headers: ["content-type", "authorization"]
  39. expose_headers: ["link"]
  40. max_age: 3600
  41. paths:
  42. '^/': ~
  43. # Doctrine Configuration
  44. doctrine:
  45. dbal:
  46. driver: pdo_mysql
  47. host: '%database_host%'
  48. port: '%database_port%'
  49. dbname: '%database_name%'
  50. user: '%database_user%'
  51. password: '%database_password%'
  52. charset: UTF8
  53. # if using pdo_sqlite as your database driver:
  54. # 1. add the path in parameters.yml
  55. # e.g. database_path: '%kernel.root_dir%/data/data.db3'
  56. # 2. Uncomment database_path in parameters.yml.dist
  57. # 3. Uncomment next line:
  58. #path: '%database_path%'
  59.  
  60. orm:
  61. auto_generate_proxy_classes: '%kernel.debug%'
  62. naming_strategy: doctrine.orm.naming_strategy.underscore
  63. auto_mapping: true
  64.  
  65. # Swiftmailer Configuration
  66. swiftmailer:
  67. transport: '%mailer_transport%'
  68. host: '%mailer_host%'
  69. username: '%mailer_user%'
  70. password: '%mailer_password%'
  71. spool: { type: memory }
  72.  
  73. fos_rest:
  74. view:
  75. view_response_listener: 'force'
  76. formats:
  77. json: true
  78. format_listener:
  79. rules:
  80. - { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: true }
  81. - { path: '^/', stop: true }
  82.  
  83. security:
  84. encoders:
  85. FOSUserBundleModelUserInterface: bcrypt
  86.  
  87. role_hierarchy:
  88. ROLE_ADMIN: ROLE_USER
  89. ROLE_SUPER_ADMIN: ROLE_ADMIN
  90.  
  91. providers:
  92. fos_userbundle:
  93. id: fos_user.user_provider.username
  94.  
  95. firewalls:
  96. dev:
  97. pattern: ^/(_(profiler|wdt)|css|images|js)/
  98. security: false
  99.  
  100. login:
  101. pattern: ^/api/login
  102. stateless: true
  103. anonymous: true
  104. form_login:
  105. check_path: /api/login_check
  106. success_handler: lexik_jwt_authentication.handler.authentication_success
  107. failure_handler: lexik_jwt_authentication.handler.authentication_failure
  108. require_previous_session: false
  109.  
  110. api:
  111. pattern: ^/api
  112. stateless: true
  113. lexik_jwt:
  114. authorization_header:
  115. enabled: true
  116. prefix: Bearer
  117. query_parameter:
  118. enabled: true
  119. name: bearer
  120. throw_exceptions: false
  121. create_entry_point: true
  122.  
  123. main:
  124. pattern: ^/
  125. provider: fos_userbundle
  126. stateless: true
  127. form_login:
  128. check_path: /login_check
  129. username_parameter: _username
  130. password_parameter: _password
  131. success_handler: lexik_jwt_authentication.handler.authentication_success
  132. failure_handler: lexik_jwt_authentication.handler.authentication_failure
  133. require_previous_session: false
  134. logout: true
  135. anonymous: true
  136.  
  137.  
  138. access_control:
  139. - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
  140. - { path: ^/api/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
  141. - { path: ^/api, role: IS_AUTHENTICATED_FULLY }
  142.  
  143. var app = angular.module("myApp", ["ngRoute","ngStorage"]);
  144.  
  145. app.config(['$routeProvider','$httpProvider', function($routeProvider,$httpProvider) {
  146. $routeProvider
  147. .when("/login", {
  148. templateUrl: Routing.generate('login',
  149. {template:"default/login.html.twig"}),
  150. controller: 'loginctrl',
  151. })
  152. .when("/register", {
  153. templateUrl: Routing.generate('register',
  154. {template:"default/registere.html.twig"}),
  155. controller: 'httpgetctrl',
  156. })
  157. .when("/affiche", {
  158. templateUrl: Routing.generate('get',
  159. {template:"default/affiche.html.twig"}),
  160. controller: 'httpgetctrl',
  161. });
  162. $httpProvider.interceptors.push('authInterceptor');
  163.  
  164. }]);
  165.  
  166. app.controller("httpgetctrl", function ($scope, $http) {
  167.  
  168. $scope.SendData = function () {
  169. // use $.param jQuery function to serialize data from JSON
  170. var data = $.param({
  171. username: $scope.username,
  172. email: $scope.email,
  173. pass: $scope.password,
  174. confir: $scope.confirmationpass
  175. });
  176.  
  177. var config = {
  178. headers : {
  179. 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
  180. }
  181. }
  182.  
  183. $http.post(Routing.generate('register'), data, config)
  184. .then(function (response) {
  185. console.log("success");
  186. })
  187. .catch(function() {
  188. console.log("error");
  189. })
  190. };
  191.  
  192. });
  193.  
  194. app.controller("loginctrl", function ($scope, $http,$localStorage) {
  195.  
  196. $scope.login = function () {
  197. // use $.param jQuery function to serialize data from JSON
  198. var data = $.param({
  199. username: $scope.user,
  200. password: $scope.pass
  201. });
  202.  
  203. var config = {
  204. headers : {
  205. 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
  206. }
  207. }
  208.  
  209. $http.post(Routing.generate('login'), data, config)
  210. .then(function (response) {
  211. console.log("success login");
  212. localStorage.token = response.data['token'] ;
  213. console.log(localStorage.token);
  214. })
  215. .catch(function() {
  216. console.log("error login");
  217. })
  218. };
  219.  
  220. });
  221.  
  222. app.factory('authInterceptor', function($rootScope, $q, $window) {
  223. return {
  224. request: function (config) {
  225. config.headers = config.headers || {};
  226.  
  227. if (localStorage.token) {
  228. //config.headers.Authorization = 'Bearer ' + localStorage.token;
  229. config.headers['x-access-token'] = localStorage.token;
  230. console.log('done');
  231. }
  232. return config;
  233. },
  234. response: function (response) {
  235. if (response.status === 401) {
  236. // if 401 unauthenticated
  237. console.log("error 401");
  238. }
  239. return response || $q.when(response);
  240. }
  241. };
  242. // call the factory ...
  243. })
  244.  
  245. {% extends 'base.html.twig' %}
  246. {% block title %} Acceuil {% endblock %}
  247. {% block stylesheets %}<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/cerulean/bootstrap.min.css">
  248. <style>
  249.  
  250. a {
  251. text-decoration:none;
  252. color: #e74c3c;
  253. margin-right: 25px;
  254.  
  255. }
  256.  
  257. </style>
  258.  
  259. {% endblock %}
  260. {% block body %}
  261. <div class="container" ng-app="myApp">
  262. <a href="#/!">Acceuil</a>
  263.  
  264. <a href="#!login">Login</a>
  265. <a href="#!register">Register</a>
  266. <a href="#!affiche">Affiche</a>
  267. <br><br>
  268.  
  269. <div ng-view></div>
  270.  
  271. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
  272. <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.min.js"></script>
  273. <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.5/angular-route.js"></script>
  274. <script src="angularJwt.js"></script>
  275. <script type="text/javascript" src="https://cdn.jsdelivr.net/ngstorage/0.3.6/ngStorage.min.js"></script>
  276. <script src="https://cdnjs.cloudflare.com/ajax/libs/todc-bootstrap/3.3.7-3.3.13/js/bootstrap.min.js"></script>
  277. <script type="text/javascript" src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
  278. <script type="text/javascript" src="{{ path('fos_js_routing_js', {"callback": "fos.Router.setData"}) }}"></script>
  279. <script type="text/javascript" src="{{ asset('bundles/fosjsrouting/js/app.js') }}"></script>
  280.  
  281. <script>
  282.  
  283.  
  284.  
  285. </script>
  286. </div>
  287. {% endblock %}
  288.  
  289. <?php
  290.  
  291. namespace MedBundleController;
  292.  
  293. use SensioBundleFrameworkExtraBundleConfigurationRoute;
  294. use SymfonyComponentHttpFoundationRequest;
  295. use SymfonyBundleFrameworkBundleControllerController;
  296. use FOSRestBundleControllerAnnotationsRouteResource;
  297. use SymfonyComponentHttpFoundationJsonResponse;
  298. use FOSRestBundleViewViewHandler;
  299. use FOSRestBundleViewView;
  300. use MedBundleEntityMed;
  301. use SymfonyComponentSecurityCoreUserUserInterface;
  302. use SymfonyComponentHttpFoundationResponse;
  303. use AppBundleEventListenerJWTCreatedlistener;
  304.  
  305. Class ApiController extends Controller {
  306.  
  307. public function getAction() {
  308.  
  309. $em = $this->getDoctrine()->getManager();
  310. $test = $em->getRepository('MedBundle:Med')->findAll();
  311. //$viewHandler = $this->get('fos_rest.view_handler');
  312.  
  313. // Création d'une vue FOSRestBundle
  314. //$view = View::create($test);
  315. //$view->setFormat('json');
  316.  
  317. // Gestion de la réponse
  318. //return $viewHandler->handle($view);
  319. return array('test'=>$test);
  320.  
  321. }
  322.  
  323.  
  324. public function loginAction() {
  325.  
  326. $em = $this->getDoctrine()->getManager();
  327. $request = $this->getRequest();
  328. $test = null; $token = null; $res = 'null';
  329. if ($request->getMethod() == 'POST') {
  330. $username = $request->request->get('username');
  331. $password = $request->request->get('password');
  332.  
  333. $test = $em->getRepository('MedBundle:Med')->findOneBy(array('username' => $username));
  334.  
  335. if (!($test)) { $res = "error"; } else{
  336. $salt= $test->getSalt();
  337. $pass = crypt($password,$salt);
  338. if ( $pass !== $test->getPassword() ) { $res='error password'; } else {
  339. $res='success';
  340.  
  341. $token = $this->get('lexik_jwt_authentication.jwt_manager')->create($test);
  342.  
  343. $test->setToken($token);
  344. $em->persist($test);
  345. $em->flush();
  346.  
  347. } }
  348. //return new Response($res);
  349. $test = [];
  350. $test['token'] = $token;
  351. $test['res'] = $res;
  352. return new JsonResponse($test);
  353. }
  354.  
  355. return $this->render('default/login.html.twig',array('test' => json_encode($test), 'token' => $token,'res' => json_encode($res)));
  356.  
  357. }
  358.  
  359.  
  360.  
  361. public function logoutAction() {
  362.  
  363.  
  364.  
  365. }
  366.  
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement