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

Evolution #404: Inscription: confirmation de l'email et conséquences

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

+ 103 - 0
src/Muzich/CoreBundle/Security/Context.php Прегледај датотеку

@@ -0,0 +1,103 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Security;
4
+
5
+use Muzich\CoreBundle\Entity\User;
6
+
7
+class Context
8
+{
9
+  
10
+  const ACTION_ELEMENT_ADD = 0;
11
+  const ACTION_ELEMENT_NOTE = 1;
12
+  const ACTION_ELEMENT_ALERT = 2;
13
+  const ACTION_ELEMENT_ADD_TO_FAVORITES = 3;
14
+  const ACTION_ELEMENT_TAGS_PROPOSITION = 4;
15
+  const ACTION_GROUP_ADD = 5;
16
+  const ACTION_COMMENT_ADD = 6;
17
+  const ACTION_COMMENT_ALERT = 7;
18
+  const ACTION_USER_FOLLOW = 8;
19
+  const ACTION_TAG_ADD = 9;
20
+  
21
+  const AFFECT_CANT_MAKE = 0;
22
+  const AFFECT_NO_SCORING = 1;
23
+  
24
+  const CONDITION_USER_EMAIL_NOT_CONFIRMED = 'UserEmailNotConfirmed';
25
+  
26
+  static $affecteds_actions = array(
27
+    self::AFFECT_CANT_MAKE => array(
28
+      self::ACTION_ELEMENT_ADD,
29
+      self::ACTION_ELEMENT_NOTE,
30
+      self::ACTION_COMMENT_ALERT,
31
+      self::ACTION_ELEMENT_ALERT,
32
+      self::ACTION_TAG_ADD,
33
+      self::ACTION_ELEMENT_TAGS_PROPOSITION,
34
+      self::ACTION_GROUP_ADD
35
+    ),
36
+    self::AFFECT_NO_SCORING => array(
37
+      self::ACTION_ELEMENT_NOTE,
38
+      self::ACTION_ELEMENT_ADD_TO_FAVORITES,
39
+      self::ACTION_ELEMENT_TAGS_PROPOSITION,
40
+      self::ACTION_USER_FOLLOW
41
+    )
42
+  );
43
+  
44
+  static $affecteds_conditions = array(
45
+    self::AFFECT_CANT_MAKE => array(
46
+      self::CONDITION_USER_EMAIL_NOT_CONFIRMED
47
+    ),
48
+    self::AFFECT_NO_SCORING => array(
49
+      self::CONDITION_USER_EMAIL_NOT_CONFIRMED
50
+    )
51
+  );
52
+  
53
+  private $user;
54
+  
55
+  public function __construct(User $user)
56
+  {
57
+    $this->user = $user;
58
+  }
59
+  
60
+  public function canMakeAction($action)
61
+  {
62
+    if ($this->actionIsAffectedBy(self::AFFECT_CANT_MAKE, $action))
63
+      return false;
64
+    return true;
65
+  }
66
+  
67
+  protected function actionCanBeAffectedBy($affect, $action)
68
+  {
69
+    if (!array_key_exists($affect, self::$affecteds_actions))
70
+      throw new \Exception("Unknow action $action");
71
+    
72
+    if (in_array($action, self::$affecteds_actions[$affect]))
73
+      return true;
74
+    return false;
75
+  }
76
+  
77
+  public function actionIsAffectedBy($affect, $action)
78
+  {
79
+    if ($this->actionCanBeAffectedBy($affect, $action))
80
+    {
81
+      foreach (self::$affecteds_conditions[$affect] as $affected_condition)
82
+      {
83
+        $affected_condition_method = 'is'.$affected_condition;
84
+        if ($this->$affected_condition_method())
85
+        {
86
+          return true;
87
+        }
88
+      }
89
+    }
90
+    
91
+    return false;
92
+  }
93
+  
94
+  protected function isUserEmailNotConfirmed()
95
+  {
96
+    if ($this->user->isEmailConfirmed())
97
+    {
98
+      return false;
99
+    }
100
+    return true;
101
+  }
102
+  
103
+}

+ 53 - 0
src/Muzich/CoreBundle/Tests/Security/ContextTest.php Прегледај датотеку

@@ -0,0 +1,53 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Context;
4
+
5
+use Muzich\CoreBundle\Entity\User;
6
+use Muzich\CoreBundle\Security\Context as SecurityContext;
7
+
8
+class ContextTest extends \PHPUnit_Framework_TestCase
9
+{
10
+  
11
+  public function testActionsWithNotConfirmedEmailUser()
12
+  {
13
+    $secutiry_context = new SecurityContext(new User());
14
+    
15
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
16
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));
17
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ALERT));
18
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ALERT));
19
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_TAG_ADD));
20
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
21
+    $this->assertFalse($secutiry_context->canMakeAction(SecurityContext::ACTION_GROUP_ADD));
22
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_USER_FOLLOW));
23
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
24
+    
25
+    $this->assertTrue($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_NOTE));
26
+    $this->assertTrue($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
27
+    $this->assertTrue($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_USER_FOLLOW));
28
+    $this->assertTrue($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
29
+  }
30
+  
31
+  public function testActionsWithConfirmedEmailUser()
32
+  {
33
+    $user_email_confirmed = new User();
34
+    $user_email_confirmed->setEmailConfirmed(true);
35
+    $secutiry_context = new SecurityContext($user_email_confirmed);
36
+    
37
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD));
38
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_NOTE));
39
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_COMMENT_ALERT));
40
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ALERT));
41
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_TAG_ADD));
42
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
43
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_GROUP_ADD));
44
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_USER_FOLLOW));
45
+    $this->assertTrue($secutiry_context->canMakeAction(SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
46
+    
47
+    $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_NOTE));
48
+    $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_TAGS_PROPOSITION));
49
+    $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_USER_FOLLOW));
50
+    $this->assertFalse($secutiry_context->actionIsAffectedBy(SecurityContext::AFFECT_NO_SCORING, SecurityContext::ACTION_ELEMENT_ADD_TO_FAVORITES));
51
+  }
52
+  
53
+}