Skip to content

Commit 3a906e6

Browse files
committed
Finish Authorization chapter.
1 parent 9c2b637 commit 3a906e6

6 files changed

Lines changed: 88 additions & 19 deletions

File tree

‎src/Controller/AppController.php‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,30 @@ class AppController extends Controller
4040
public function initialize()
4141
{
4242
parent::initialize();
43-
4443
$this->loadComponent('RequestHandler');
4544
$this->loadComponent('Flash');
4645

47-
/*
48-
* Enable the following components for recommended CakePHP security settings.
49-
* see https://book.cakephp.org/3.0/en/controllers/components/security.html
50-
*/
51-
//$this->loadComponent('Security');
52-
//$this->loadComponent('Csrf');
46+
$this->loadComponent('Auth', [
47+
'authorize'=> 'Controller',
48+
'authenticate' => [
49+
'Form' => [
50+
'fields' => [
51+
'username' => 'email',
52+
'password' => 'password'
53+
]
54+
]
55+
],
56+
'loginAction' => [
57+
'controller' => 'Users',
58+
'action' => 'login'
59+
],
60+
// If unauthorized, return them to page they were just on
61+
'unauthorizedRedirect' => $this->referer()
62+
]);
63+
64+
// Allow the display action so our PagesController
65+
// continues to work. Also enable the read only actions.
66+
$this->Auth->allow(['display', 'view', 'index']);
5367
}
5468

5569
/**
@@ -69,4 +83,10 @@ public function beforeRender(Event $event)
6983
$this->set('_serialize', true);
7084
}
7185
}
86+
87+
public function isAuthorized($user)
88+
{
89+
// By default deny access.
90+
return false;
91+
}
7292
}

‎src/Controller/ArticlesController.php‎

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function initialize()
1414

1515
$this->loadComponent('Flash'); // Include the FlashComponent
1616
$this->loadComponent('Paginator'); // Include the FlashComponent
17+
18+
$this->Auth->allow(['tags']);
1719
}
1820

1921
public function index()
@@ -33,18 +35,16 @@ public function add()
3335
$article = $this->Articles->newEntity();
3436
if ($this->request->is('post')) {
3537
$article = $this->Articles->patchEntity($article, $this->request->getData());
38+
39+
// Added: Set the user_id from the session.
40+
$article->user_id = $this->Auth->user('id');
41+
3642
if ($this->Articles->save($article)) {
3743
$this->Flash->success(__('Your article has been saved.'));
3844
return $this->redirect(['action' => 'index']);
3945
}
4046
$this->Flash->error(__('Unable to add your article.'));
4147
}
42-
// Get a list of tags.
43-
$tags = $this->Articles->Tags->find('list');
44-
45-
// Set tags to the view context
46-
$this->set('tags', $tags);
47-
4848
$this->set('article', $article);
4949
}
5050

@@ -54,8 +54,12 @@ public function edit($slug)
5454
->findBySlug($slug)
5555
->contain('Tags') // load associated Tags
5656
->firstOrFail();
57+
5758
if ($this->request->is(['post', 'put'])) {
58-
$this->Articles->patchEntity($article, $this->request->getData());
59+
$this->Articles->patchEntity($article, $this->request->getData(), [
60+
// Added: Disable modification of user_id.
61+
'accessibleFields' => ['user_id' => false]
62+
]);
5963
if ($this->Articles->save($article)) {
6064
$this->Flash->success(__('Your article has been updated.'));
6165
return $this->redirect(['action' => 'index']);
@@ -66,9 +70,8 @@ public function edit($slug)
6670
// Get a list of tags.
6771
$tags = $this->Articles->Tags->find('list');
6872

69-
// Set tags to the view context
73+
// Set article & tags to the view context
7074
$this->set('tags', $tags);
71-
7275
$this->set('article', $article);
7376
}
7477

@@ -100,4 +103,24 @@ public function tags()
100103
'tags' => $tags
101104
]);
102105
}
106+
107+
public function isAuthorized($user)
108+
{
109+
$action = $this->request->getParam('action');
110+
// The add and tags actions are always allowed to logged in users.
111+
if (in_array($action, ['add', 'tags'])) {
112+
return true;
113+
}
114+
115+
// All other actions require a slug.
116+
$slug = $this->request->getParam('pass.0');
117+
if (!$slug) {
118+
return false;
119+
}
120+
121+
// Check that the article belongs to the current user.
122+
$article = $this->Articles->findBySlug($slug)->first();
123+
124+
return $article->user_id === $user['id'];
125+
}
103126
}

‎src/Controller/UsersController.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
class UsersController extends AppController
1414
{
15+
public function initialize()
16+
{
17+
parent::initialize();
18+
$this->Auth->allow(['logout', 'add']);
19+
}
1520

1621
/**
1722
* Index method
@@ -26,6 +31,24 @@ public function index()
2631
$this->set('_serialize', ['users']);
2732
}
2833

34+
public function login()
35+
{
36+
if ($this->request->is('post')) {
37+
$user = $this->Auth->identify();
38+
if ($user) {
39+
$this->Auth->setUser($user);
40+
return $this->redirect($this->Auth->redirectUrl());
41+
}
42+
$this->Flash->error('Your username or password is incorrect.');
43+
}
44+
}
45+
46+
public function logout()
47+
{
48+
$this->Flash->success('You are now logged out.');
49+
return $this->redirect($this->Auth->logout());
50+
}
51+
2952
/**
3053
* View method
3154
*

‎src/Template/Articles/add.ctp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<h1>Add Article</h1>
44
<?php
55
echo $this->Form->create($article);
6-
// Hard code the user for now.
7-
echo $this->Form->control('user_id', ['type' => 'hidden', 'value' => 1]);
86
echo $this->Form->control('title');
97
echo $this->Form->control('body', ['rows' => '3']);
108
echo $this->Form->control('tag_string', ['type' => 'text']);

‎src/Template/Articles/edit.ctp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<h1>Edit Article</h1>
44
<?php
55
echo $this->Form->create($article);
6-
echo $this->Form->control('user_id', ['type' => 'hidden']);
76
echo $this->Form->control('title');
87
echo $this->Form->control('body', ['rows' => '3']);
98
echo $this->Form->control('tag_string', ['type' => 'text']);

‎src/Template/Users/login.ctp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Login</h1>
2+
<?= $this->Form->create() ?>
3+
<?= $this->Form->control('email') ?>
4+
<?= $this->Form->control('password') ?>
5+
<?= $this->Form->button('Login') ?>
6+
<?= $this->Form->end() ?>

0 commit comments

Comments
 (0)