123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
-
- namespace Muzich\CoreBundle\Security;
-
- use Muzich\CoreBundle\Entity\User;
-
- class Context
- {
-
- const ACTION_ELEMENT_ADD = 0;
- const ACTION_ELEMENT_NOTE = 1;
- const ACTION_ELEMENT_ALERT = 2;
- const ACTION_ELEMENT_ADD_TO_FAVORITES = 3;
- const ACTION_ELEMENT_TAGS_PROPOSITION = 4;
- const ACTION_GROUP_ADD = 5;
- const ACTION_COMMENT_ADD = 6;
- const ACTION_COMMENT_ALERT = 7;
- const ACTION_USER_FOLLOW = 8;
- const ACTION_TAG_ADD = 9;
- const ACTION_GET_FAVORITES_TAGS = 10;
-
- const ACTION_PLAYLIST_SHOW = 11;
- const ACTION_PLAYLIST_DATA_AUTOPLAY = 12;
- const ACTION_PLAYLIST_ADD_ELEMENT = 13;
- const ACTION_PLAYLIST_UPDATE_ORDER = 14;
- const ACTION_PLAYLIST_REMOVE_ELEMENT = 15;
- const ACTION_PLAYLIST_ADD_PROMPT = 16;
- const ACTION_PLAYLIST_CREATE = 17;
- const ACTION_PLAYLIST_COPY = 18;
- const ACTION_PLAYLIST_DELETE = 19;
- const ACTION_PLAYLIST_UNPICK = 20;
- const ACTION_PLAYLIST_PICK = 21;
-
- const AFFECT_CANT_MAKE = 0;
- const AFFECT_NO_SCORING = 1;
-
- const CONDITION_USER_EMAIL_NOT_CONFIRMED = 'UserEmailNotConfirmed';
- const CONDITION_USER_NOT_CONNECTED = 'UserNotConnected';
-
- static $affecteds_actions = array(
- self::AFFECT_CANT_MAKE => array(
- self::ACTION_ELEMENT_ADD => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_ELEMENT_NOTE => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_COMMENT_ALERT => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_ELEMENT_ALERT => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_TAG_ADD => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_ELEMENT_TAGS_PROPOSITION => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_GROUP_ADD => array(
- self::CONDITION_USER_NOT_CONNECTED,
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_ELEMENT_ADD_TO_FAVORITES => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_COMMENT_ADD => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_USER_FOLLOW => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_GET_FAVORITES_TAGS => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_ADD_ELEMENT => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_UPDATE_ORDER => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_REMOVE_ELEMENT => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_CREATE => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_COPY => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_DELETE => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_UNPICK => array(
- self::CONDITION_USER_NOT_CONNECTED
- ),
- self::ACTION_PLAYLIST_PICK => array(
- self::CONDITION_USER_NOT_CONNECTED
- )
- ),
- self::AFFECT_NO_SCORING => array(
- self::ACTION_ELEMENT_NOTE => array(
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_ELEMENT_ADD_TO_FAVORITES => array(
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_ELEMENT_TAGS_PROPOSITION => array(
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_USER_FOLLOW => array(
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_PLAYLIST_ADD_ELEMENT => array(
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- ),
- self::ACTION_PLAYLIST_REMOVE_ELEMENT => array(
- self::CONDITION_USER_EMAIL_NOT_CONFIRMED
- )
- )
- );
-
- private $user;
- private $anonymous = false;
-
- public function __construct($user)
- {
- if ($user instanceof User)
- {
- $this->user = $user;
- }
- else if ($user == 'anon.')
- {
- $this->user = new User();
- $this->anonymous = true;
- }
- else
- {
- throw new \Exception('Unable to determine type of user');
- }
- }
-
- public function canMakeAction($action)
- {
- if ($this->actionIsAffectedBy(self::AFFECT_CANT_MAKE, $action) !== false)
- return false;
- return true;
- }
-
- protected function actionCanBeAffectedBy($affect, $action)
- {
- if (!array_key_exists($affect, self::$affecteds_actions))
- throw new \Exception("Unknow action $action");
-
- if (array_key_exists($action, self::$affecteds_actions[$affect]))
- return true;
- return false;
- }
-
- public function actionIsAffectedBy($affect, $action)
- {
- if ($this->actionCanBeAffectedBy($affect, $action))
- {
- foreach (self::$affecteds_actions[$affect][$action] as $affected_condition)
- {
- if ($this->userIsInThisCondition($affected_condition))
- {
- return $affected_condition;
- }
- }
- }
-
- return false;
- }
-
- public function userIsInThisCondition($condition)
- {
- $affected_condition_method = 'is'.$condition;
- if ($this->$affected_condition_method())
- {
- return true;
- }
- return false;
- }
-
- protected function isUserNotConnected()
- {
- if ($this->anonymous)
- {
- return true;
- }
- return false;
- }
-
- protected function isUserEmailNotConfirmed()
- {
- if ($this->user->isEmailConfirmed())
- {
- return false;
- }
- return true;
- }
-
- public function getConditionForAffectedAction($action)
- {
-
- }
-
- }
|