Passed
Branch master (44bfae)
by giu
03:41
created

UsersController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
use Cake\Event\Event;
6
7
/**
8
 * Users Controller
9
 *
10
 * @property \App\Model\Table\UsersTable $Users
11
 */
12
class UsersController extends AppController
13
{
14
15 6
    public function beforeFilter(Event $event)
16
    {
17 6
        parent::beforeFilter($event);
18 6
        $this->Auth->allow('add');
19 6
    }
20
	
21
	
22
	/**
23
     * Index method
24
     *
25
     * @return \Cake\Network\Response|null
26
     */
27 1
    public function index()
28
    {
29 1
        $users = $this->paginate($this->Users);
30
31 1
        $this->set(compact('users'));
32 1
        $this->set('_serialize', ['users']);
33 1
    }
34
35
    /**
36
     * View method
37
     *
38
     * @param string|null $id User id.
39
     * @return \Cake\Network\Response|null
40
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
41
     */
42
    public function view($id = null)
43
    {
44
        $user = $this->Users->get($id, [
45
            'contain' => []
46
        ]);
47
48
        $this->set('user', $user);
49
        $this->set('_serialize', ['user']);
50
    }
51
52
    /**
53
     * Add method
54
     *
55
     * @return \Cake\Network\Response|null Redirects on successful add, renders view otherwise.
56
     */
57 3
    public function add()
58
    {
59 3
        $user = $this->Users->newEntity();
60 3
        if ($this->request->is('post')) {
61 3
            $user = $this->Users->patchEntity($user, $this->request->data);
0 ignored issues
show
Bug introduced by
It seems like $this->request->data can also be of type object; however, parameter $data of App\Model\Table\UsersTable::patchEntity() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
            $user = $this->Users->patchEntity($user, /** @scrutinizer ignore-type */ $this->request->data);
Loading history...
Deprecated Code introduced by
The property Cake\Http\ServerRequest::$data has been deprecated: 3.4.0 This public property will be removed in 4.0.0. Use getData() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            $user = $this->Users->patchEntity($user, /** @scrutinizer ignore-deprecated */ $this->request->data);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
62 3
            if ($this->Users->save($user)) {
63 1
                $this->Flash->success(__('The user has been saved.'));
64
65 1
                return $this->redirect(['prefix'=>'admin', 'controller'=>'Users', 'action' => 'index']);
66
            }
67 2
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
68
        }
69 2
        $this->set(compact('user'));
70 2
        $this->set('_serialize', ['user']);
71 2
    }
72
73
    /**
74
     * Edit method
75
     *
76
     * @param string|null $id User id.
77
     * @return \Cake\Network\Response|null Redirects on successful edit, renders view otherwise.
78
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
79
     */
80
    public function edit($id = null)
81
    {
82
        $user = $this->Users->get($id, [
83
            'contain' => []
84
        ]);
85
        if ($this->request->is(['patch', 'post', 'put'])) {
86
            $user = $this->Users->patchEntity($user, $this->request->data);
0 ignored issues
show
Deprecated Code introduced by
The property Cake\Http\ServerRequest::$data has been deprecated: 3.4.0 This public property will be removed in 4.0.0. Use getData() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

86
            $user = $this->Users->patchEntity($user, /** @scrutinizer ignore-deprecated */ $this->request->data);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Bug introduced by
It seems like $this->request->data can also be of type object; however, parameter $data of App\Model\Table\UsersTable::patchEntity() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

86
            $user = $this->Users->patchEntity($user, /** @scrutinizer ignore-type */ $this->request->data);
Loading history...
87
            if ($this->Users->save($user)) {
88
                $this->Flash->success(__('The user has been saved.'));
89
90
                return $this->redirect(['prefix'=>'admin', 'controller'=>'Users', 'action' => 'index']);
91
            }
92
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
93
        }
94
        $this->set(compact('user'));
95
        $this->set('_serialize', ['user']);
96
    }
97
98
    /**
99
     * Delete method
100
     *
101
     * @param string|null $id User id.
102
     * @return \Cake\Network\Response|null Redirects to index.
103
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
104
     */
105 1
    public function delete($id = null)
106
    {
107 1
        $this->request->allowMethod(['post', 'delete']);
108 1
        $user = $this->Users->get($id);
109 1
        if ($this->Users->delete($user)) {
110 1
            $this->Flash->success(__('The user has been deleted.'));
111
        } else {
112
            $this->Flash->error(__('The user could not be deleted. Please, try again.'));
113
        }
114
115 1
        return $this->redirect(['prefix'=>'admin', 'controller'=>'Users', 'action' => 'index']);
116
    }
117
	
118
	public function login() {
119
		if ($this->request->is('post')) {
120
            $user = $this->Auth->identify();
121
			$this->set('lu', $user);
122
            if ($user) {
123
                $this->Auth->setUser($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type true; however, parameter $user of Cake\Controller\Component\AuthComponent::setUser() does only seem to accept ArrayAccess|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
                $this->Auth->setUser(/** @scrutinizer ignore-type */ $user);
Loading history...
124
                return $this->redirect($this->Auth->redirectUrl());
125
            }
126
            $this->Flash->error(__('Invalid username or password, try again'));
127
        }
128
	}
129 1
	public function logout() 
130
	{
131 1
		return $this->redirect($this->Auth->logout());
132
	}
133
}
134