final class RefreshInstagramTokenAction in Instagram Feeds 8
Provides an action that can refresh Instagram Token for an entity.
Plugin annotation
@Action(
id = "entity:refresh_instagram_token",
action_label = @Translation("Refresh Token"),
deriver = "Drupal\instagram_feeds\Plugin\Action\Derivative\RefreshInstagramTokenActionDeriver",
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Action\Plugin\Action\EntityActionBase implements DependentPluginInterface, ContainerFactoryPluginInterface
- class \Drupal\instagram_feeds\Plugin\Action\RefreshInstagramTokenAction
- class \Drupal\Core\Action\Plugin\Action\EntityActionBase implements DependentPluginInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of RefreshInstagramTokenAction
File
- src/
Plugin/ Action/ RefreshInstagramTokenAction.php, line 22
Namespace
Drupal\instagram_feeds\Plugin\ActionView source
final class RefreshInstagramTokenAction extends EntityActionBase {
/**
* The time service.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* GuzzleHttp\ClientInterface definition.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $httpClient;
/**
* Constructs a RefreshTokenAction object.
*
* @param mixed[] $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Component\Datetime\TimeInterface $time
* The time service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time, ClientInterface $http_client) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
$this->time = $time;
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('datetime.time'), $container
->get('http_client'));
}
/**
* {@inheritdoc}
*/
public function execute($entity = NULL) {
/** @var \Drupal\instagram_feeds\Entity\InstagramAccountInterface $entity */
$entity
->refreshToken($this->httpClient, TRUE);
}
/**
* {@inheritdoc}
*/
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
/** @var \Drupal\instagram_feeds\Entity\InstagramAccountInterface $object */
if (!$account) {
$account = \Drupal::currentUser();
}
// Token is invalid or entity is unpublished.
if (!$object
->isPublished() || !$object
->tokenIsValid()) {
$result = AccessResult::forbidden('Unpublished or Instagram token is invalid.')
->addCacheableDependency($object);
return $return_as_object ? $result : $result
->isAllowed();
}
$primary_access = $account
->hasPermission('administer instagram_feeds');
$role_access = $primary_access || $account
->hasPermission('update instagram_account');
$user_access = $role_access || $account
->id() == $object
->getOwnerId() && $account
->hasPermission('update own instagram_account');
// User doesn't have access.
if (!$user_access) {
$result = AccessResult::forbidden()
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
// Token can be refreshed only after 24h. It lives 60 days. Expired token
// cannot be refreshed anymore. 60 days - 1 day = 59 days = 5097600 sec.
$age = $object
->getTokenExpirationTime() - $this->time
->getRequestTime();
if ($age <= 5097600) {
$result = AccessResult::allowed()
->setCacheMaxAge($age)
->cachePerPermissions()
->addCacheableDependency($object);
return $return_as_object ? $result : $result
->isAllowed();
}
$age -= 5097600;
$result = AccessResult::forbidden('You should wait at least 24 hours prior previous token refesh')
->setCacheMaxAge($age)
->cachePerPermissions();
return $return_as_object ? $result : $result
->isAllowed();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ActionBase:: |
public | function |
Executes the plugin for an array of objects. Overrides ActionInterface:: |
3 |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityActionBase:: |
protected | property | The entity type manager. | |
EntityActionBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
RefreshInstagramTokenAction:: |
protected | property | GuzzleHttp\ClientInterface definition. | |
RefreshInstagramTokenAction:: |
protected | property | The time service. | |
RefreshInstagramTokenAction:: |
public | function |
Checks object access. Overrides ActionInterface:: |
|
RefreshInstagramTokenAction:: |
public static | function |
Creates an instance of the plugin. Overrides EntityActionBase:: |
|
RefreshInstagramTokenAction:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
RefreshInstagramTokenAction:: |
public | function |
Constructs a RefreshTokenAction object. Overrides EntityActionBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |