forked from CakeDC/users
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthLinkHelper.php
More file actions
83 lines (73 loc) · 2.4 KB
/
Copy pathAuthLinkHelper.php
File metadata and controls
83 lines (73 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\View\Helper;
use Cake\View\Helper\HtmlHelper;
use CakeDC\Auth\Traits\IsAuthorizedTrait;
/**
* AuthLink helper
*
* @property \Cake\View\Helper\FormHelper $Form
*/
class AuthLinkHelper extends HtmlHelper
{
use IsAuthorizedTrait;
public $helpers = ['Url', 'Form'];
/**
* Generate a link if the target url is authorized for the logged in user
*
* @param string $title link's title.
* @param string|array|null $url url that the user is making request.
* @param array $options Array with option data. Extra options include
* 'before' and 'after' to quickly inject some html code in the link, like icons etc
* 'allowed' to manage if the link should be displayed, default is null to check isAuthorized
* @return string
*/
public function link($title, $url = null, array $options = []): string
{
$linkOptions = $options;
unset($linkOptions['before'], $linkOptions['after'], $linkOptions['allowed']);
$allowed = $options['allowed'] ?? null;
if ($allowed === false) {
return '';
}
if ($allowed === true || $this->isAuthorized($url)) {
return ($options['before'] ?? '') .
parent::link($title, $url, $linkOptions) .
($options['after'] ?? '');
}
return '';
}
/**
* Wrapper for FormHelper.postLink.
* Write the link only if user is authorized.
*
* @param string $title Link's title
* @param string|array $url Link's url
* @param array $options Link's options
* @return string Link as a string.
*/
public function postLink($title, $url = null, array $options = []): string
{
return $this->isAuthorized($url)
? $this->Form->postLink($title, $url, $options)
: '';
}
/**
* Get the current request
*
* @return \Cake\Http\ServerRequest
*/
public function getRequest()
{
return $this->getView()->getRequest();
}
}