Ver código fonte

Anomalie #105: Ajax et utilisateur déconnecté

bastien 12 anos atrás
pai
commit
f8e32e7a1c

+ 15 - 1
src/Muzich/CoreBundle/Controller/CoreController.php Ver arquivo

@@ -142,7 +142,21 @@ class CoreController extends Controller
142 142
    *  Procédure d'ajout d'un element
143 143
    */
144 144
   public function elementAddAction($group_slug)
145
-  {    
145
+  {   
146
+    if ($this->getUser() == 'anon.')
147
+    {
148
+      if ($this->getRequest()->isXmlHttpRequest())
149
+      {
150
+        return $this->jsonResponse(array(
151
+          'status' => 'mustbeconnected'
152
+        ));
153
+      }
154
+      else
155
+      {
156
+        return $this->redirect($this->generateUrl('index'));
157
+      }
158
+    }
159
+    
146 160
     if ($this->getRequest()->getMethod() != 'POST')
147 161
     {
148 162
       throw $this->createNotFoundException('Cette ressource n\'est pas accessible');

+ 42 - 0
src/Muzich/CoreBundle/Controller/ElementController.php Ver arquivo

@@ -34,6 +34,20 @@ class ElementController extends Controller
34 34
    */
35 35
   public function editAction($element_id)
36 36
   {    
37
+    if ($this->getUser() == 'anon.')
38
+    {
39
+      if ($this->getRequest()->isXmlHttpRequest())
40
+      {
41
+        return $this->jsonResponse(array(
42
+          'status' => 'mustbeconnected'
43
+        ));
44
+      }
45
+      else
46
+      {
47
+        return $this->redirect($this->generateUrl('index'));
48
+      }
49
+    }
50
+    
37 51
     $element = $this->checkExistingAndOwned($element_id);
38 52
     
39 53
     $element_tags = $element->getTags();
@@ -77,6 +91,20 @@ class ElementController extends Controller
77 91
    */
78 92
   public function updateAction($element_id)
79 93
   {
94
+    if ($this->getUser() == 'anon.')
95
+    {
96
+      if ($this->getRequest()->isXmlHttpRequest())
97
+      {
98
+        return $this->jsonResponse(array(
99
+          'status' => 'mustbeconnected'
100
+        ));
101
+      }
102
+      else
103
+      {
104
+        return $this->redirect($this->generateUrl('index'));
105
+      }
106
+    }
107
+    
80 108
     $element = $this->checkExistingAndOwned($element_id);
81 109
     // Si il y a un groupe on le retire pour le bind
82 110
     $group = $element->getGroup();
@@ -145,6 +173,20 @@ class ElementController extends Controller
145 173
   
146 174
   public function removeAction($element_id)
147 175
   {
176
+    if ($this->getUser() == 'anon.')
177
+    {
178
+      if ($this->getRequest()->isXmlHttpRequest())
179
+      {
180
+        return $this->jsonResponse(array(
181
+          'status' => 'mustbeconnected'
182
+        ));
183
+      }
184
+      else
185
+      {
186
+        return $this->redirect($this->generateUrl('index'));
187
+      }
188
+    }
189
+    
148 190
     try {
149 191
       $element = $this->checkExistingAndOwned($element_id);
150 192
       $em = $this->getDoctrine()->getEntityManager();

+ 28 - 0
src/Muzich/CoreBundle/Controller/SearchController.php Ver arquivo

@@ -41,6 +41,20 @@ class SearchController extends Controller
41 41
    */
42 42
   public function searchElementsAction($id_limit = null, $invertcolors = false)
43 43
   {
44
+    if ($this->getUser() == 'anon.')
45
+    {
46
+      if ($this->getRequest()->isXmlHttpRequest())
47
+      {
48
+        return $this->jsonResponse(array(
49
+          'status' => 'mustbeconnected'
50
+        ));
51
+      }
52
+      else
53
+      {
54
+        return $this->redirect($this->generateUrl('index'));
55
+      }
56
+    }
57
+    
44 58
     $request = $this->getRequest();
45 59
     $search_object = $this->getElementSearcher();
46 60
     
@@ -200,6 +214,20 @@ class SearchController extends Controller
200 214
    */
201 215
   public function searchTagAction($string_search, $timestamp)
202 216
   {
217
+    if ($this->getUser() == 'anon.')
218
+    {
219
+      if ($this->getRequest()->isXmlHttpRequest())
220
+      {
221
+        return $this->jsonResponse(array(
222
+          'status' => 'mustbeconnected'
223
+        ));
224
+      }
225
+      else
226
+      {
227
+        return $this->redirect($this->generateUrl('index'));
228
+      }
229
+    }
230
+    
203 231
     if ($this->getRequest()->isXmlHttpRequest())
204 232
     {
205 233
       if (strlen($string_search) > 1)

+ 11 - 0
src/Muzich/CoreBundle/Resources/config/security.yml Ver arquivo

@@ -65,6 +65,17 @@ security:
65 65
         - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
66 66
         - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
67 67
         
68
+        ## Ouvert pour l'ajax (et le message de redirection en cas de déco)
69
+        - { path: ^/(?:fr|en)/search-elements, role: IS_AUTHENTICATED_ANONYMOUSLY }
70
+        - { path: ^/(?:fr|en)/element, role: IS_AUTHENTICATED_ANONYMOUSLY }
71
+        - { path: ^/(?:fr|en)/favorite/add, role: IS_AUTHENTICATED_ANONYMOUSLY }
72
+        - { path: ^/(?:fr|en)/favorite/remove, role: IS_AUTHENTICATED_ANONYMOUSLY }
73
+        - { path: ^/(?:fr|en)/element/add, role: IS_AUTHENTICATED_ANONYMOUSLY }
74
+        - { path: ^/(?:fr|en)/search/tag, role: IS_AUTHENTICATED_ANONYMOUSLY }
75
+        - { path: ^/(?:fr|en)/show/user/, role: IS_AUTHENTICATED_ANONYMOUSLY }
76
+        - { path: ^/(?:fr|en)/show/group/, role: IS_AUTHENTICATED_ANONYMOUSLY }
77
+        - { path: ^/(?:fr|en)/favoritesajax, role: IS_AUTHENTICATED_ANONYMOUSLY }
78
+                                
68 79
         - { path: ^/, roles: ROLE_USER }
69 80
         
70 81
 #        # Liste des pages accessibles à tous les utilisateurs (ne pas toucher)

+ 2 - 0
src/Muzich/CoreBundle/Resources/views/layout.html.twig Ver arquivo

@@ -27,6 +27,8 @@
27 27
     string_follow_stop = "{{ 'user.stop_follow'|trans({}, 'users') }}";
28 28
     string_follow_following = "{{ 'user.following'|trans({}, 'users') }}";
29 29
     string_follow_follow = "{{ 'user.follow'|trans({}, 'users') }}";
30
+    
31
+    url_index = "{{ path('index') }}";
30 32
   </script>
31 33
   {% block js %}{% endblock %}
32 34
   

+ 42 - 0
src/Muzich/FavoriteBundle/Controller/FavoriteController.php Ver arquivo

@@ -22,6 +22,20 @@ class FavoriteController extends Controller
22 22
    */
23 23
   public function addAction($id, $token)
24 24
   {
25
+    if ($this->getUser() == 'anon.')
26
+    {
27
+      if ($this->getRequest()->isXmlHttpRequest())
28
+      {
29
+        return $this->jsonResponse(array(
30
+          'status' => 'mustbeconnected'
31
+        ));
32
+      }
33
+      else
34
+      {
35
+        return $this->redirect($this->generateUrl('index'));
36
+      }
37
+    }
38
+    
25 39
     $user = $this->getUser();
26 40
     
27 41
     /**
@@ -86,6 +100,20 @@ class FavoriteController extends Controller
86 100
    */
87 101
   public function removeAction($id, $token)
88 102
   {
103
+    if ($this->getUser() == 'anon.')
104
+    {
105
+      if ($this->getRequest()->isXmlHttpRequest())
106
+      {
107
+        return $this->jsonResponse(array(
108
+          'status' => 'mustbeconnected'
109
+        ));
110
+      }
111
+      else
112
+      {
113
+        return $this->redirect($this->generateUrl('index'));
114
+      }
115
+    }
116
+    
89 117
     $user = $this->getUser();
90 118
     $em = $this->getDoctrine()->getEntityManager();
91 119
     
@@ -193,6 +221,20 @@ class FavoriteController extends Controller
193 221
   
194 222
   public function getElementsAction($user_id, $tags_ids_json, $id_limit = null, $invert = false)
195 223
   {
224
+    if ($this->getUser() == 'anon.')
225
+    {
226
+      if ($this->getRequest()->isXmlHttpRequest())
227
+      {
228
+        return $this->jsonResponse(array(
229
+          'status' => 'mustbeconnected'
230
+        ));
231
+      }
232
+      else
233
+      {
234
+        return $this->redirect($this->generateUrl('index'));
235
+      }
236
+    }
237
+    
196 238
     $tag_ids = json_decode($tags_ids_json);
197 239
     $search_object = new ElementSearcher();
198 240
     

+ 1 - 1
src/Muzich/FavoriteBundle/Resources/config/routing.yml Ver arquivo

@@ -16,5 +16,5 @@ favorite_user_list:
16 16
   defaults: { _controller: MuzichFavoriteBundle:Favorite:userList }
17 17
 
18 18
 favorite_get:
19
-  pattern:   /favorites/{user_id}/getElements/{tags_ids_json}/{id_limit}/{invert}
19
+  pattern:   /favoritesajax/{user_id}/getElements/{tags_ids_json}/{id_limit}/{invert}
20 20
   defaults: { _controller: MuzichFavoriteBundle:Favorite:getElements, id_limit: null, invert: false }

+ 14 - 0
src/Muzich/HomeBundle/Controller/ShowController.php Ver arquivo

@@ -96,6 +96,20 @@ class ShowController extends Controller
96 96
   
97 97
   public function getElementsAction($type, $object_id, $tags_ids_json, $id_limit = null, $invert = false)
98 98
   {
99
+    if ($this->getUser() == 'anon.')
100
+    {
101
+      if ($this->getRequest()->isXmlHttpRequest())
102
+      {
103
+        return $this->jsonResponse(array(
104
+          'status' => 'mustbeconnected'
105
+        ));
106
+      }
107
+      else
108
+      {
109
+        return $this->redirect($this->generateUrl('index'));
110
+      }
111
+    }
112
+    
99 113
     $object_id = null;
100 114
     if ($type != 'user' && $type != 'group')
101 115
     {

+ 69 - 25
web/bundles/muzichcore/js/muzich.js Ver arquivo

@@ -5,23 +5,6 @@
5 5
  * 
6 6
  */
7 7
 
8
-// Controle du focus sur la page
9
-function onBlur() {
10
-  document.body.className = 'blurred';
11
-}
12
-
13
-function onFocus(){
14
-    document.body.className = 'focused';
15
-}
16
-
17
-if (/*@cc_on!@*/false) { // check for Internet Explorer
18
-    document.onfocusin = onFocus;
19
-    document.onfocusout = onBlur;
20
-} else {
21
-    window.onfocus = onFocus;
22
-    window.onblur = onBlur;
23
-}
24
-
25 8
 // Messages flashs
26 9
 var myMessages = ['info','warning','error','success']; // define the messages types	
27 10
 
@@ -230,6 +213,22 @@ function explode (delimiter, string, limit) {
230 213
 
231 214
 $(document).ready(function(){
232 215
     
216
+  // Controle du focus sur la page
217
+  function onBlur() {
218
+    document.body.className = 'blurred';
219
+  }
220
+
221
+  function onFocus(){
222
+      document.body.className = 'focused';
223
+  }
224
+
225
+  if (/*@cc_on!@*/false) { // check for Internet Explorer
226
+      document.onfocusin = onFocus;
227
+      document.onfocusout = onBlur;
228
+  } else {
229
+      window.onfocus = onFocus;
230
+      window.onblur = onBlur;
231
+  }
233 232
   
234 233
   // Bouton de personalisation du filtre
235 234
   // pour le moment ce ne sotn que des redirection vers des actions
@@ -312,14 +311,19 @@ $(document).ready(function(){
312 311
 
313 312
   // Mise en favoris
314 313
   $('a.favorite_link').live("click", function(){
315
-     link = $(this);
316
-     $.getJSON($(this).attr('href'), function(response) {
317
-       img = link.find('img');
318
-       link.attr('href', response.link_new_url);
319
-       img.attr('src', response.img_new_src);
320
-       img.attr('title', response.img_new_title);
321
-     });
322
-     return false;
314
+    link = $(this);
315
+    $.getJSON($(this).attr('href'), function(response) {
316
+      if (response.status == 'mustbeconnected')
317
+      {
318
+        $(location).attr('href', url_index);
319
+      }
320
+      
321
+      img = link.find('img');
322
+      link.attr('href', response.link_new_url);
323
+      img.attr('src', response.img_new_src);
324
+      img.attr('title', response.img_new_title);
325
+    });
326
+    return false;
323 327
   });
324 328
     
325 329
   // Affichage du bouton Modifier et Supprimer
@@ -358,6 +362,11 @@ $(document).ready(function(){
358 362
      }
359 363
      $('img.elements_more_loader').show();
360 364
      $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
365
+       if (response.status == 'mustbeconnected')
366
+        {
367
+          $(location).attr('href', url_index);
368
+        }
369
+       
361 370
        if (response.count)
362 371
        {
363 372
          $('ul.elements').append(response.html);
@@ -386,6 +395,11 @@ $(document).ready(function(){
386 395
   
387 396
   $('form[name="search"]').ajaxForm(function(response) { 
388 397
     
398
+    if (response.status == 'mustbeconnected')
399
+    {
400
+      $(location).attr('href', url_index);
401
+    }
402
+    
389 403
     $('ul.elements').html(response.html);
390 404
     
391 405
     if (response.count)
@@ -418,6 +432,11 @@ $(document).ready(function(){
418 432
       li = link.parent('td').parent('tr').parent().parent().parent('li.element');
419 433
       li.find('img.element_loader').show();
420 434
       $.getJSON(link.attr('href'), function(response){
435
+        if (response.status == 'mustbeconnected')
436
+        {
437
+          $(location).attr('href', url_index);
438
+        }
439
+        
421 440
         if (response.status == 'success')
422 441
         {
423 442
           li.remove();
@@ -458,6 +477,11 @@ $(document).ready(function(){
458 477
     
459 478
     $.getJSON($(this).attr('href'), function(response) {
460 479
       
480
+      if (response.status == 'mustbeconnected')
481
+      {
482
+        $(location).attr('href', url_index);
483
+      }
484
+      
461 485
       // On prépare le tagBox
462 486
       li.html(response.html);
463 487
      
@@ -476,6 +500,11 @@ $(document).ready(function(){
476 500
       });
477 501
       $('form[name="'+response.form_name+'"]').ajaxForm(function(response){
478 502
         
503
+        if (response.status == 'mustbeconnected')
504
+        {
505
+          $(location).attr('href', url_index);
506
+        }
507
+        
479 508
         if (response.status == 'success')
480 509
         {
481 510
           li.html(response.html);
@@ -567,6 +596,11 @@ $(document).ready(function(){
567 596
 
568 597
       // Récupération des tags correspondants
569 598
       $.getJSON('/app_dev.php/fr/search/tag/'+input.val()+'/'+ajax_query_timestamp, function(data) {
599
+        if (data.status == 'mustbeconnected')
600
+        {
601
+          $(location).attr('href', url_index);
602
+        }
603
+        
570 604
         // Ce contrôle permet de ne pas continuer si une requete
571 605
         // ajax a été faite depuis.
572 606
         if (data.timestamp == ajax_query_timestamp)
@@ -768,6 +802,11 @@ $(document).ready(function(){
768 802
     $('form[name="add"]').find('img.tag_loader').show();
769 803
   });
770 804
   $('form[name="add"]').ajaxForm(function(response) {
805
+    if (response.status == 'mustbeconnected')
806
+    {
807
+      $(location).attr('href', url_index);
808
+    }
809
+    
771 810
     $('form[name="add"] img.tag_loader').hide();
772 811
     if (response.status == 'success')
773 812
     {
@@ -812,6 +851,11 @@ $(document).ready(function(){
812 851
     $('img.elements_more_loader').show();
813 852
     $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
814 853
       
854
+      if (response.status == 'mustbeconnected')
855
+      {
856
+        $(location).attr('href', url_index);
857
+      }
858
+      
815 859
       $('ul.elements').html(response.html);
816 860
       
817 861
       if (response.count)