-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #993 from cakephp/authorization-template
Detect Authorization plugin and include it in bake output
- Loading branch information
Showing
10 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
tests/comparisons/Controller/testBakeActionsAuthorizationPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bake\Test\App\Controller; | ||
|
||
/** | ||
* BakeArticles Controller | ||
* | ||
* @property \Bake\Test\App\Model\Table\BakeArticlesTable $BakeArticles | ||
* @property \Authorization\Controller\Component\AuthorizationComponent $Authorization | ||
*/ | ||
class BakeArticlesController extends AppController | ||
{ | ||
/** | ||
* Initialize controller | ||
* | ||
* @return void | ||
*/ | ||
public function initialize(): void | ||
{ | ||
parent::initialize(); | ||
|
||
$this->loadComponent('Authorization.Authorization'); | ||
} | ||
|
||
/** | ||
* Index method | ||
* | ||
* @return \Cake\Http\Response|null|void Renders view | ||
*/ | ||
public function index() | ||
{ | ||
$query = $this->BakeArticles->find() | ||
->contain(['BakeUsers']); | ||
$query = $this->Authorization->applyScope($query); | ||
$bakeArticles = $this->paginate($query); | ||
|
||
$this->set(compact('bakeArticles')); | ||
} | ||
|
||
/** | ||
* View method | ||
* | ||
* @param string|null $id Bake Article id. | ||
* @return \Cake\Http\Response|null|void Renders view | ||
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. | ||
*/ | ||
public function view($id = null) | ||
{ | ||
$bakeArticle = $this->BakeArticles->get($id, contain: ['BakeUsers', 'BakeTags', 'BakeComments']); | ||
$this->Authorization->authorize($bakeArticle); | ||
$this->set(compact('bakeArticle')); | ||
} | ||
|
||
/** | ||
* Add method | ||
* | ||
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. | ||
*/ | ||
public function add() | ||
{ | ||
$bakeArticle = $this->BakeArticles->newEmptyEntity(); | ||
$this->Authorization->authorize($bakeArticle); | ||
if ($this->request->is('post')) { | ||
$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->getData()); | ||
if ($this->BakeArticles->save($bakeArticle)) { | ||
$this->Flash->success(__('The bake article has been saved.')); | ||
|
||
return $this->redirect(['action' => 'index']); | ||
} | ||
$this->Flash->error(__('The bake article could not be saved. Please, try again.')); | ||
} | ||
$bakeUsers = $this->BakeArticles->BakeUsers->find('list', limit: 200)->all(); | ||
$bakeTags = $this->BakeArticles->BakeTags->find('list', limit: 200)->all(); | ||
$this->set(compact('bakeArticle', 'bakeUsers', 'bakeTags')); | ||
} | ||
|
||
/** | ||
* Edit method | ||
* | ||
* @param string|null $id Bake Article id. | ||
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise. | ||
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. | ||
*/ | ||
public function edit($id = null) | ||
{ | ||
$bakeArticle = $this->BakeArticles->get($id, contain: ['BakeTags']); | ||
$this->Authorization->authorize($bakeArticle); | ||
if ($this->request->is(['patch', 'post', 'put'])) { | ||
$bakeArticle = $this->BakeArticles->patchEntity($bakeArticle, $this->request->getData()); | ||
if ($this->BakeArticles->save($bakeArticle)) { | ||
$this->Flash->success(__('The bake article has been saved.')); | ||
|
||
return $this->redirect(['action' => 'index']); | ||
} | ||
$this->Flash->error(__('The bake article could not be saved. Please, try again.')); | ||
} | ||
$bakeUsers = $this->BakeArticles->BakeUsers->find('list', limit: 200)->all(); | ||
$bakeTags = $this->BakeArticles->BakeTags->find('list', limit: 200)->all(); | ||
$this->set(compact('bakeArticle', 'bakeUsers', 'bakeTags')); | ||
} | ||
|
||
/** | ||
* Delete method | ||
* | ||
* @param string|null $id Bake Article id. | ||
* @return \Cake\Http\Response|null Redirects to index. | ||
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. | ||
*/ | ||
public function delete($id = null) | ||
{ | ||
$this->request->allowMethod(['post', 'delete']); | ||
$bakeArticle = $this->BakeArticles->get($id); | ||
$this->Authorization->authorize($bakeArticle); | ||
if ($this->BakeArticles->delete($bakeArticle)) { | ||
$this->Flash->success(__('The bake article has been deleted.')); | ||
} else { | ||
$this->Flash->error(__('The bake article could not be deleted. Please, try again.')); | ||
} | ||
|
||
return $this->redirect(['action' => 'index']); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
tests/test_app/Plugin/Authorization/src/AuthorizationPlugin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Authorization; | ||
|
||
use Cake\Core\BasePlugin; | ||
|
||
/** | ||
* Plugin class stub for Authorization tests | ||
*/ | ||
class AuthorizationPlugin extends BasePlugin | ||
{ | ||
protected ?string $name = 'Authorization'; | ||
} |
10 changes: 10 additions & 0 deletions
10
tests/test_app/Plugin/Authorization/src/Controller/Component/AuthorizationComponent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Authorization\Controller\Component; | ||
|
||
use Cake\Controller\Component; | ||
|
||
class AuthorizationComponent extends Component | ||
{ | ||
} |