Преглед изворни кода

Evolution #414: Navigation en mode non inscrit

Sevajol Bastien пре 11 година
родитељ
комит
380a04abcf

+ 2 - 0
app/Resources/translations/navigationui.en.yml Прегледај датотеку

@@ -10,6 +10,8 @@ container_menu:
10 10
 top_bar:
11 11
   my_account:           My account
12 12
   logout:               Logout
13
+  subscribe:            Subscribe
14
+  login:                Connexion
13 15
   
14 16
 network:
15 17
   mygroups:             My groups

+ 2 - 0
app/Resources/translations/navigationui.fr.yml Прегледај датотеку

@@ -10,6 +10,8 @@ container_menu:
10 10
 top_bar:
11 11
   my_account:           Mon compte
12 12
   logout:               Déconnexion
13
+  subscribe:            Devenir membre
14
+  login:                Connexion
13 15
   
14 16
 network:
15 17
   mygroups:             Mes groupes

+ 4 - 2
src/Muzich/CoreBundle/Resources/config/security.yml Прегледај датотеку

@@ -11,7 +11,7 @@ security:
11 11
             algorithm: sha512
12 12
             encode_as_base64: false
13 13
             iterations: 10
14
-
14
+    
15 15
     firewalls:
16 16
         main:
17 17
             pattern:      .*
@@ -22,10 +22,12 @@ security:
22 22
                 check_path:     /login_check
23 23
                 failure_path:   null
24 24
                 default_target_path: home
25
+                success_handler: security.authentication.success_handler
26
+                failure_handler: security.authentication.failure_handler
25 27
                 
26 28
             logout:
27 29
                 path:   /logout
28
-                target: /home
30
+                target: /
29 31
             anonymous:  ~
30 32
             remember_me:
31 33
               key:      %secret%

+ 62 - 18
src/Muzich/CoreBundle/Resources/public/js/muzich.js Прегледај датотеку

