Skip to content

Commit e696777

Browse files
committed
Do not add uris to transition dropdown items if use is not allowed to apply
1 parent 7336e95 commit e696777

File tree

2 files changed

+65
-18
lines changed

2 files changed

+65
-18
lines changed

src/Admin/Extension/WorkflowExtension.php

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,7 @@ public function configureSideMenu(
7878
}
7979

8080
$subject = $admin->getSubject();
81-
if (null === $subject) {
82-
return;
83-
}
84-
85-
try {
86-
$admin->checkAccess('viewTransitions', $subject);
87-
} catch (AccessDeniedException $exception) {
81+
if (null === $subject || !$this->isGrantedView($admin, $subject)) {
8882
return;
8983
}
9084

@@ -210,13 +204,16 @@ protected function transitionsDropdown(MenuItemInterface $menu, AdminInterface $
210204
protected function transitionsItem(MenuItemInterface $menu, AdminInterface $admin, Transition $transition, $subject)
211205
{
212206
$options = [
213-
'uri' => $this->generateTransitionUri($admin, $transition, $subject),
214207
'attributes' => [],
215208
'extras' => [
216209
'translation_domain' => $admin->getTranslationDomain(),
217210
],
218211
];
219212

213+
if ($this->isGrantedApply($admin, $subject)) {
214+
$options['uri'] = $this->generateTransitionUri($admin, $transition, $subject);
215+
}
216+
220217
if ($icon = $this->getTransitionIcon($transition)) {
221218
$options['attributes']['icon'] = $icon;
222219
}
@@ -256,4 +253,38 @@ protected function generateTransitionUri(AdminInterface $admin, Transition $tran
256253
['transition' => $transition->getName()]
257254
);
258255
}
256+
257+
/**
258+
* @param AdminInterface $admin
259+
* @param object $subject
260+
*
261+
* @return bool
262+
*/
263+
protected function isGrantedView(AdminInterface $admin, $subject)
264+
{
265+
try {
266+
$admin->checkAccess('viewTransitions', $subject);
267+
} catch (AccessDeniedException $exception) {
268+
return false;
269+
}
270+
271+
return true;
272+
}
273+
274+
/**
275+
* @param AdminInterface $admin
276+
* @param object $subject
277+
*
278+
* @return bool
279+
*/
280+
protected function isGrantedApply(AdminInterface $admin, $subject)
281+
{
282+
try {
283+
$admin->checkAccess('applyTransitions', $subject);
284+
} catch (AccessDeniedException $exception) {
285+
return false;
286+
}
287+
288+
return true;
289+
}
259290
}

tests/Admin/Extension/WorkflowExtensionTest.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function testConfigureSideMenuWithoutWorkflow()
123123
/**
124124
* @dataProvider markingToTransition
125125
*/
126-
public function testConfigureSideMenu($marking, array $transitions)
126+
public function testConfigureSideMenu($marking, array $transitions, $grantedApply)
127127
{
128128
$pullRequest = new PullRequest();
129129
$pullRequest->setMarking($marking);
@@ -137,14 +137,24 @@ public function testConfigureSideMenu($marking, array $transitions)
137137
$admin->getLabelTranslatorStrategy()->willReturn($labelStrategy->reveal());
138138
$admin->getSubject()->willReturn($pullRequest);
139139
$admin->checkAccess('viewTransitions', $pullRequest)->shouldBeCalled();
140+
if ($grantedApply) {
141+
$admin->checkAccess('applyTransitions', $pullRequest)->shouldBeCalledTimes(count($transitions));
142+
} else {
143+
$admin->checkAccess('applyTransitions', $pullRequest)->willThrow(new AccessDeniedException());
144+
}
140145

141146
foreach ($transitions as $transition) {
142147
$labelStrategy->getLabel($transition, 'workflow', 'transition')
143148
->shouldBeCalledTimes(1)
144149
->willReturn('workflow.transition.'.$transition);
145-
$admin->generateObjectUrl('workflow_apply_transition', $pullRequest, ['transition' => $transition])
146-
->shouldBeCalledTimes(1)
147-
->willReturn('/pull-request/42/workflow/transition/'.$transition.'/apply');
150+
if ($grantedApply) {
151+
$admin->generateObjectUrl('workflow_apply_transition', $pullRequest, ['transition' => $transition])
152+
->shouldBeCalledTimes(1)
153+
->willReturn('/pull-request/42/workflow/transition/'.$transition.'/apply');
154+
} else {
155+
$admin->generateObjectUrl('workflow_apply_transition', $pullRequest, ['transition' => $transition])
156+
->shouldNotBeCalled();
157+
}
148158
}
149159

150160
$registry = new Registry();
@@ -182,7 +192,11 @@ public function testConfigureSideMenu($marking, array $transitions)
182192
}
183193

184194
self::assertNotNull($item = $child->getChild('workflow.transition.'.$transition));
185-
self::assertSame('/pull-request/42/workflow/transition/'.$transition.'/apply', $item->getUri());
195+
if ($grantedApply) {
196+
self::assertSame('/pull-request/42/workflow/transition/'.$transition.'/apply', $item->getUri());
197+
} else {
198+
self::assertNull($item->getUri());
199+
}
186200
self::assertSame('admin', $item->getExtra('translation_domain'));
187201
self::assertSame($icon, $item->getAttribute('icon'));
188202
}
@@ -191,10 +205,12 @@ public function testConfigureSideMenu($marking, array $transitions)
191205

192206
public function markingToTransition()
193207
{
194-
return [
195-
'opened' => ['opened', ['start_review']],
196-
'pending_review' => ['pending_review', ['merge', 'close']],
197-
'closed' => ['closed', []],
198-
];
208+
foreach ([true, false] as $grantedApply) {
209+
$grantedApplyStr = $grantedApply ? 'with links' : 'without links';
210+
211+
yield 'opened '.$grantedApplyStr => ['opened', ['start_review'], $grantedApply];
212+
yield 'pending_review '.$grantedApplyStr => ['pending_review', ['merge', 'close'], $grantedApply];
213+
yield 'closed '.$grantedApplyStr => ['closed', [], $grantedApply];
214+
}
199215
}
200216
}

0 commit comments

Comments
 (0)