-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect Authorization plugin and include it in bake output #993
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,3 @@ indent_size = 2 | |
|
||
[phars.xml] | ||
indent_size = 2 | ||
|
||
[*.twig] | ||
insert_final_newline = false | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,9 @@ | |
public function add() | ||
{ | ||
${{ singularName }} = $this->{{ currentModelName }}->newEmptyEntity(); | ||
{% if Bake.hasPlugin('Authorization') %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am curious about the use case for authorizing using an empty entity. Personally I have only authorized the "add" action. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applications could have roles or rules on who can create what. I agree that most of the time add() will have a policy of |
||
$this->Authorization->authorize(${{ singularName }}); | ||
{% endif %} | ||
if ($this->request->is('post')) { | ||
${{ singularName }} = $this->{{ currentModelName }}->patchEntity(${{ singularName }}, $this->request->getData()); | ||
if ($this->{{ currentModelName }}->save(${{ singularName }})) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,9 @@ | |
->contain({{ Bake.exportArray(belongsTo)|raw }}); | ||
{% else %} | ||
$query = $this->{{ currentModelName }}->find(); | ||
{% endif %} | ||
{% if Bake.hasPlugin('Authorization') %} | ||
$query = $this->Authorization->applyScope($query); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to bake stub code for these? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is helpful. I usually use this to build in permissions like 'view all the projects I have membership in'. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, should we bake/generate the authorization code stubs to match this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That can be done already with |
||
{% endif %} | ||
${{ pluralName }} = $this->paginate($query); | ||
|
||
|
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']); | ||
} | ||
} |
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'; | ||
} |
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 | ||
{ | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was some file that needed this, but I can't remember which one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah whitespace changes in templates can affect the display, so I wouldn't auto add/remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@markstory Can you revert this please.