1: <?php
2:
3: App::uses('AccessControlComponent', 'AccessControl.Controller/Component');
4: App::uses('Helper', 'View');
5:
6: class AccessControlHelper extends Helper {
7:
8: public $helpers = array(
9: 'Html',
10: 'Form',
11: );
12:
13: public function __call($method, $params) {
14: if (preg_match('/^hasAccessBy(.+)$/', $method, $matches)) {
15: if (count($params) < 1) {
16: trigger_error(__d('access_control','Missing argument 1 for %1$s::%2$s', get_class($this), $method), E_USER_ERROR);
17: }
18:
19: return AccessControlComponent::sessionUserHasAccess(
20: $params[0], Inflector::variable($matches[1])
21: );
22: }
23:
24: return parent::__call($method, $params);
25: }
26:
27: public function restrictedOutput($url, $contentIfTrue, $contentIfFalse = '', $return = true) {
28: $out = $this->hasAccessByUrl($url) ? $contentIfTrue : $contentIfFalse;
29: return $this->Html->output($out, $return);
30: }
31:
32: public function link($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true, $showTextIfAccessDenied = false) {
33: return $this->restrictedOutput(
34: $url,
35: !empty($htmlAttributes['method']) && $htmlAttributes['method'] == 'post'
36: ? $this->Form->postlink($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle)
37: : $this->Html->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle), ($showTextIfAccessDenied ? $title : '')
38: );
39: }
40:
41: public function linkOrText($title, $url = null, $htmlAttributes = array(), $confirmMessage = false, $escapeTitle = true, $showTextIfAccessDenied = false) {
42: return $this->link($title, $url, $htmlAttributes, $confirmMessage, $escapeTitle, true);
43: }
44:
45: public function image($title, $image, $url, $confirmationMessage = null) {
46: return $this->restrictedOutput(
47: $url, $this->Html->image($image, array("alt" => $title, "title" => $title, 'url' => $url, "onclick" => (!empty($confirmationMessage) ? "return confirm('$confirmationMessage')" : "")))
48: );
49: }
50:
51: }
52:
53: ?>
54: