Skip to content

Compare version to use AuthClientService#7

Open
sebmennetrier wants to merge 1 commit intomasterfrom
fix-humhub-1.15
Open

Compare version to use AuthClientService#7
sebmennetrier wants to merge 1 commit intomasterfrom
fix-humhub-1.15

Conversation

@sebmennetrier
Copy link
Contributor

@sebmennetrier sebmennetrier commented Jan 8, 2024

Try to fix issue #5

Copy link

@ArchBlood ArchBlood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it now does work, although the following code should be done in a controller not the Events.php;

if ("user/auth/login" === Yii::$app->request->getPathInfo()) {
    Yii::$app->getResponse()->redirect(['/user/password-recovery']);
}

@ArchBlood
Copy link

ArchBlood commented Jan 8, 2024

Here's an example

AuthController.php

<?php

namespace humhub\auth\basic\controllers;

use Yii;
use humhub\modules\user\controllers\AuthController as BaseAuthController;

class AuthController extends BaseAuthController
{
    public function actionLogin()
    {
        // Call the parent action method to retain the base functionality
        $result = parent::actionLogin();

        // Perform your additional logic here
        if ($this->shouldRedirectToRecovery()) {
            Yii::$app->getResponse()->redirect(['/user/password-recovery'])->send();
            Yii::$app->end();
        }

        return $result;
    }

    /**
     * Example method to determine if redirection should occur
     * Modify this method based on your specific conditions
     */
    private function shouldRedirectToRecovery()
    {
        return ("user/auth/login" === Yii::$app->request->getPathInfo());
        // Add more conditions as needed to determine when to redirect
    }
}

Events.php

<?php

namespace humhub\auth\basic;

use Yii;
use humhub\components\Request;
use humhub\modules\user\models\forms\Login;
use humhub\auth\basic\controllers\AuthController;
use humhub\modules\user\services\AuthClientService;
use humhub\modules\user\authclient\AuthClientHelpers;

class Events
{
    /**
     * @param Event $event
     */
    public static function onBeforeRequest($event)
    {
        if (!Yii::$app->has('request')) {
            return;
        }

        $request = Yii::$app->request;
        list($username, $password) = $request->getAuthCredentials();
        $identity = Yii::$app->user->getIdentity();

        if ($username != null && $password != null && $identity == null) {
            $login = new Login;
            if ($login->load(['username' => $username, 'password' => $password], '') && $login->validate()) {
                if (version_compare(Yii::$app->version, '1.14', '>=')) {
                    $authClientService = new AuthClientService($login->authClient);
                    $user = $authClientService->getUser();
                    if ($user == null) {
                        $user = $authClientService->createUser($login->authClient);
                    }
                    if ($user != null) {
                        Yii::$app->user->login($user);
                    }
                } else {
                    $user = AuthClientHelpers::getUserByAuthClient($login->authClient);
                    if ($user == null) {
                        $user = AuthClientHelpers::createUser($login->authClient);
                    }
                    if ($user != null) {
                        Yii::$app->user->login($user);
                    }
                }
            }
        }

        $authController = new AuthController('auth', Yii::$app->getModule('user'));
        $authController->actionLogin();
    }
}

@CodeShakingSheep
Copy link

Hi @sebmennetrier ,
Thank you for the fix. Yes, like this it works correctly on my system. 👍

@buzzzo
Copy link

buzzzo commented May 2, 2024

Here's an example

AuthController.php

<?php

namespace humhub\auth\basic\controllers;

use Yii;
use humhub\modules\user\controllers\AuthController as BaseAuthController;

class AuthController extends BaseAuthController
{
    public function actionLogin()
    {
        // Call the parent action method to retain the base functionality
        $result = parent::actionLogin();

        // Perform your additional logic here
        if ($this->shouldRedirectToRecovery()) {
            Yii::$app->getResponse()->redirect(['/user/password-recovery'])->send();
            Yii::$app->end();
        }

        return $result;
    }

    /**
     * Example method to determine if redirection should occur
     * Modify this method based on your specific conditions
     */
    private function shouldRedirectToRecovery()
    {
        return ("user/auth/login" === Yii::$app->request->getPathInfo());
        // Add more conditions as needed to determine when to redirect
    }
}

Events.php

<?php

namespace humhub\auth\basic;

use Yii;
use humhub\components\Request;
use humhub\modules\user\models\forms\Login;
use humhub\auth\basic\controllers\AuthController;
use humhub\modules\user\services\AuthClientService;
use humhub\modules\user\authclient\AuthClientHelpers;

class Events
{
    /**
     * @param Event $event
     */
    public static function onBeforeRequest($event)
    {
        if (!Yii::$app->has('request')) {
            return;
        }

        $request = Yii::$app->request;
        list($username, $password) = $request->getAuthCredentials();
        $identity = Yii::$app->user->getIdentity();

        if ($username != null && $password != null && $identity == null) {
            $login = new Login;
            if ($login->load(['username' => $username, 'password' => $password], '') && $login->validate()) {
                if (version_compare(Yii::$app->version, '1.14', '>=')) {
                    $authClientService = new AuthClientService($login->authClient);
                    $user = $authClientService->getUser();
                    if ($user == null) {
                        $user = $authClientService->createUser($login->authClient);
                    }
                    if ($user != null) {
                        Yii::$app->user->login($user);
                    }
                } else {
                    $user = AuthClientHelpers::getUserByAuthClient($login->authClient);
                    if ($user == null) {
                        $user = AuthClientHelpers::createUser($login->authClient);
                    }
                    if ($user != null) {
                        Yii::$app->user->login($user);
                    }
                }
            }
        }

        $authController = new AuthController('auth', Yii::$app->getModule('user'));
        $authController->actionLogin();
    }
}

Overall it now does work, although the following code should be done in a controller not the Events.php;

if ("user/auth/login" === Yii::$app->request->getPathInfo()) {
    Yii::$app->getResponse()->redirect(['/user/password-recovery']);
}

Would not be better if you fork the code and apply your own fix ?
The author of this module seems not to be focus on it anymore and this module is very useful for scenarios where external sso is used.

Thx

@ArchBlood
Copy link

Would not be better if you fork the code and apply your own fix ?
The author of this module seems not to be focus on it anymore and this module is very useful for scenarios where external sso is used.

Thx

I no longer have a client that uses the module, so maintaining it through a fork doesn't make much sense for me at this time.

@CodeShakingSheep
Copy link

@sebmennetrier Can you merge this PR and release a new version please?

@ArchBlood
Copy link

@sebmennetrier Can you merge this PR and release a new version please?

Seems like even I haven't been able to get any answers back from the developer(s) (i.e. #8) about the current status of development on this or any other modules they maintain, and I can't just maintain the project due to a no license clause.

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

Successfully merging this pull request may close these issues.

4 participants