|
6 | 6 | * |
7 | 7 | */ |
8 | 8 |
|
| 9 | +use Neos\Flow\Mvc\ActionRequest; |
9 | 10 | use Neos\FluidAdaptor\Core\ViewHelper\AbstractViewHelper; |
10 | 11 | use Neos\FluidAdaptor\Core\ViewHelper; |
11 | 12 | use Neos\Flow\Annotations as Flow; |
|
36 | 37 | class AuthenticatedActionViewHelper extends AbstractViewHelper |
37 | 38 | { |
38 | 39 |
|
39 | | - /** |
40 | | - * @Flow\Inject |
41 | | - * @var \FormatD\HmacAuthentication\Service\HmacService |
42 | | - */ |
43 | | - protected $hmacService; |
| 40 | + /** |
| 41 | + * @Flow\Inject |
| 42 | + * @var \FormatD\HmacAuthentication\Service\HmacService |
| 43 | + */ |
| 44 | + protected $hmacService; |
44 | 45 |
|
45 | | - /** |
46 | | - * Render the Uri. |
47 | | - * |
48 | | - * @param string $accountIdentifier AccountIdentifier to authenticate with |
49 | | - * @param string $action Target action |
50 | | - * @param array $arguments Arguments |
51 | | - * @param string $controller Target controller. If NULL current controllerName is used |
52 | | - * @param string $package Target package. if NULL current package is used |
53 | | - * @param string $subpackage Target subpackage. if NULL current subpackage is used |
54 | | - * @param string $section The anchor to be added to the URI |
55 | | - * @param string $format The requested format, e.g. ".html" |
56 | | - * @param array $additionalParams additional query parameters that won't be prefixed like $arguments (overrule $arguments) |
57 | | - * @param boolean $absolute If set, an absolute URI is rendered |
58 | | - * @param boolean $addQueryString If set, the current query parameters will be kept in the URI |
59 | | - * @param array $argumentsToBeExcludedFromQueryString arguments to be removed from the URI. Only active if $addQueryString = TRUE |
60 | | - * @param boolean $useParentRequest If set, the parent Request will be used instead of the current one. Note: using this argument can be a sign of undesired tight coupling, use with care |
61 | | - * @param boolean $useMainRequest If set, the main Request will be used instead of the current one. Note: using this argument can be a sign of undesired tight coupling, use with care |
62 | | - * @return string The rendered link |
63 | | - * @throws ViewHelper\Exception |
64 | | - * @api |
65 | | - */ |
66 | | - public function render($accountIdentifier, $action, array $arguments = array(), $controller = null, $package = null, $subpackage = null, $section = '', $format = '', array $additionalParams = array(), $absolute = false, $addQueryString = false, array $argumentsToBeExcludedFromQueryString = array(), $useParentRequest = false, $useMainRequest = false) |
67 | | - { |
68 | | - $uriBuilder = $this->controllerContext->getUriBuilder(); |
69 | | - if ($useParentRequest === true) { |
70 | | - $request = $this->controllerContext->getRequest(); |
71 | | - if ($request->isMainRequest()) { |
72 | | - throw new ViewHelper\Exception('You can\'t use the parent Request, you are already in the MainRequest.', 1360590758); |
73 | | - } |
74 | | - $uriBuilder = clone $uriBuilder; |
75 | | - $uriBuilder->setRequest($request->getParentRequest()); |
76 | | - } elseif ($useMainRequest === true) { |
77 | | - $request = $this->controllerContext->getRequest(); |
78 | | - if (!$request->isMainRequest()) { |
79 | | - $uriBuilder = clone $uriBuilder; |
80 | | - $uriBuilder->setRequest($request->getMainRequest()); |
81 | | - } |
82 | | - } |
| 46 | + /** |
| 47 | + * Initialize arguments |
| 48 | + * |
| 49 | + * @return void |
| 50 | + * @api |
| 51 | + */ |
| 52 | + public function initializeArguments() |
| 53 | + { |
| 54 | + $this->registerArgument('action', 'string', 'Target action', true); |
| 55 | + $this->registerArgument('arguments', 'array', 'Arguments', false, []); |
| 56 | + $this->registerArgument('controller', 'string', 'Target controller. If NULL current controllerName is used', false, null); |
| 57 | + $this->registerArgument('package', 'string', 'Target package. if NULL current package is used', false, null); |
| 58 | + $this->registerArgument('subpackage', 'string', 'Target subpackage. if NULL current subpackage is used', false, null); |
| 59 | + $this->registerArgument('section', 'string', 'The anchor to be added to the URI', false, ''); |
| 60 | + $this->registerArgument('format', 'string', 'The requested format, e.g. ".html"', false, ''); |
| 61 | + $this->registerArgument('additionalParams', 'array', 'additional query parameters that won\'t be prefixed like $arguments (overrule $arguments)', false, []); |
| 62 | + $this->registerArgument('absolute', 'boolean', 'By default this ViewHelper renders links with absolute URIs. If this is false, a relative URI is created instead', false, false); |
| 63 | + $this->registerArgument('addQueryString', 'boolean', 'If set, the current query parameters will be kept in the URI', false, false); |
| 64 | + $this->registerArgument('argumentsToBeExcludedFromQueryString', 'array', 'arguments to be removed from the URI. Only active if $addQueryString = true', false, []); |
| 65 | + $this->registerArgument('useParentRequest', 'boolean', 'If set, the parent Request will be used instead of the current one. Note: using this argument can be a sign of undesired tight coupling, use with care', false, false); |
| 66 | + $this->registerArgument('useMainRequest', 'boolean', 'If set, the main Request will be used instead of the current one. Note: using this argument can be a sign of undesired tight coupling, use with care', false, false); |
| 67 | + $this->registerArgument('accountIdentifier', 'string', 'accountIdentifier to authenticate with', true); |
| 68 | + } |
83 | 69 |
|
84 | | - $uriBuilder |
85 | | - ->reset() |
86 | | - ->setSection($section) |
87 | | - ->setCreateAbsoluteUri($absolute) |
88 | | - ->setArguments($additionalParams) |
89 | | - ->setAddQueryString($addQueryString) |
90 | | - ->setArgumentsToBeExcludedFromQueryString($argumentsToBeExcludedFromQueryString) |
91 | | - ->setFormat($format); |
92 | | - try { |
93 | | - $uri = $uriBuilder->uriFor($action, $arguments, $controller, $package, $subpackage); |
94 | | - } catch (\Exception $exception) { |
95 | | - throw new ViewHelper\Exception($exception->getMessage(), $exception->getCode(), $exception); |
96 | | - } |
| 70 | + /** |
| 71 | + * Render the Uri. |
| 72 | + * |
| 73 | + * @return string The rendered link |
| 74 | + * @throws ViewHelper\Exception |
| 75 | + * @api |
| 76 | + */ |
| 77 | + public function render() |
| 78 | + { |
| 79 | + $uriBuilder = $this->controllerContext->getUriBuilder(); |
| 80 | + if ($this->arguments['useParentRequest'] === true) { |
| 81 | + $request = $this->controllerContext->getRequest(); |
| 82 | + if ($request->isMainRequest()) { |
| 83 | + throw new ViewHelper\Exception('You can\'t use the parent Request, you are already in the MainRequest.', 1360590758); |
| 84 | + } |
| 85 | + $parentRequest = $request->getParentRequest(); |
| 86 | + if (!$parentRequest instanceof ActionRequest) { |
| 87 | + throw new ViewHelper\Exception('The parent requests was unexpectedly empty, probably the current request is broken.', 1565948254); |
| 88 | + } |
97 | 89 |
|
98 | | - $authenticationQueryPart = $this->hmacService->generateHmacAuthenticationQueryStringPart($accountIdentifier); |
99 | | - $uri = $uri . (strpos($uri, '?') === FALSE ? '?' : '&' ) . $authenticationQueryPart; |
| 90 | + $uriBuilder = clone $uriBuilder; |
| 91 | + $uriBuilder->setRequest($parentRequest); |
| 92 | + } elseif ($this->arguments['useMainRequest'] === true) { |
| 93 | + $request = $this->controllerContext->getRequest(); |
| 94 | + if (!$request->isMainRequest()) { |
| 95 | + $uriBuilder = clone $uriBuilder; |
| 96 | + $uriBuilder->setRequest($request->getMainRequest()); |
| 97 | + } |
| 98 | + } |
100 | 99 |
|
101 | | - return $uri; |
102 | | - } |
| 100 | + $uriBuilder |
| 101 | + ->reset() |
| 102 | + ->setSection($this->arguments['section']) |
| 103 | + ->setCreateAbsoluteUri($this->arguments['absolute']) |
| 104 | + ->setArguments($this->arguments['additionalParams']) |
| 105 | + ->setAddQueryString($this->arguments['addQueryString']) |
| 106 | + ->setArgumentsToBeExcludedFromQueryString($this->arguments['argumentsToBeExcludedFromQueryString']) |
| 107 | + ->setFormat($this->arguments['format']); |
| 108 | + try { |
| 109 | + $uri = $uriBuilder->uriFor($this->arguments['action'], $this->arguments['arguments'], $this->arguments['controller'], $this->arguments['package'], $this->arguments['subpackage']); |
| 110 | + } catch (\Exception $exception) { |
| 111 | + throw new ViewHelper\Exception($exception->getMessage(), $exception->getCode(), $exception); |
| 112 | + } |
| 113 | + |
| 114 | + $authenticationQueryPart = $this->hmacService->generateHmacAuthenticationQueryStringPart($this->arguments['accountIdentifier']); |
| 115 | + $uri = $uri . (strpos($uri, '?') === FALSE ? '?' : '&' ) . $authenticationQueryPart; |
| 116 | + |
| 117 | + return $uri; |
| 118 | + } |
103 | 119 | } |
0 commit comments