Skip to content
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

Automatic login when I logout from site in cakephp 5.x #1061

Open
mdeanquin0520 opened this issue Jan 15, 2024 · 2 comments
Open

Automatic login when I logout from site in cakephp 5.x #1061

mdeanquin0520 opened this issue Jan 15, 2024 · 2 comments

Comments

@mdeanquin0520
Copy link

mdeanquin0520 commented Jan 15, 2024

When I logout of the site the site logs me in again automatically.
This is the code for login action:

	public function login()
	{
		$this->Observations = $this->fetchTable('Observations');
		$this->Controllers = $this->fetchTable('Controllers');
		$this->ControllersRoles = $this->fetchTable('ControllersRoles');
		$this->MenuGroups = $this->fetchTable('MenuGroups');
		$result = $this->Authentication->getResult();
        if ($result->isValid()) {
            $user = $this->request->getAttribute('identity')->getOriginalData();
			$last_login = $user->last_login;
			$this->last_login = $last_login;
			$this->set('last_login', $last_login);
			$now = DateTime::now();
			$user->last_login = $now;
			$this->MyUsers->save($user);
			$observations = $this->Observations->newEmptyEntity();
			$user_id = $user->id;
			$username = $user->username;
			$observations->user_id = $user_id;
			$observations->observation = __('El usuario {0} ha ingresado al sistema', $username);
			$this->Observations->save($observations);

			$target = $this->Authentication->getLoginRedirect() ?? '/home';
			return $this->redirect($target);
        }
        if ($this->request->is('post')) {
			$this->Flash->error(__('Nombre de usuario o contraseña incorrectos.'));
        }
		$countControllers = $this->Controllers->find('all')->matching('ControllersTranslations')->count();
		$this->set('countControllers', $countControllers);
		$countMenu = $this->MenuGroups->find('all')->count();
		$this->set('countMenu', $countMenu);
		$countPermissions = $this->ControllersRoles->find('all')->count();
		$this->set('countPermissions', $countPermissions);
		$countRoles = $this->MyUsers->Roles->find('all')->count();
		$this->set('countRoles', $countRoles);
		$countUsers = $this->MyUsers->find('all')->count();
		$this->set('countUsers', $countUsers);
	}

This is the code for logout action:

	public function logout()
	{
		$this->Observations = $this->fetchTable('Observations');
        $user = $this->Authentication->getIdentity();
		$observations = $this->Observations->newEmptyEntity();
		$observations->user_id = $user->id;
		$observations->observation = __('El usuario {0} salió del sistema', $user->username);
		$this->Observations->save($observations);
		$session = $this->request->getSession();
		$session->destroy();
		$this->Flash->success(__d('cake_d_c/users', 'You\'ve successfully logged out'));
		return $this->redirect($this->Authentication->logout());
	}

And I have the default code for /config/users.php and /config/permissions.php
Let me clarify that the getAuthenticationService and getAuthorizationService from App\Application look different than what you put in the tutorial to configure the Authentication and Authorization services in this plugin, this is the code for getAuthenticationService method in Application.php:

    public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
    {
        $service = new AuthenticationService();

        $fields = [
            'username' => 'username',
            'password' => 'password'
        ];

        // Load identifiers
        $service->loadIdentifier('Authentication.Password', compact('fields'));

        // Load the authenticators, you want session first
        $service->loadAuthenticator('Authentication.Session', [
            'skipTwoFactorVerify' => true
        ]);
        $service->loadAuthenticator('Authentication.Form', [
            'fields' => $fields,
            'loginUrl' => Router::url(['controller' => 'MyUsers', 'action' => 'login'])
        ]);

        return $service;
    }

And this is the code for getAuthorizationService method in Application.php:

    public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
    {
        $map = new MapResolver();
		$map->map(
			ServerRequest::class,
			new CollectionPolicy([
				SuperuserPolicy::class,
				RbacPolicy::class,
			])
		);

		$orm = new OrmResolver();

		$resolver = new ResolverCollection([$map, $orm]);

		return new AuthorizationService($resolver);
    }     

As you can see my version of both methods doesn't have the ResponseInterface parameter because it gives me this error:
image
How can I solve this issue?

@mdeanquin0520 mdeanquin0520 changed the title Automatic login when I logout from system in cakephp 5.x Automatic login when I logout from site in cakephp 5.x Jan 16, 2024
@steinkel
Copy link
Member

If you are using the defaults, check this line > https://github.com/CakeDC/users/blob/11.next-cake4/config/users.php#L178 that enables the CookieAuthentication, also check your browser for a cookie set named CookieAuth, I think that could be the reason of the user auto-login.

About the type issues, check you are correctly importing the classes, it could be a class or interface not imported.

@rochamarcelo
Copy link
Collaborator

@mdeanquin0520 when using the plugin you have authentication|authorization pre-configured, check https://github.com/CakeDC/users/blob/11.next-cake4/Docs/Documentation/Authentication.md , https://github.com/CakeDC/users/blob/11.next-cake4/Docs/Documentation/Authorization.md and make sure to create a config/permissions.php file to allow access your controllers (https://github.com/CakeDC/users/blob/11.next-cake4/config/permissions.php)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants