简体   繁体   English

错字3:在FE登录上创建用户

[英]Typo 3: Create user on FE login

The situation: 情况:
I build an authentication service that uses Basic Authentication to check if the user exists on an external database and fetches some data. 我构建了一个使用基本身份验证的身份验证服务,以检查用户是否存在于外部数据库中并获取一些数据。 The users in question only exist on the external database. 有问题的用户仅存在于外部数据库上。

The problem: 问题:
Typo3 needs to have an user entry in the fe_user table to login the user. Typo3需要在fe_user表中具有一个用户条目才能登录该用户。 So whenever this entry does not exist, the user cannot login. 因此,只要该条目不存在,用户就无法登录。

What I want to do: 我想做的事:
Create the user in the authentication service to avoid using a sql dump from the external database and ensure that synchronisation is possible. 在身份验证服务中创建用户,以避免使用来自外部数据库的sql转储,并确保可以进行同步。

The relevant code: 相关代码:

public function authUser(array $user) {
    $a_user = $this->login['uname'];
    $a_pwd = $this->login['uident_text'];
    $url = 'https://soliday.fluchtpunkt.at/api/queryMediaItems';
    $data = json_decode('{"language":"de-at"}');
    $basicAuth = base64_encode("$a_user:$a_pwd");

    // use key 'http' even if you send the request to https://...
    $options = array (
            'http' => array (
                    'header' => array(
                                "Content-Type: application/json",
                                "Accept: application/json",
                                "Authorization: Basic {$basicAuth}"
                            ),
                    'method' => 'POST',
                    'content' => '{"language":"de-at"}'
            ) 
    );
    $context = stream_context_create ( $options );
    $result = file_get_contents ($url, false, $context);
    $response = gzdecode($result);
    $checkUser = $this->fetchUserRecord ( $this->login ['uname'] );
    if (!is_array($checkUser)&& $result!== FALSE) {
        $this->createUser();
    }
    // failure
    if ($result === FALSE) {
        return static::STATUS_AUTHENTICATION_FAILURE_BREAK;
    }
    $this->processData($response);

    // success
    return static::STATUS_AUTHENTICATION_SUCCESS_BREAK;
}

public function createUser() {
    $username = $this->login ['uname'];
    $password = $this->login ['uident_text'];
    $record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', "username = '" . $username . "' AND disable = 0 AND deleted = 0" );
    if (! $record) {
        // user has no DB record (yet), create one using defaults registered in extension config
        // password is not important, username is set to the user's input
        $record = array (
                'username' => $username,
                'password' => $password,
                'name' => '',
                'email' => '',
                'disable' => '0',
                'deleted' => '0',
                'pid' => $this->config ['storagePid'],
                'usergroup' => $this->config ['addUsersToGroups'],
                'tstamp' => time () 
        );
        if (t3lib_extMgm::isLoaded ( 'extbase' )) {
            $record ['tx_extbase_type'] = $this->config ['recordType'];
        }
        $GLOBALS ['TYPO3_DB']->exec_INSERTquery ( 'fe_users', $record );
        $uid = $GLOBALS ['TYPO3_DB']->sql_insert_id ();
        $record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', 'uid = ' . intval ( $uid ) );
    }
    $_SESSION [$this->sessionKey] ['user'] ['fe'] = $record;
}

the ext_localconf.php file: ext_localconf.php文件:

   <?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
    $_EXTKEY,
    'auth' /* sv type */,
    'AuthService' /* sv key */,
    array(
        'title' => 'GET Authentication service',
        'description' => 'Authenticates users with GET request.',

        'subtype' => 'getUserFE, authUserFE',

        'available' => true,
        'priority' => 90,
        'quality' => 90,

        'os' => '',
        'exec' => '',

        'className' => Plaspack\professionalZoneLogin\Service\AuthService::class,
    )
);

您应该使用自己的代码扩展AuthenticationService,此处描述了这样做的方法https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Xclasses/Index.html

Not sure if it's related, but t3lib_extMgm should be \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility unless you're using TYPO3 6. 不确定是否相关,但是除非您使用TYPO3 6,否则t3lib_extMgm应该为\\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility

You can also see if you get any SQL errors by calling $GLOBALS['TYPO3_DB']->sql_error() . 您还可以通过调用$GLOBALS['TYPO3_DB']->sql_error()来查看是否遇到任何SQL错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM