|
@@ -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
|
+}
|