Passed
Pull Request — master (#38)
by giu
04:13
created

UsersController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace App\Controller\Admin;
4
5
use App\Controller\AppController;
6
use Cake\Event\EventInterface;
7
8
/**
9
 * Users Controller
10
 *
11
 * @property \App\Model\Table\UsersTable $Users
12
 */
13
class UsersController extends AppController {
14
15 3
    public function beforeFilter(EventInterface $event) {
16 3
        parent::beforeFilter($event);
17 3
        $this->Auth->allow('add','login','logout');
18 3
    }
19
20
    /**
21
     * Index method
22
     *
23
     * @return \Cake\Network\Response|null
24
     */
25 1
    public function index() {
26 1
        $users = $this->paginate($this->Users);
27
28 1
        $this->set(compact('users'));
29 1
        $this->set('_serialize', ['users']);
30 1
    }
31
32
    /**
33
     * View method
34
     *
35
     * @param string|null $id User id.
36
     * @return \Cake\Network\Response|null
37
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
38
     */
39
    public function view($id = null) {
40
        $user = $this->Users->get($id, [
41
            'contain' => []
42
        ]);
43
44
        $this->set('user', $user);
45
        $this->set('_serialize', ['user']);
46
    }
47
48
    /**
49
     * Add method
50
     *
51
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
52
     */
53
    public function add() {
54
        $user = $this->Users->newEmptyEntity();
55
        if ($this->request->is('post')) {
56
            $user = $this->Users->patchEntity($user, $this->request->getData());
57
            if ($this->Users->save($user)) {
58
                $this->Flash->success(__('The user has been saved.'));
59
60
                return $this->redirect(['prefix' => 'admin', 'controller' => 'users', 'action' => 'index']);
61
            }
62
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
63
        }
64
        $this->set(compact('user'));
65
        $this->set('_serialize', ['user']);
66
    }
67
68
    /**
69
     * Edit method
70
     *
71
     * @param string|null $id User id.
72
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
73
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
74
     */
75
    public function edit($id = null) {
76
        $user = $this->Users->get($id, [
77
            'contain' => []
78
        ]);
79
        if ($this->request->is(['patch', 'post', 'put'])) {
80
            $user = $this->Users->patchEntity($user, $this->request->data);
81
            if ($this->Users->save($user)) {
82
                $this->Flash->success(__('The user has been saved.'));
83
84
                return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
85
            }
86
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
87
        }
88
        $this->set(compact('user'));
89
        $this->set('_serialize', ['user']);
90
    }
91
92
    /**
93
     * Delete method
94
     *
95
     * @param string|null $id User id.
96
     * @return \Cake\Network\Response|null Redirects to index.
97
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
98
     */
99 1
    public function delete($id = null) {
100 1
        $this->request->allowMethod(['post', 'delete']);
101 1
        $user = $this->Users->get($id);
102 1
        if ($this->Users->delete($user)) {
103 1
            $this->Flash->success(__('The user has been deleted.'));
104
        } else {
105
            $this->Flash->error(__('The user could not be deleted. Please, try again.'));
106
        }
107
108 1
        return $this->redirect(['prefix' => 'Admin', 'controller' => 'Users', 'action' => 'index']);
109
    }
110
111
    public function login() {
112
        if ($this->request->is('post')) {
113
            $user = $this->Auth->identify();
114
            $this->set('lu', $user);
115
            if ($user) {
116
                $this->Auth->setUser($user);
117
                return $this->redirect($this->Auth->redirectUrl());
118
            }
119
            $this->Flash->error(__('Invalid username or password, try again'));
120
        }
121
    }
122
123 1
    public function logout() {
124 1
        return $this->redirect($this->Auth->logout());
125
    }
126
127
}
128