Skip to content
Snippets Groups Projects

Issue #3184322: "user/current/{action}" not working

3 files
+ 73
29
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -2,54 +2,92 @@
namespace Drupal\user_current_paths\Controller;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Core\Path\PathValidator;
class UserCurrentPathsController extends ControllerBase
{
/**
* Defines the User Current Paths controller.
*/
class UserCurrentPathsController extends ControllerBase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxy
*/
protected $currentUser;
/**
* The path validator.
*
* @var \Drupal\Core\Path\PathValidator
*/
protected $pathValidator;
/**
* Constructs a UserCurrentPathsController object.
*
* @param \Drupal\Core\Session\AccountProxy $current_user
* The current user.
* @param \Drupal\Core\Path\PathValidator $path_validator
* The path validator.
*/
public function __construct(AccountProxy $current_user, PathValidator $path_validator) {
$this->currentUser = $current_user;
$this->pathValidator = $path_validator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user'),
$container->get('path.validator')
);
}
/**
* Handles wildcard (user/current/*) redirects for the current user.
* Replaces the second "current" parameter in the URL with the currently logged in user
* and redirects to the target if the resulting path is valid. Ohterwise throws a NotFoundHttpException.
* This is safe because the redirect is handled as if the user entered the URL manually with all security checks.
*
* Replaces the second "current" parameter in the URL with the currently
* logged in user and redirects to the target if the resulting path is valid.
* Ohterwise throws a NotFoundHttpException. This is safe because the redirect
* is handled as if the user entered the URL manually with all security
* checks.
*
* @param string $wildcardaction
* @param Request $request
* @return void
* The wildcart action.
*/
public function wildcardActionRedirect($wildcardaction = 'view', Request $request)
{
$currentUserId = (int)\Drupal::currentUser()->id();
$path = '/user/' . $currentUserId;
public function wildcardActionRedirect(string $wildcardaction) {
$path = '/user/' . $this->currentUser->id();
if ($wildcardaction != 'view') {
// /view doesn't exist for user entities
$path .= '/' . $wildcardaction;
}
$url = \Drupal::service('path.validator')->getUrlIfValid($path);
if ($url !== false) {
$url = $this->pathValidator->getUrlIfValid($path);
if ($url !== FALSE) {
// Valid internal path:
return $this->redirect($url);
} else {
throw new NotFoundHttpException();
return $this->redirect($url->getRouteName(), $url->getRouteParameters(), $url->getOptions());
}
throw new NotFoundHttpException();
}
/**
* Handles redirects to the user edit page (user/edit) for the currently logged in user.
*
* @param Request $request
* @return void
* Handles redirects to the user edit page for the currently logged in user.
*/
public function editRedirect(Request $request)
{
public function editRedirect() {
$route_name = 'entity.user.edit_form';
$route_parameters = [
'user' => \Drupal::currentUser()->id(),
'user' => $this->currentUser->id(),
];
return $this->redirect($route_name, $route_parameters);
}
}
Loading