Context.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Muzich\CoreBundle\Security;
  3. use Muzich\CoreBundle\Entity\User;
  4. class Context
  5. {
  6. const ACTION_ELEMENT_ADD = 0;
  7. const ACTION_ELEMENT_NOTE = 1;
  8. const ACTION_ELEMENT_ALERT = 2;
  9. const ACTION_ELEMENT_ADD_TO_FAVORITES = 3;
  10. const ACTION_ELEMENT_TAGS_PROPOSITION = 4;
  11. const ACTION_GROUP_ADD = 5;
  12. const ACTION_COMMENT_ADD = 6;
  13. const ACTION_COMMENT_ALERT = 7;
  14. const ACTION_USER_FOLLOW = 8;
  15. const ACTION_TAG_ADD = 9;
  16. const ACTION_GET_FAVORITES_TAGS = 10;
  17. const AFFECT_CANT_MAKE = 0;
  18. const AFFECT_NO_SCORING = 1;
  19. const CONDITION_USER_EMAIL_NOT_CONFIRMED = 'UserEmailNotConfirmed';
  20. const CONDITION_USER_NOT_CONNECTED = 'UserNotConnected';
  21. static $affecteds_actions = array(
  22. self::AFFECT_CANT_MAKE => array(
  23. self::ACTION_ELEMENT_ADD => array(
  24. self::CONDITION_USER_NOT_CONNECTED,
  25. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  26. ),
  27. self::ACTION_ELEMENT_NOTE => array(
  28. self::CONDITION_USER_NOT_CONNECTED,
  29. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  30. ),
  31. self::ACTION_COMMENT_ALERT => array(
  32. self::CONDITION_USER_NOT_CONNECTED,
  33. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  34. ),
  35. self::ACTION_ELEMENT_ALERT => array(
  36. self::CONDITION_USER_NOT_CONNECTED,
  37. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  38. ),
  39. self::ACTION_TAG_ADD => array(
  40. self::CONDITION_USER_NOT_CONNECTED,
  41. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  42. ),
  43. self::ACTION_ELEMENT_TAGS_PROPOSITION => array(
  44. self::CONDITION_USER_NOT_CONNECTED,
  45. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  46. ),
  47. self::ACTION_GROUP_ADD => array(
  48. self::CONDITION_USER_NOT_CONNECTED,
  49. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  50. ),
  51. self::ACTION_ELEMENT_ADD_TO_FAVORITES => array(
  52. self::CONDITION_USER_NOT_CONNECTED
  53. ),
  54. self::ACTION_COMMENT_ADD => array(
  55. self::CONDITION_USER_NOT_CONNECTED
  56. ),
  57. self::ACTION_USER_FOLLOW => array(
  58. self::CONDITION_USER_NOT_CONNECTED
  59. ),
  60. self::ACTION_GET_FAVORITES_TAGS => array(
  61. self::CONDITION_USER_NOT_CONNECTED
  62. )
  63. ),
  64. self::AFFECT_NO_SCORING => array(
  65. self::ACTION_ELEMENT_NOTE => array(
  66. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  67. ),
  68. self::ACTION_ELEMENT_ADD_TO_FAVORITES => array(
  69. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  70. ),
  71. self::ACTION_ELEMENT_TAGS_PROPOSITION => array(
  72. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  73. ),
  74. self::ACTION_USER_FOLLOW => array(
  75. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  76. )
  77. )
  78. );
  79. private $user;
  80. private $anonymous = false;
  81. public function __construct($user)
  82. {
  83. if ($user instanceof User)
  84. {
  85. $this->user = $user;
  86. }
  87. else if ($user == 'anon.')
  88. {
  89. $this->user = new User();
  90. $this->anonymous = true;
  91. }
  92. else
  93. {
  94. throw new \Exception('Unable to determine type of user');
  95. }
  96. }
  97. public function canMakeAction($action)
  98. {
  99. if ($this->actionIsAffectedBy(self::AFFECT_CANT_MAKE, $action) !== false)
  100. return false;
  101. return true;
  102. }
  103. protected function actionCanBeAffectedBy($affect, $action)
  104. {
  105. if (!array_key_exists($affect, self::$affecteds_actions))
  106. throw new \Exception("Unknow action $action");
  107. if (array_key_exists($action, self::$affecteds_actions[$affect]))
  108. return true;
  109. return false;
  110. }
  111. public function actionIsAffectedBy($affect, $action)
  112. {
  113. if ($this->actionCanBeAffectedBy($affect, $action))
  114. {
  115. foreach (self::$affecteds_actions[$affect][$action] as $affected_condition)
  116. {
  117. if ($this->userIsInThisCondition($affected_condition))
  118. {
  119. return $affected_condition;
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. public function userIsInThisCondition($condition)
  126. {
  127. $affected_condition_method = 'is'.$condition;
  128. if ($this->$affected_condition_method())
  129. {
  130. return true;
  131. }
  132. return false;
  133. }
  134. protected function isUserNotConnected()
  135. {
  136. if ($this->anonymous)
  137. {
  138. return true;
  139. }
  140. return false;
  141. }
  142. protected function isUserEmailNotConfirmed()
  143. {
  144. if ($this->user->isEmailConfirmed())
  145. {
  146. return false;
  147. }
  148. return true;
  149. }
  150. public function getConditionForAffectedAction($action)
  151. {
  152. }
  153. }