如何(正确)扩展ScnSocialAuth \ Authentication \ Adapter \ HybridAuth :: authenticate()方法? [英] How to (correctly) extend ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method?

查看:88
本文介绍了如何(正确)扩展ScnSocialAuth \ Authentication \ Adapter \ HybridAuth :: authenticate()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ScnSocialAuth ZF2模块在我的项目中启用社交媒体身份验证.

I'm using ScnSocialAuth ZF2 module to enable social media authentication in my project.

由于它使用ZfcUser作为其依赖项之一,因此默认情况下会创建/使用两个数据库表:

As it uses ZfcUser as one of its dependencies, two DB tables are created/used by default:

CREATE TABLE `user`
(
    `user_id`       INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `username`      VARCHAR(255) DEFAULT NULL UNIQUE,
    `email`         VARCHAR(255) DEFAULT NULL UNIQUE,
    `display_name`  VARCHAR(50) DEFAULT NULL,
    `password`      VARCHAR(128) NOT NULL,
    `state`         SMALLINT UNSIGNED
) ENGINE=InnoDB CHARSET="utf8";

CREATE TABLE IF NOT EXISTS `user_provider` (
  `user_id` INT NOT NULL,
  `provider_id` varchar(50) NOT NULL,
  `provider` varchar(255) NOT NULL,
  PRIMARY KEY (`user_id`,`provider_id`),
  UNIQUE KEY (`provider_id`,`provider`),
  FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
) ENGINE=InnoDB;

我想在两个表中添加更多字段.

I'd like to add more fields to both tables.

第一步是在数据库服务器中更改这些表.

The first step was to alter these tables in my DB server.

接下来,我覆盖了User和UserProvider实体/类(添加属性以及它们的getter和setter)并设置了配置文件(zfcuser.global.php和scn-social-auth.global.php),以便使它们使用这些新实体.到这里为止,一切运行顺利.

Next, I've override User and UserProvider entities/classes (adding properties and their getters and setters) and set up the config files (zfcuser.global.php and scn-social-auth.global.php) so they make use of these new entities. Up to here everything runs smoothly.

现在,我想获取用户的个人资料数据,并在身份验证时将其保存到这些表中(即使使用此数据来填充新的用户记录或更新现有的用户记录).

Now I want to get user's profile data and save them into these tables at auth time (even using this data to populate a new user record or to update an existing one).

查看ScnSocialAuth \ Authentication \ Adapter \ HybridAuth :: authenticate()方法,我已经意识到,在对用户进行身份验证时,只会设置以下字段:

Looking at the ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method I've realized that, when the user is authenticated, only the following fields are set:

user.email
user.password
user.display_name
user_provider.user_id
user_provider.provider_id
user_provider.provider

将我添加的字段(例如:性别,生日,地区等)留空.

leaving my added fields (for example: gender, birth_day, region, etc) blank.

通过检查\ ScnSocialAuth \ Authentication \ Adapter \ HybridAuth类的工作方式,我注意到的另一件事是实体以硬编码"方式混合.例如:

Another thing I've noticed, by checking how \ScnSocialAuth\Authentication\Adapter\HybridAuth class works, is that the entities are hydrated in a "hardcoded" way. For example:

protected function facebookToLocalUser($userProfile)
    {
        ...

        $localUser = $this->instantiateLocalUser();
        $localUser->setEmail($userProfile->emailVerified)
            ->setDisplayName($userProfile->displayName)
            ->setPassword(__FUNCTION__);
        $result = $this->insert($localUser, 'facebook', $userProfile);

        return $localUser;
    }

谁能解释扩展/覆盖authenticate()方法的最佳方法,以便我可以使用用户个人资料中的值设置这些新字段?当然,无需修改原始方法.

Can anyone explain which is the best way to extend/override the authenticate() method so I could set these new fields with the values from user's profile? Of course, without modifying the original method.

谢谢

推荐答案

此问题已在github中打开. https://github.com/SocalNick/ScnSocialAuth/issues/202

The solution was given in this issue, opened in github. https://github.com/SocalNick/ScnSocialAuth/issues/202

这篇关于如何(正确)扩展ScnSocialAuth \ Authentication \ Adapter \ HybridAuth :: authenticate()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