如何(正确地)扩展 ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() 方法?
How to (correctly) extend ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() method?
我正在使用 ScnSocialAuth ZF2 模块在我的项目中启用社交媒体身份验证。
由于它使用 ZfcUser 作为其依赖项之一,默认情况下有两个数据库表 created/used:
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;
我想向两个表中添加更多字段。
第一步是更改我的数据库服务器中的这些表。
接下来,我覆盖了 User 和 UserProvider entities/classes(添加属性及其 getter 和 setter)并设置了配置文件(zfcuser.global.php 和 scn-social-auth.global.php ) 所以他们利用这些新实体。至此一切顺利。
现在我想获取用户的个人资料数据并在授权时将它们保存到这些表中(甚至使用此数据来填充新用户记录或更新现有用户记录)。
查看 ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() 方法我意识到,当用户通过身份验证时,只会设置以下字段:
user.email
user.password
user.display_name
user_provider.user_id
user_provider.provider_id
user_provider.provider
将我添加的字段(例如:性别、birth_day、地区等)留空。
我注意到的另一件事是,通过检查 \ScnSocialAuth\Authentication\Adapter\HybridAuth class 的工作原理,实体以 "hardcoded" 的方式水合。例如:
protected function facebookToLocalUser($userProfile)
{
...
$localUser = $this->instantiateLocalUser();
$localUser->setEmail($userProfile->emailVerified)
->setDisplayName($userProfile->displayName)
->setPassword(__FUNCTION__);
$result = $this->insert($localUser, 'facebook', $userProfile);
return $localUser;
}
任何人都可以解释哪种是 extend/override authenticate() 方法的最佳方式,以便我可以使用用户配置文件中的值设置这些新字段?当然,在不修改原方法的情况下。
谢谢
映射器用于获取混合适配器提供的信息并插入到实体中。因此,您所要做的就是确保您的新实体方法具有您尝试捕获的字段的设置器。需要注意的是,这些必须与适配器提供的相匹配。例如,github 适配器在 getUserProfile
中有以下内容
$this->user->profile->identifier = @ $data->id;
$this->user->profile->displayName = @ $data->name;
$this->user->profile->description = @ $data->bio;
$this->user->profile->photoURL = @ $data->avatar_url;
$this->user->profile->profileURL = @ $data->html_url;
$this->user->profile->email = @ $data->email;
$this->user->profile->webSiteURL = @ $data->blog;
$this->user->profile->region = @ $data->location;
这意味着,如果这是您要捕获的字段,您的实体应该有一个方法 setPhotoUrl
。但这是 github 实现提供的字段名称。并非所有提供商都以相同的名称提供相同的数据。您需要检查 getUserProfile
的每个混合适配器实现以找到您 want/need.
的字段
如果您发现不同的提供者在不同的属性名称下提供相同的信息,您可以将自定义水化器注入映射器,以实现自定义命名策略。
本期给出解决方案,开于github。 https://github.com/SocalNick/ScnSocialAuth/issues/202
我正在使用 ScnSocialAuth ZF2 模块在我的项目中启用社交媒体身份验证。
由于它使用 ZfcUser 作为其依赖项之一,默认情况下有两个数据库表 created/used:
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;
我想向两个表中添加更多字段。
第一步是更改我的数据库服务器中的这些表。
接下来,我覆盖了 User 和 UserProvider entities/classes(添加属性及其 getter 和 setter)并设置了配置文件(zfcuser.global.php 和 scn-social-auth.global.php ) 所以他们利用这些新实体。至此一切顺利。
现在我想获取用户的个人资料数据并在授权时将它们保存到这些表中(甚至使用此数据来填充新用户记录或更新现有用户记录)。
查看 ScnSocialAuth\Authentication\Adapter\HybridAuth::authenticate() 方法我意识到,当用户通过身份验证时,只会设置以下字段:
user.email
user.password
user.display_name
user_provider.user_id
user_provider.provider_id
user_provider.provider
将我添加的字段(例如:性别、birth_day、地区等)留空。
我注意到的另一件事是,通过检查 \ScnSocialAuth\Authentication\Adapter\HybridAuth class 的工作原理,实体以 "hardcoded" 的方式水合。例如:
protected function facebookToLocalUser($userProfile)
{
...
$localUser = $this->instantiateLocalUser();
$localUser->setEmail($userProfile->emailVerified)
->setDisplayName($userProfile->displayName)
->setPassword(__FUNCTION__);
$result = $this->insert($localUser, 'facebook', $userProfile);
return $localUser;
}
任何人都可以解释哪种是 extend/override authenticate() 方法的最佳方式,以便我可以使用用户配置文件中的值设置这些新字段?当然,在不修改原方法的情况下。
谢谢
映射器用于获取混合适配器提供的信息并插入到实体中。因此,您所要做的就是确保您的新实体方法具有您尝试捕获的字段的设置器。需要注意的是,这些必须与适配器提供的相匹配。例如,github 适配器在 getUserProfile
中有以下内容 $this->user->profile->identifier = @ $data->id;
$this->user->profile->displayName = @ $data->name;
$this->user->profile->description = @ $data->bio;
$this->user->profile->photoURL = @ $data->avatar_url;
$this->user->profile->profileURL = @ $data->html_url;
$this->user->profile->email = @ $data->email;
$this->user->profile->webSiteURL = @ $data->blog;
$this->user->profile->region = @ $data->location;
这意味着,如果这是您要捕获的字段,您的实体应该有一个方法 setPhotoUrl
。但这是 github 实现提供的字段名称。并非所有提供商都以相同的名称提供相同的数据。您需要检查 getUserProfile
的每个混合适配器实现以找到您 want/need.
如果您发现不同的提供者在不同的属性名称下提供相同的信息,您可以将自定义水化器注入映射器,以实现自定义命名策略。
本期给出解决方案,开于github。 https://github.com/SocalNick/ScnSocialAuth/issues/202