nextcloud_webdav_client-1.0.x-dev/nextcloud_webdav_client.install

nextcloud_webdav_client.install
<?php

/**
 * @file
 * Install, update and uninstall functions for NextCloud WebDAV Client module.
 */

/**
 * Implements hook_schema().
 */
function nextcloud_webdav_client_schema() {
  $schema = [];

  $schema['nextcloud_user_token'] = [
    'description' => 'Stores per-user OAuth2 tokens for NextCloud.',
    'fields' => [
      'id' => [
        'description' => 'Primary Key: Unique token ID.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'uuid' => [
        'description' => 'Unique UUID for this token.',
        'type' => 'varchar_ascii',
        'length' => 128,
        'not null' => TRUE,
      ],
      'uid' => [
        'description' => 'The {users}.uid that owns this token.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'access_token' => [
        'description' => 'The OAuth2 access token.',
        'type' => 'text',
        'size' => 'big',
        'not null' => TRUE,
      ],
      'refresh_token' => [
        'description' => 'The OAuth2 refresh token.',
        'type' => 'text',
        'size' => 'big',
        'not null' => FALSE,
      ],
      'token_expiry' => [
        'description' => 'The Unix timestamp when the token expires.',
        'type' => 'int',
        'not null' => FALSE,
      ],
      'nextcloud_username' => [
        'description' => 'The NextCloud username for this user.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ],
      'created' => [
        'description' => 'The Unix timestamp when the token was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
      'changed' => [
        'description' => 'The Unix timestamp when the token was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['id'],
    'unique keys' => [
      'uuid' => ['uuid'],
      'uid' => ['uid'],
    ],
    'indexes' => [
      'token_expiry' => ['token_expiry'],
      'changed' => ['changed'],
    ],
    'foreign keys' => [
      'token_owner' => [
        'table' => 'users',
        'columns' => ['uid' => 'uid'],
      ],
    ],
  ];

  return $schema;
}

/**
 * Implements hook_install().
 */
function nextcloud_webdav_client_install() {
  // Set default authentication mode setting if not already set.
  $config = \Drupal::configFactory()->getEditable('nextcloud_webdav_client.settings');
  if ($config->get('oauth2_mode') === NULL) {
    $config->set('oauth2_mode', 'site')->save();
  }

  // Set sensible default for OAuth2 Server user identifier.
  // Use 'name' (username) instead of 'uid' for more user-friendly NextCloud accounts.
  if (\Drupal::moduleHandler()->moduleExists('oauth2_server')) {
    $oauth2_config = \Drupal::configFactory()->getEditable('oauth2_server.oauth');
    if ($oauth2_config->get('user_sub_property') === NULL || $oauth2_config->get('user_sub_property') === 'uid') {
      $oauth2_config->set('user_sub_property', 'name')->save();
      \Drupal::logger('nextcloud_webdav_client')->info('Set OAuth2 user identifier to "name" (username) for better NextCloud integration.');
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function nextcloud_webdav_client_uninstall() {
  // Delete all configuration.
  \Drupal::configFactory()->getEditable('nextcloud_webdav_client.settings')->delete();
}

/**
 * Add per-user token storage table and oauth2_mode configuration.
 */
function nextcloud_webdav_client_update_9001() {
  // Create the nextcloud_user_token table.
  $schema = nextcloud_webdav_client_schema();
  \Drupal::database()->schema()->createTable('nextcloud_user_token', $schema['nextcloud_user_token']);

  // Add oauth2_mode configuration.
  $config = \Drupal::configFactory()->getEditable('nextcloud_webdav_client.settings');
  if ($config->get('oauth2_mode') === NULL) {
    $config->set('oauth2_mode', 'site')->save();
  }

  return t('Added per-user token storage and oauth2_mode configuration.');
}

/**
 * Set sensible OAuth2 Server user identifier default (username instead of UID).
 */
function nextcloud_webdav_client_update_9002() {
  // Only set if oauth2_server module is installed.
  if (!\Drupal::moduleHandler()->moduleExists('oauth2_server')) {
    return t('OAuth2 Server module not installed, skipping user identifier configuration.');
  }

  $oauth2_config = \Drupal::configFactory()->getEditable('oauth2_server.oauth');
  $current_value = $oauth2_config->get('user_sub_property');

  // Only change if it's the default 'uid' value or not set.
  // Don't override if admin has explicitly set it to something else.
  if ($current_value === NULL || $current_value === 'uid') {
    $oauth2_config->set('user_sub_property', 'name')->save();
    return t('Set OAuth2 user identifier to "name" (username) for better NextCloud integration. Previous value was: @value', [
      '@value' => $current_value ?? 'not set',
    ]);
  }

  return t('OAuth2 user identifier already set to "@value", not changing.', [
    '@value' => $current_value,
  ]);
}

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

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