Skip to content

Commit f1eb71e

Browse files
authored
Merge pull request eventum#261 from glensc/controller-min-role
add $min_role to base controller
2 parents 9974d4e + 85f85c9 commit f1eb71e

10 files changed

+62
-62
lines changed

src/Controller/AdvSearchController.php

-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ class AdvSearchController extends BaseController
3636
/** @var int */
3737
protected $prj_id;
3838

39-
/** @var int */
40-
private $role_id;
41-
4239
/** @var int */
4340
private $custom_id;
4441

src/Controller/BaseController.php

+43-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ abstract class BaseController
3939
/** @var bool */
4040
protected $is_popup = false;
4141

42+
/**
43+
* Minimum role required to access the page
44+
*
45+
* @var int
46+
*/
47+
protected $min_role;
48+
49+
/** @var int */
50+
protected $role_id;
51+
4252
/** @var array */
4353
private $helpers;
4454

@@ -59,8 +69,8 @@ public function __construct()
5969
public function run()
6070
{
6171
// NOTE: canAccess needs $issue_id for the template
62-
if (!$this->canAccess()) {
63-
$this->displayTemplate('permission_denied.tpl.html');
72+
if (!$this->canRoleAccess() || !$this->canAccess()) {
73+
$this->error(ev_gettext('Sorry, you are not allowed to access this page.'));
6474
exit;
6575
}
6676

@@ -89,18 +99,48 @@ protected function getRequest()
8999

90100
/**
91101
* display template
102+
*
92103
* @param string $tpl_name
93104
*/
94105
protected function displayTemplate($tpl_name = null)
95106
{
96-
$this->tpl->assign('messages', $this->messages->getMessages());
107+
$this->tpl->assign(
108+
[
109+
'messages' => $this->messages->getMessages(),
110+
'is_popup' => $this->is_popup,
111+
]
112+
);
113+
97114
// set new template, if needed
98115
if ($tpl_name) {
99116
$this->tpl->setTemplate($tpl_name);
100117
}
101118
$this->tpl->displayTemplate();
102119
}
103120

121+
/**
122+
* If page is restricted, check for minimum role.
123+
*
124+
* @return bool
125+
*/
126+
final protected function canRoleAccess()
127+
{
128+
if ($this->min_role === null) {
129+
// not restricted
130+
return true;
131+
}
132+
133+
if ($this->is_popup) {
134+
Auth::checkAuthentication(null, true);
135+
} else {
136+
Auth::checkAuthentication();
137+
}
138+
139+
$this->role_id = Auth::getCurrentRole();
140+
141+
return $this->role_id >= $this->min_role;
142+
}
143+
104144
/**
105145
* Display error message $msg and exit
106146
*

src/Controller/CloseController.php

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class CloseController extends BaseController
3939
/** @var int */
4040
private $prj_id;
4141

42-
/** @var int */
43-
private $role_id;
44-
4542
/** @var string */
4643
private $cat;
4744

src/Controller/MainController.php

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ class MainController extends BaseController
2727
/** @var string */
2828
protected $tpl_name = 'main.tpl.html';
2929

30-
/** @var int */
31-
private $role_id;
32-
3330
/** @var int */
3431
private $usr_id;
3532

src/Controller/Manage/ManageBaseController.php

+3-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace Eventum\Controller\Manage;
1515

16-
use Auth;
1716
use Eventum\Controller\BaseController;
1817
use User;
1918

@@ -22,9 +21,6 @@ abstract class ManageBaseController extends BaseController
2221
/** @var int */
2322
protected $min_role = User::ROLE_MANAGER;
2423

25-
/** @var int */
26-
protected $role_id;
27-
2824
public function __construct()
2925
{
3026
parent::__construct();
@@ -36,25 +32,11 @@ public function __construct()
3632
);
3733
}
3834

39-
/**
40-
* {@inheritdoc}
41-
*/
4235
protected function canAccess()
4336
{
44-
if ($this->is_popup) {
45-
Auth::checkAuthentication(null, true);
46-
} else {
47-
Auth::checkAuthentication();
48-
}
49-
50-
$this->role_id = Auth::getCurrentRole();
51-
if ($this->role_id < $this->min_role) {
52-
if ($this->is_popup) {
53-
return false;
54-
}
55-
$this->error(ev_gettext('Sorry, you are not allowed to access this page.'));
56-
}
57-
37+
// if manage controller does not implement this
38+
// then give access permission.
39+
// probably canRoleAccess satisfied access restriction.
5840
return true;
5941
}
6042
}

src/Controller/PartnersController.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class PartnersController extends BaseController
2424
/** @var string */
2525
protected $tpl_name = 'select_partners.tpl.html';
2626

27+
/** @var int */
28+
protected $min_role = User::ROLE_DEVELOPER;
29+
30+
/** @var bool */
31+
protected $is_popup = true;
32+
2733
/** @var int */
2834
private $issue_id;
2935

@@ -52,12 +58,6 @@ protected function configure()
5258
*/
5359
protected function canAccess()
5460
{
55-
Auth::checkAuthentication(null, true);
56-
57-
if (Auth::getCurrentRole() <= User::ROLE_USER) {
58-
return false;
59-
}
60-
6161
$this->usr_id = Auth::getUserID();
6262

6363
if (Access::canViewIssuePartners($this->issue_id, $this->usr_id)) {

src/Controller/UpdateController.php

-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class UpdateController extends BaseController
5050
/** @var int */
5151
private $prj_id;
5252

53-
/** @var int */
54-
private $role_id;
55-
5653
/** @var array */
5754
private $details;
5855

src/Controller/ViewController.php

-3
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ class ViewController extends BaseController
5454
/** @var int */
5555
private $prj_id;
5656

57-
/** @var int */
58-
private $role_id;
59-
6057
/** @var int */
6158
private $issue_id;
6259

templates/error_message.tpl.html

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
{extends "base_full.tpl.html"}
1+
{if $is_popup}
2+
{assign var="base_template" value="base.tpl.html"}
3+
{else}
4+
{assign var="base_template" value="base_full.tpl.html"}
5+
{/if}
6+
{extends "$base_template"}
27
{block title}{t}Error{/t}{/block}
38

49
{block "content"}
5-
&nbsp;<a href="javascript:history.go(-1);">{t}Go Back{/t}</a>
6-
{/block}
10+
{if !$is_popup}
11+
&nbsp;<a href="javascript:history.go(-1);">{t}Go Back{/t}</a>
12+
{/if}
13+
{/block}

templates/permission_denied.tpl.html

-14
This file was deleted.

0 commit comments

Comments
 (0)