nextcloud_webdav_client-1.0.x-dev/src/Form/NextCloudUserLinkForm.php
src/Form/NextCloudUserLinkForm.php
<?php
namespace Drupal\nextcloud_webdav_client\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\nextcloud_webdav_client\Service\NextCloudOAuth2Manager;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Form for users to link their NextCloud account.
*/
class NextCloudUserLinkForm extends FormBase {
/**
* The OAuth2 manager service.
*
* @var \Drupal\nextcloud_webdav_client\Service\NextCloudOAuth2Manager
*/
protected $oauth2Manager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a NextCloudUserLinkForm object.
*
* @param \Drupal\nextcloud_webdav_client\Service\NextCloudOAuth2Manager $oauth2_manager
* The OAuth2 manager service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(NextCloudOAuth2Manager $oauth2_manager, EntityTypeManagerInterface $entity_type_manager) {
$this->oauth2Manager = $oauth2_manager;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('nextcloud_webdav_client.oauth2_manager'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'nextcloud_user_link_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
$config = $this->config('nextcloud_webdav_client.settings');
// Check if OAuth2 is configured.
if (!$this->oauth2Manager->isConfigured()) {
$form['not_configured'] = [
'#markup' => '<div class="messages messages--warning">' .
$this->t('NextCloud OAuth2 is not configured yet. Please contact an administrator.') .
'</div>',
];
return $form;
}
// Check if site is in per-user mode.
$oauth2_mode = $config->get('oauth2_mode') ?: 'site';
if ($oauth2_mode !== 'per-user') {
$form['wrong_mode'] = [
'#markup' => '<div class="messages messages--info">' .
$this->t('Per-user NextCloud linking is not enabled. The site is using site-wide authentication.') .
'</div>',
];
return $form;
}
/** @var \Drupal\nextcloud_webdav_client\NextCloudUserTokenStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage('nextcloud_user_token');
$token_entity = $storage->loadByUser($user);
if ($token_entity) {
// User has linked account.
$form['status'] = [
'#markup' => '<div class="messages messages--status">' .
'<strong>' . $this->t('✓ NextCloud Account Linked') . '</strong><br>' .
$this->t('NextCloud Username: @username', ['@username' => $token_entity->getNextCloudUsername()]) .
'</div>',
];
$expiry = $token_entity->getTokenExpiry();
if ($expiry) {
$expiry_date = \Drupal::service('date.formatter')->format($expiry, 'medium');
$form['expiry'] = [
'#markup' => '<p>' . $this->t('Token expires: @date', ['@date' => $expiry_date]) . '</p>',
];
}
$form['info'] = [
'#markup' => '<p>' . $this->t('Your NextCloud account is linked and active. File operations will use your personal NextCloud storage.') . '</p>',
];
$form['actions'] = ['#type' => 'actions'];
$form['actions']['refresh'] = [
'#type' => 'link',
'#title' => $this->t('Refresh Token'),
'#url' => Url::fromRoute('nextcloud_webdav_client.user_oauth2_refresh', ['user' => $user->id()]),
'#attributes' => ['class' => ['button']],
];
$form['actions']['unlink'] = [
'#type' => 'link',
'#title' => $this->t('Unlink Account'),
'#url' => Url::fromRoute('nextcloud_webdav_client.user_oauth2_unlink', ['user' => $user->id()]),
'#attributes' => [
'class' => ['button', 'button--danger'],
'onclick' => 'return confirm("' . $this->t('Are you sure you want to unlink your NextCloud account?') . '");',
],
];
}
else {
// User needs to link account.
$form['not_linked'] = [
'#markup' => '<div class="messages messages--warning">' .
$this->t('You have not linked your NextCloud account yet.') .
'</div>',
];
$form['info'] = [
'#markup' => '<p>' . $this->t('To use NextCloud file operations, you need to link your NextCloud account. Click the button below to authorize access.') . '</p>',
];
$initiate_url = Url::fromRoute('nextcloud_webdav_client.user_oauth2_initiate');
$form['actions'] = ['#type' => 'actions'];
$form['actions']['link'] = [
'#type' => 'link',
'#title' => $this->t('Link NextCloud Account'),
'#url' => $initiate_url,
'#attributes' => ['class' => ['button', 'button--primary']],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// This form has no submit action, only links.
}
}
