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
49 lines (42 loc) · 1.36 KB
/
Copy pathAuthLinkHelper.php
File metadata and controls
49 lines (42 loc) · 1.36 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
<?php
namespace CakeDC\Users\View\Helper;
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use Cake\Event\Event;
use Cake\Utility\Hash;
use Cake\View\Helper;
use Cake\View\Helper\HtmlHelper;
/**
* AuthLink helper
*/
class AuthLinkHelper extends HtmlHelper
{
/**
* 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.
* @return string
*/
public function link($title, $url = null, array $options = [])
{
if ($this->isAuthorized($url)) {
$linkOptions = $options;
unset($linkOptions['before'], $linkOptions['after']);
return Hash::get($options, 'before') . parent::link($title, $url, $linkOptions) . Hash::get($options, 'after');
}
return false;
}
/**
* Returns true if the target url is authorized for the logged in user
*
* @param string|array|null $url url that the user is making request.
* @return bool
*/
public function isAuthorized($url = null)
{
$event = new Event(UsersAuthComponent::EVENT_IS_AUTHORIZED, $this, ['url' => $url]);
$result = $this->_View->eventManager()->dispatch($event);
return $result->result;
}
}