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
68 lines (60 loc) · 2.13 KB
/
Copy pathAuthLinkHelper.php
File metadata and controls
68 lines (60 loc) · 2.13 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
<?php
/**
* Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2017, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace CakeDC\Users\View\Helper;
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use Cake\Event\Event;
use Cake\Event\EventManager;
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. 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|bool
*/
public function link($title, $url = null, array $options = [])
{
$linkOptions = $options;
unset($linkOptions['before'], $linkOptions['after'], $linkOptions['allowed']);
$allowed = Hash::get($options, 'allowed');
if ($allowed === false) {
return false;
}
if ($allowed === true || $this->isAuthorized($url)) {
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 = EventManager::instance()->dispatch($event);
return $result->result;
}
}