Context.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 ACTION_PLAYLIST_SHOW = 11;
  18. const ACTION_PLAYLIST_DATA_AUTOPLAY = 12;
  19. const ACTION_PLAYLIST_ADD_ELEMENT = 13;
  20. const ACTION_PLAYLIST_UPDATE_ORDER = 14;
  21. const ACTION_PLAYLIST_REMOVE_ELEMENT = 15;
  22. const ACTION_PLAYLIST_ADD_PROMPT = 16;
  23. const ACTION_PLAYLIST_CREATE = 17;
  24. const ACTION_PLAYLIST_COPY = 18;
  25. const ACTION_PLAYLIST_DELETE = 19;
  26. const ACTION_PLAYLIST_UNPICK = 20;
  27. const ACTION_PLAYLIST_PICK = 21;
  28. const AFFECT_CANT_MAKE = 0;
  29. const AFFECT_NO_SCORING = 1;
  30. const CONDITION_USER_EMAIL_NOT_CONFIRMED = 'UserEmailNotConfirmed';
  31. const CONDITION_USER_NOT_CONNECTED = 'UserNotConnected';
  32. static $affecteds_actions = array(
  33. self::AFFECT_CANT_MAKE => array(
  34. self::ACTION_ELEMENT_ADD => array(
  35. self::CONDITION_USER_NOT_CONNECTED,
  36. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  37. ),
  38. self::ACTION_ELEMENT_NOTE => array(
  39. self::CONDITION_USER_NOT_CONNECTED,
  40. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  41. ),
  42. self::ACTION_COMMENT_ALERT => array(
  43. self::CONDITION_USER_NOT_CONNECTED,
  44. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  45. ),
  46. self::ACTION_ELEMENT_ALERT => array(
  47. self::CONDITION_USER_NOT_CONNECTED,
  48. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  49. ),
  50. self::ACTION_TAG_ADD => array(
  51. self::CONDITION_USER_NOT_CONNECTED,
  52. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  53. ),
  54. self::ACTION_ELEMENT_TAGS_PROPOSITION => array(
  55. self::CONDITION_USER_NOT_CONNECTED,
  56. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  57. ),
  58. self::ACTION_GROUP_ADD => array(
  59. self::CONDITION_USER_NOT_CONNECTED,
  60. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  61. ),
  62. self::ACTION_ELEMENT_ADD_TO_FAVORITES => array(
  63. self::CONDITION_USER_NOT_CONNECTED
  64. ),
  65. self::ACTION_COMMENT_ADD => array(
  66. self::CONDITION_USER_NOT_CONNECTED
  67. ),
  68. self::ACTION_USER_FOLLOW => array(
  69. self::CONDITION_USER_NOT_CONNECTED
  70. ),
  71. self::ACTION_GET_FAVORITES_TAGS => array(
  72. self::CONDITION_USER_NOT_CONNECTED
  73. ),
  74. self::ACTION_PLAYLIST_ADD_ELEMENT => array(
  75. self::CONDITION_USER_NOT_CONNECTED
  76. ),
  77. self::ACTION_PLAYLIST_UPDATE_ORDER => array(
  78. self::CONDITION_USER_NOT_CONNECTED
  79. ),
  80. self::ACTION_PLAYLIST_REMOVE_ELEMENT => array(
  81. self::CONDITION_USER_NOT_CONNECTED
  82. ),
  83. self::ACTION_PLAYLIST_CREATE => array(
  84. self::CONDITION_USER_NOT_CONNECTED
  85. ),
  86. self::ACTION_PLAYLIST_COPY => array(
  87. self::CONDITION_USER_NOT_CONNECTED
  88. ),
  89. self::ACTION_PLAYLIST_DELETE => array(
  90. self::CONDITION_USER_NOT_CONNECTED
  91. ),
  92. self::ACTION_PLAYLIST_UNPICK => array(
  93. self::CONDITION_USER_NOT_CONNECTED
  94. ),
  95. self::ACTION_PLAYLIST_PICK => array(
  96. self::CONDITION_USER_NOT_CONNECTED
  97. )
  98. ),
  99. self::AFFECT_NO_SCORING => array(
  100. self::ACTION_ELEMENT_NOTE => array(
  101. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  102. ),
  103. self::ACTION_ELEMENT_ADD_TO_FAVORITES => array(
  104. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  105. ),
  106. self::ACTION_ELEMENT_TAGS_PROPOSITION => array(
  107. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  108. ),
  109. self::ACTION_USER_FOLLOW => array(
  110. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  111. ),
  112. self::ACTION_PLAYLIST_ADD_ELEMENT => array(
  113. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  114. ),
  115. self::ACTION_PLAYLIST_REMOVE_ELEMENT => array(
  116. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  117. )
  118. )
  119. );
  120. private $user;
  121. private $anonymous = false;
  122. public function __construct($user)
  123. {
  124. if ($user instanceof User)
  125. {
  126. $this->user = $user;
  127. }
  128. else if ($user == 'anon.')
  129. {
  130. $this->user = new User();
  131. $this->anonymous = true;
  132. }
  133. else
  134. {
  135. throw new \Exception('Unable to determine type of user');
  136. }
  137. }
  138. public function canMakeAction($action)
  139. {
  140. if ($this->actionIsAffectedBy(self::AFFECT_CANT_MAKE, $action) !== false)
  141. return false;
  142. return true;
  143. }
  144. protected function actionCanBeAffectedBy($affect, $action)
  145. {
  146. if (!array_key_exists($affect, self::$affecteds_actions))
  147. throw new \Exception("Unknow action $action");
  148. if (array_key_exists($action, self::$affecteds_actions[$affect]))
  149. return true;
  150. return false;
  151. }
  152. public function actionIsAffectedBy($affect, $action)
  153. {
  154. if ($this->actionCanBeAffectedBy($affect, $action))
  155. {
  156. foreach (self::$affecteds_actions[$affect][$action] as $affected_condition)
  157. {
  158. if ($this->userIsInThisCondition($affected_condition))
  159. {
  160. return $affected_condition;
  161. }
  162. }
  163. }
  164. return false;
  165. }
  166. public function userIsInThisCondition($condition)
  167. {
  168. $affected_condition_method = 'is'.$condition;
  169. if ($this->$affected_condition_method())
  170. {
  171. return true;
  172. }
  173. return false;
  174. }
  175. protected function isUserNotConnected()
  176. {
  177. if ($this->anonymous)
  178. {
  179. return true;
  180. }
  181. return false;
  182. }
  183. protected function isUserEmailNotConfirmed()
  184. {
  185. if ($this->user->isEmailConfirmed())
  186. {
  187. return false;
  188. }
  189. return true;
  190. }
  191. public function getConditionForAffectedAction($action)
  192. {
  193. }
  194. }