View source
<?php
declare (strict_types=1);
namespace Drupal\og\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Url;
use Drupal\og\Og;
use Drupal\og\OgAccessInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\og\OgMembershipTypeInterface;
use Drupal\user\EntityOwnerInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class SubscriptionController extends ControllerBase {
protected $ogAccess;
protected $messenger;
public function __construct(OgAccessInterface $og_access, MessengerInterface $messenger) {
$this->ogAccess = $og_access;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('og.access'), $container
->get('messenger'));
}
public function subscribe($entity_type_id, EntityInterface $group, OgMembershipTypeInterface $membership_type) {
if (!$group instanceof ContentEntityInterface) {
throw new AccessDeniedHttpException();
}
if (!Og::isGroup($entity_type_id, $group
->bundle())) {
throw new AccessDeniedHttpException();
}
$user = $this
->entityTypeManager()
->getStorage('user')
->load($this
->currentUser()
->id());
if ($user
->isAnonymous()) {
$destination = $this
->getDestinationArray();
$user_login_url = Url::fromRoute('user.login', [], $destination)
->toString();
if ($this
->config('user.settings')
->get('register') === UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
$params = [
':login' => $user_login_url,
];
$this->messenger
->addMessage($this
->t('In order to join any group, you must <a href=":login">login</a>. After you have successfully done so, you will need to request membership again.', $params));
}
else {
$user_register_url = Url::fromRoute('user.register', [], $destination)
->toString();
$params = [
':register' => $user_register_url,
':login' => $user_login_url,
];
$this->messenger
->addMessage($this
->t('In order to join any group, you must <a href=":login">login</a> or <a href=":register">register</a> a new account. After you have successfully done so, you will need to request membership again.', $params));
}
return new RedirectResponse(Url::fromRoute('user.page')
->setAbsolute(TRUE)
->toString());
}
$redirect = FALSE;
$message = '';
$params = [
'@user' => $user
->getDisplayName(),
];
$params['@group'] = $group
->access('view', $user) ? $group
->label() : $this
->t('Private group');
if (Og::isMemberBlocked($group, $user)) {
throw new AccessDeniedHttpException();
}
if (Og::isMemberPending($group, $user)) {
$message = $this
->t('You already have a pending membership for the the group @group.', $params);
$redirect = TRUE;
}
if (Og::isMember($group, $user)) {
$message = $this
->t('You are already a member of the group @group.', $params);
$redirect = TRUE;
}
if ($redirect) {
$this->messenger
->addMessage($message, 'warning');
return new RedirectResponse($group
->toUrl()
->setAbsolute(TRUE)
->toString());
}
$subscribe = $this->ogAccess
->userAccess($group, 'subscribe');
$subscribe_without_approval = $this->ogAccess
->userAccess($group, 'subscribe without approval');
if (!$subscribe
->isAllowed() && !$subscribe_without_approval
->isAllowed()) {
throw new AccessDeniedHttpException();
}
$membership = Og::createMembership($group, $user, $membership_type
->id());
$form = $this
->entityFormBuilder()
->getForm($membership, 'subscribe');
return $form;
}
public function unsubscribe(ContentEntityInterface $group) {
$user = $this
->currentUser();
if (!($membership = Og::getMembership($group, $user, OgMembershipInterface::ALL_STATES))) {
throw new AccessDeniedHttpException();
}
if ($membership
->getState() == OgMembershipInterface::STATE_BLOCKED) {
throw new AccessDeniedHttpException();
}
if ($group instanceof EntityOwnerInterface && $group
->getOwnerId() == $user
->id()) {
$this->messenger
->addMessage($this
->t('As the manager of %group, you can not leave the group.', [
'%group' => $group
->label(),
]));
return new RedirectResponse($group
->toUrl()
->setAbsolute()
->toString());
}
$form = $this
->entityFormBuilder()
->getForm($membership, 'unsubscribe');
return $form;
}
}