nextcloud_webdav_client-1.0.x-dev/src/Entity/NextCloudUserToken.php

src/Entity/NextCloudUserToken.php
<?php

namespace Drupal\nextcloud_webdav_client\Entity;

use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\EntityOwnerTrait;

/**
 * Defines the NextCloud User Token entity.
 *
 * Stores OAuth2 tokens for individual Drupal users.
 *
 * @ContentEntityType(
 *   id = "nextcloud_user_token",
 *   label = @Translation("NextCloud User Token"),
 *   label_collection = @Translation("NextCloud User Tokens"),
 *   handlers = {
 *     "storage" = "Drupal\nextcloud_webdav_client\NextCloudUserTokenStorage",
 *     "access" = "Drupal\nextcloud_webdav_client\NextCloudUserTokenAccessControlHandler",
 *   },
 *   base_table = "nextcloud_user_token",
 *   entity_keys = {
 *     "id" = "id",
 *     "uuid" = "uuid",
 *     "owner" = "uid",
 *   },
 * )
 */
class NextCloudUserToken extends ContentEntityBase implements EntityOwnerInterface {

  use EntityOwnerTrait;

  /**
   * {@inheritdoc}
   */
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    $fields = parent::baseFieldDefinitions($entity_type);

    // User ID (owner).
    $fields['uid'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('User ID'))
      ->setDescription(t('The user ID of the token owner.'))
      ->setSetting('target_type', 'user')
      ->setRequired(TRUE);

    // OAuth2 Access Token.
    $fields['access_token'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Access Token'))
      ->setDescription(t('The OAuth2 access token.'))
      ->setRequired(TRUE);

    // OAuth2 Refresh Token.
    $fields['refresh_token'] = BaseFieldDefinition::create('string_long')
      ->setLabel(t('Refresh Token'))
      ->setDescription(t('The OAuth2 refresh token.'));

    // Token Expiry Timestamp.
    $fields['token_expiry'] = BaseFieldDefinition::create('timestamp')
      ->setLabel(t('Token Expiry'))
      ->setDescription(t('The timestamp when the access token expires.'));

    // NextCloud Username.
    $fields['nextcloud_username'] = BaseFieldDefinition::create('string')
      ->setLabel(t('NextCloud Username'))
      ->setDescription(t('The NextCloud username for this user.'))
      ->setRequired(TRUE);

    // Created timestamp.
    $fields['created'] = BaseFieldDefinition::create('created')
      ->setLabel(t('Created'))
      ->setDescription(t('The time that the token was created.'));

    // Changed timestamp.
    $fields['changed'] = BaseFieldDefinition::create('changed')
      ->setLabel(t('Changed'))
      ->setDescription(t('The time that the token was last updated.'));

    return $fields;
  }

  /**
   * Gets the access token.
   *
   * @return string|null
   *   The access token, or NULL if not set.
   */
  public function getAccessToken(): ?string {
    return $this->get('access_token')->value;
  }

  /**
   * Sets the access token.
   *
   * @param string $token
   *   The access token.
   *
   * @return $this
   */
  public function setAccessToken(string $token): self {
    $this->set('access_token', $token);
    return $this;
  }

  /**
   * Gets the refresh token.
   *
   * @return string|null
   *   The refresh token, or NULL if not set.
   */
  public function getRefreshToken(): ?string {
    return $this->get('refresh_token')->value;
  }

  /**
   * Sets the refresh token.
   *
   * @param string $token
   *   The refresh token.
   *
   * @return $this
   */
  public function setRefreshToken(string $token): self {
    $this->set('refresh_token', $token);
    return $this;
  }

  /**
   * Gets the token expiry timestamp.
   *
   * @return int|null
   *   The expiry timestamp, or NULL if not set.
   */
  public function getTokenExpiry(): ?int {
    return $this->get('token_expiry')->value;
  }

  /**
   * Sets the token expiry timestamp.
   *
   * @param int $timestamp
   *   The expiry timestamp.
   *
   * @return $this
   */
  public function setTokenExpiry(int $timestamp): self {
    $this->set('token_expiry', $timestamp);
    return $this;
  }

  /**
   * Gets the NextCloud username.
   *
   * @return string|null
   *   The NextCloud username, or NULL if not set.
   */
  public function getNextCloudUsername(): ?string {
    return $this->get('nextcloud_username')->value;
  }

  /**
   * Sets the NextCloud username.
   *
   * @param string $username
   *   The NextCloud username.
   *
   * @return $this
   */
  public function setNextCloudUsername(string $username): self {
    $this->set('nextcloud_username', $username);
    return $this;
  }

  /**
   * Checks if the token is expired or about to expire.
   *
   * @param int $buffer
   *   Buffer time in seconds before expiry (default: 300 = 5 minutes).
   *
   * @return bool
   *   TRUE if expired or about to expire, FALSE otherwise.
   */
  public function isExpired(int $buffer = 300): bool {
    $expiry = $this->getTokenExpiry();
    if (empty($expiry)) {
      return TRUE;
    }

    $current_time = \Drupal::time()->getRequestTime();
    return ($current_time + $buffer) >= $expiry;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc