Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- namespace App\Controller;
- use Authentication\AuthenticationService;
- use Authentication\AuthenticationServiceInterface;
- use Authentication\AuthenticationServiceProviderInterface;
- use Authentication\Middleware\AuthenticationMiddleware;
- use Psr\Http\Message\ServerRequestInterface;
- use Cake\Auth\DefaultPasswordHasher;
- /**
- * Users Controller
- *
- * @property \App\Model\Table\UsersTable $Users
- * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
- */
- class UsersController extends AppController
- {
- public function beforeFilter(\Cake\Event\EventInterface $event)
- {
- parent::beforeFilter($event);
- // Configure the login action to not require authentication, preventing
- // the infinite redirect loop issue
- $this->Authentication->addUnauthenticatedActions(['login', 'add', 'index']);
- }
- /**
- * Index method
- *
- * @return \Cake\Http\Response|null|void Renders view
- */
- public function index()
- {
- $users = $this->paginate($this->Users);
- $this->set(compact('users'));
- }
- public function login()
- {
- $result = $this->Authentication->getResult();
- // If the user is logged in send them away.
- if ($result->isValid()) {
- $target = $this->Authentication->getLoginRedirect() ?? '/products';
- return $this->redirect($target);
- }
- if ($this->request->is('post') && !$result->isValid()) {
- $this->Flash->error('Invalid username or password');
- }
- }
- public function logout()
- {
- $result = $this->Authentication->getResult();
- // regardless of POST or GET, redirect if user is logged in
- if ($result->isValid()) {
- $this->Authentication->logout();
- return $this->redirect(['controller' => 'Users', 'action' => 'login']);
- }
- }
- /**
- * Add method
- *
- * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
- */
- public function add()
- {
- $user = $this->Users->newEmptyEntity();
- if ($this->request->is('post')) {
- $user = $this->Users->patchEntity($user, $this->request->getData());
- if ($this->Users->save($user)) {
- $this->Flash->success(__('The user has been saved.'));
- return $this->redirect(['action' => 'index']);
- }
- $this->Flash->error(__('The user could not be saved. Please, try again.'));
- }
- $this->set(compact('user'));
- }
- /**
- * Edit method
- *
- * @param string|null $id User 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)
- {
- $user = $this->Users->get($id, [
- 'contain' => [],
- ]);
- if ($this->request->is(['patch', 'post', 'put'])) {
- $user = $this->Users->patchEntity($user, $this->request->getData());
- if ($this->Users->save($user)) {
- $this->Flash->success(__('The user has been saved.'));
- return $this->redirect(['action' => 'index']);
- }
- $this->Flash->error(__('The user could not be saved. Please, try again.'));
- }
- $this->set(compact('user'));
- }
- /**
- * Delete method
- *
- * @param string|null $id User id.
- * @return \Cake\Http\Response|null|void Redirects to index.
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function delete($id = null)
- {
- $this->request->allowMethod(['post', 'delete']);
- $user = $this->Users->get($id);
- if ($this->Users->delete($user)) {
- $this->Flash->success(__('The user has been deleted.'));
- } else {
- $this->Flash->error(__('The user could not be deleted. Please, try again.'));
- }
- return $this->redirect(['action' => 'index']);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement