Context.php 4.2KB

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