@@ -2879,24 +2879,10 @@ $(document).ready(function(){
2879 2879
   
2880 2880
   /* HELPBOX */
2881 2881
   $('.helpbox').live('click', function(){
2882
-    $('body').append(
2883
-      '<div id="helpbox" class="popin_block"><img src="/bundles/muzichcore/img/ajax-loader.gif" alt="loading..." /></div>'
2884
-    );
2885
-    open_popin_dialog('helpbox');
2886
-    JQueryJson($(this).attr('href'), {}, function(response){
2887
-      if (response.status == 'success')
2888
-      {
2889
-        $('div#helpbox').html(
2890
-          '<a href="javascript:void(0);" id="helpbox_close" >'+
2891
-            '<img src="/bundles/muzichcore/img/1317386146_cancel.png" alt="close" />'+
2892
-          '</a>'+
2893
-          response.data
2894
-        );
2895
-      }
2896
-    });
2897
-    
2882
+    open_ajax_popin($(this).attr('href'));
2898 2883
     return false;
2899 2884
   });
2885
+  
2900 2886
   $('a#helpbox_close').live('click', function(){
2901 2887
     // Fond gris
2902 2888
     $('#fade').fadeOut(1000, function(){$('#fade').remove();});
@@ -2918,14 +2904,72 @@ $(document).ready(function(){
2918 2904
     $('a.mustbeconnected').off('click').on('click',function(){
2919 2905
       open_connection_or_subscription_window();
2920 2906
     });
2907
+    
2908
+    $('a.open_login').click(function(){
2909
+      open_connection_or_subscription_window(true);
2910
+    });
2921 2911
    
2922 2912
 });
2923 2913
 
2924
-function open_connection_or_subscription_window()
2914
+function open_ajax_popin(url, callback)
2915
+{
2916
+  $('body').append(
2917
+    '<div id="helpbox" class="popin_block"><img src="/bundles/muzichcore/img/ajax-loader.gif" alt="loading..." /></div>'
2918
+  );
2919
+  open_popin_dialog('helpbox');
2920
+  JQueryJson(url, {}, function(response){
2921
+    if (response.status == 'success')
2922
+    {
2923
+      $('div#helpbox').html(
2924
+        '<a href="javascript:void(0);" id="helpbox_close" >'+
2925
+          '<img src="/bundles/muzichcore/img/1317386146_cancel.png" alt="close" />'+
2926
+        '</a>'+
2927
+        response.data
2928
+      );
2929
+      
2930
+      if (callback)
2931
+      {
2932
+        callback();
2933
+      }
2934
+    }
2935
+  });
2936
+}
2937
+
2938
+function open_connection_or_subscription_window(open_login_part)
2925 2939
 {
2926 2940
   if (window_login_or_subscription_opened == false)
2927 2941
   {
2928 2942
     window_login_or_subscription_opened = true;
2929
-    alert('must be connected');
2943
+    open_ajax_popin(url_subscription_or_login, function(){
2944
+      if (open_login_part)
2945
+      {
2946
+        $('div#helpbox div#login_box').show();
2947
+      }
2948
+      else
2949
+      {
2950
+        $('div#helpbox div#registration_box').show();
2951
+      }
2952
+      
2953
+      $('a#helpbox_close').click(function(){
2954
+        window_login_or_subscription_opened = false;
2955
+      });
2956
+      
2957
+      $('div.login form').ajaxForm(function(response) {
2958
+        if (response.status == 'success')
2959
+        {
2960
+          $(location).attr('href', url_home);
2961
+        }
2962
+        else if (response.status == 'error')
2963
+        {
2964
+          $('div.login form').prepend('<ul class="error_list"><li>'+response.data.error+'</li></ul>');
2965
+          $('div.login form input#password').val('');
2966
+        }
2967
+      });
2968
+      
2969
+      $('div.register form.fos_user_registration_register').ajaxForm(function(response) {
2970
+        
2971
+      });
2972
+      
2973
+    });
2930 2974
   }
2931 2975
 }

+ 3 - 0
src/Muzich/CoreBundle/Resources/views/Layout/head_js.html.twig Прегледај датотеку

@@ -108,6 +108,7 @@
108 108
   string_removefromgroup_confirm_no = "{{ 'element.group.remove_from.confirm.no'|trans({}, 'elements') }}";
109 109
 
110 110
   url_index = "{{ path('index') }}";
111
+  url_home = "{{ path('home') }}";
111 112
   url_search_tag = "{{ path('search_tag') }}";
112 113
   url_get_favorites_tags = "{{ path('ajax_get_favorites_tags') }}";
113 114
   url_add_tag = "{{ path('ajax_add_tag') }}";
@@ -121,6 +122,8 @@
121 122
   url_img_ajax_loader = "{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}";
122 123
   url_get_embed_for_element = "{{ path('url_get_embed_for_element') }}";
123 124
   
125
+  url_subscription_or_login = "{{ path('subscribe_or_login') }}";
126
+  
124 127
   config_player_youtube_width = "{{ player_config_youtube_width }}";
125 128
   config_player_youtube_height = "{{ player_config_youtube_height }}";
126 129
 </script>

+ 10 - 1
src/Muzich/CoreBundle/Resources/views/Menu/access.html.twig Прегледај датотеку

@@ -1,6 +1,15 @@
1 1
 <ul id="register_and_login">
2 2
   {% if not app.user %}
3
-    
3
+    <li>
4
+      <a href="#" class="left mustbeconnected">
5
+        {{ 'top_bar.subscribe'|trans({}, 'navigationui') }}
6
+      </a>
7
+    </li>
8
+    <li>
9
+      <a class="right open_login" href="#">
10
+          {{ 'top_bar.login'|trans({}, 'navigationui') }}
11
+      </a>
12
+    </li>
4 13
   {% else %}
5 14
     <li>
6 15
       <a href="{{ path('my_account') }}" class="left">

+ 60 - 0
src/Muzich/CoreBundle/Security/Http/Authentication/AuthenticationFailureHandler.php Прегледај датотеку

@@ -0,0 +1,60 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Security\Http\Authentication;
4
+
5
+use Symfony\Component\Security\Core\Exception\AuthenticationException;
6
+use Symfony\Component\HttpFoundation\Request;
7
+use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
8
+use Symfony\Component\HttpFoundation\RedirectResponse;
9
+use Symfony\Component\Routing\RouterInterface;
10
+use Symfony\Component\HttpFoundation\Response;
11
+use Symfony\Bundle\FrameworkBundle\Translation\Translator;
12
+use Doctrine\ORM\EntityManager;
13
+use Muzich\CoreBundle\Entity\User;
14
+
15
+/**
16
+ * Custom authentication success handler
17
+ */
18
+class AuthenticationFailureHandler implements AuthenticationFailureHandlerInterface
19
+{
20
+
21
+   private $router;
22
+   private $em;
23
+   private $translator;
24
+
25
+   /**
26
+    * Constructor
27
+    * @param RouterInterface   $router
28
+    * @param EntityManager     $em
29
+    */
30
+   public function __construct(RouterInterface $router, EntityManager $em, Translator $translator)
31
+   {
32
+      $this->translator = $translator;
33
+      $this->router = $router;
34
+      $this->em = $em;
35
+   }
36
+
37
+   function onAuthenticationFailure(Request $request, AuthenticationException $exception)
38
+   {
39
+      if ($request->isXmlHttpRequest())
40
+      {
41
+        $response = new Response(json_encode($this->getResponseParameters($request)));
42
+        $response->headers->set('Content-Type', 'application/json; charset=utf-8');
43
+        return $response;
44
+      }
45
+      
46
+      return new RedirectResponse($this->router->generate('index'));
47
+   }
48
+   
49
+   protected function getResponseParameters(Request $request)
50
+   {
51
+    $session = $request->getSession();
52
+    return array(
53
+      'status' => 'error',
54
+      'data'   => array(
55
+        'error' => $this->translator->trans('login.fail', array(), 'users')
56
+      )
57
+    );
58
+   }
59
+   
60
+}

+ 52 - 0
src/Muzich/CoreBundle/Security/Http/Authentication/AuthenticationSuccessHandler.php Прегледај датотеку

@@ -0,0 +1,52 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Security\Http\Authentication;
4
+
5
+use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
+use Symfony\Component\HttpFoundation\Request;
7
+use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
8
+use Symfony\Component\HttpFoundation\RedirectResponse;
9
+use Symfony\Component\Routing\RouterInterface;
10
+use Symfony\Component\HttpFoundation\Response;
11
+use Doctrine\ORM\EntityManager;
12
+use Muzich\CoreBundle\Entity\User;
13
+
14
+/**
15
+ * Custom authentication success handler
16
+ */
17
+class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface
18
+{
19
+
20
+   private $router;
21
+   private $em;
22
+
23
+   /**
24
+    * Constructor
25
+    * @param RouterInterface   $router
26
+    * @param EntityManager     $em
27
+    */
28
+   public function __construct(RouterInterface $router, EntityManager $em)
29
+   {
30
+      $this->router = $router;
31
+      $this->em = $em;
32
+   }
33
+
34
+   /**
35
+    * This is called when an interactive authentication attempt succeeds. This
36
+    * is called by authentication listeners inheriting from AbstractAuthenticationListener.
37
+    * @param Request        $request
38
+    * @param TokenInterface $token
39
+    * @return Response The response to return
40
+    */
41
+   function onAuthenticationSuccess(Request $request, TokenInterface $token)
42
+   {
43
+      if ($request->isXmlHttpRequest())
44
+      {
45
+        $response = new Response(json_encode(array('status' => 'success')));
46
+        $response->headers->set('Content-Type', 'application/json; charset=utf-8');
47
+        return $response;
48
+      }
49
+      
50
+      return new RedirectResponse($this->router->generate('home'));
51
+   }
52
+}

+ 2 - 4
src/Muzich/IndexBundle/Controller/IndexController.php Прегледај датотеку

@@ -14,7 +14,6 @@ class IndexController extends Controller
14 14
   
15 15
   /**
16 16
    * 
17
-   * @Template()
18 17
    */
19 18
   public function indexAction()
20 19
   {
@@ -28,10 +27,10 @@ class IndexController extends Controller
28 27
     $vars = $this->proceedLogin();
29 28
     $form = $this->container->get('fos_user.registration.form');
30 29
     
31
-    return array_merge($vars, array(
30
+    return $this->render('MuzichIndexBundle:Index:index.html.twig', array_merge($vars, array(
32 31
       'form' => $form->createView(),
33 32
       'presubscription_form' => $this->getPreSubscriptionForm()->createView()
34
-    ));
33
+    )));
35 34
   }
36 35
   
37 36
   /**
@@ -57,7 +56,6 @@ class IndexController extends Controller
57 56
     }
58 57
 
59 58
     if ($error) {
60
-        // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
61 59
         $error = $this->trans('login.fail', array(), 'users');
62 60
     }
63 61
     // last username entered by the user

+ 10 - 0
src/Muzich/UserBundle/Controller/UserController.php Прегледај датотеку

@@ -606,4 +606,14 @@ class UserController extends Controller
606 606
     ));
607 607
   }
608 608
   
609
+  public function subscribeOrLoginAction(Request $request)
610
+  {
611
+    return $this->jsonResponse(array(
612
+      'status' => 'success',
613
+      'data'   => $this->render('MuzichUserBundle:Account:subscribe_or_login.html.twig', array(
614
+        'form' => $this->container->get('fos_user.registration.form')->createView()
615
+      ))->getContent()
616
+    ));
617
+  }
618
+  
609 619
 }

+ 5 - 1
src/Muzich/UserBundle/Resources/config/routing.yml Прегледај датотеку

@@ -59,4 +59,8 @@ user_hide_help:
59 59
   pattern: /account/update-help/hide/{help_id}/{token}
60 60
   defaults: { _controller: MuzichUserBundle:User:updateHelpViewed }
61 61
   requirements:
62
-    _method:  POST
62
+    _method:  POST
63
+
64
+subscribe_or_login:
65
+  pattern: /ajax/subscribe-or-login
66
+  defaults: { _controller: MuzichUserBundle:User:subscribeOrLogin }

+ 13 - 0
src/Muzich/UserBundle/Resources/views/Account/subscribe_or_login.html.twig Прегледај датотеку

@@ -0,0 +1,13 @@
1
+<div id="login_box" style="display: none;">
2
+  
3
+  {% include "MuzichUserBundle:Security:login.html.twig" %}
4
+  
5
+</div>
6
+
7
+<div id="registration_box" style="display: none;">
8
+  
9
+  <h1>{{ 'register.title'|trans({}, 'userui') }}</h1>
10
+  
11
+  {% include "MuzichUserBundle:Registration:register.html.twig" %}
12
+  
13
+</div>

+ 4 - 0
src/Muzich/UserBundle/Resources/views/Registration/register.html.twig Прегледај датотеку

@@ -1,3 +1,7 @@
1
+{% if registration_errors_pers is not defined %}
2
+  {% set registration_errors_pers = null %}
3
+{% endif %}
4
+
1 5
 <div class="register">
2 6
   <form action="{{ path('register') }}" {{ form_enctype(form) }} 
3 7
     method="POST" 

+ 7 - 0
src/Muzich/UserBundle/Resources/views/Security/login.html.twig Прегледај датотеку

@@ -1,3 +1,10 @@
1
+{% if error is not defined %}
2
+  {% set error = null %}
3
+{% endif %}
4
+{% if last_username is not defined %}
5
+  {% set last_username = null %}
6
+{% endif %}
7
+
1 8
 <div class="login">
2 9
   {% if error %}
3 10
       <ul class="error_list"><li>{{ error|trans({}, 'validators') }}</li></ul>