Context.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static $affecteds_actions = array(
  20. self::AFFECT_CANT_MAKE => array(
  21. self::ACTION_ELEMENT_ADD,
  22. self::ACTION_ELEMENT_NOTE,
  23. self::ACTION_COMMENT_ALERT,
  24. self::ACTION_ELEMENT_ALERT,
  25. self::ACTION_TAG_ADD,
  26. self::ACTION_ELEMENT_TAGS_PROPOSITION,
  27. self::ACTION_GROUP_ADD
  28. ),
  29. self::AFFECT_NO_SCORING => array(
  30. self::ACTION_ELEMENT_NOTE,
  31. self::ACTION_ELEMENT_ADD_TO_FAVORITES,
  32. self::ACTION_ELEMENT_TAGS_PROPOSITION,
  33. self::ACTION_USER_FOLLOW
  34. )
  35. );
  36. static $affecteds_conditions = array(
  37. self::AFFECT_CANT_MAKE => array(
  38. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  39. ),
  40. self::AFFECT_NO_SCORING => array(
  41. self::CONDITION_USER_EMAIL_NOT_CONFIRMED
  42. )
  43. );
  44. private $user;
  45. public function __construct(User $user)
  46. {
  47. $this->user = $user;
  48. }
  49. public function canMakeAction($action)
  50. {
  51. if ($this->actionIsAffectedBy(self::AFFECT_CANT_MAKE, $action))
  52. return false;
  53. return true;
  54. }
  55. protected function actionCanBeAffectedBy($affect, $action)
  56. {
  57. if (!array_key_exists($affect, self::$affecteds_actions))
  58. throw new \Exception("Unknow action $action");
  59. if (in_array($action, self::$affecteds_actions[$affect]))
  60. return true;
  61. return false;
  62. }
  63. public function actionIsAffectedBy($affect, $action)
  64. {
  65. if ($this->actionCanBeAffectedBy($affect, $action))
  66. {
  67. foreach (self::$affecteds_conditions[$affect] as $affected_condition)
  68. {
  69. $affected_condition_method = 'is'.$affected_condition;
  70. if ($this->$affected_condition_method())
  71. {
  72. return true;
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. protected function isUserEmailNotConfirmed()
  79. {
  80. if ($this->user->isEmailConfirmed())
  81. {
  82. return false;
  83. }
  84. return true;
  85. }
  86. }