Created
June 18, 2015 01:40
-
-
Save medy36/c065bccfcaf56b76934d to your computer and use it in GitHub Desktop.
fos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Plt\Bundle\UserBundle\Security\Core\User; | |
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; | |
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass; | |
use Plt\Bundle\UserBundle\Entity\User; | |
use Plt\Bundle\UtilityBundle\Helper\ImageProcessor; | |
use Symfony\Component\Filesystem\Filesystem; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
class FOSUBUserProvider extends BaseClass | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
public function connect(UserInterface $user, UserResponseInterface $response) | |
{ | |
$property = $this->getProperty($response); | |
$username = $response->getUsername(); | |
//on connect - get the access token and the user ID | |
$service = $response->getResourceOwner()->getName(); | |
$setter = 'set' . ucfirst($service); | |
$setter_id = $setter . 'Id'; | |
$setter_token = $setter . 'AccessToken'; | |
//we "disconnect" previously connected users | |
if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) { | |
$previousUser->$setter_id(null); | |
$previousUser->$setter_token(null); | |
$this->userManager->updateUser($previousUser); | |
} | |
//we connect current user | |
$user->$setter_id($username); | |
$user->$setter_token($response->getAccessToken()); | |
$this->userManager->updateUser($user); | |
} | |
public function facebookProvider($response) | |
{ | |
$email = $response->getEmail(); | |
$firstname = $response->getFirstName(); | |
$lastname = $response->getLastName(); | |
// $gender = $response->getGender(); | |
$link = $response->getLink(); | |
$picture = $response->getProfilePicture(); | |
$username = $firstname . " " . $lastname; | |
if ($picture) { | |
$pic = file_get_contents($picture); | |
$filename = uniqid(); | |
$f1 = substr($filename, 0, 2); | |
$f2 = substr($filename, 2, 2); | |
$fs = new Filesystem(); | |
// $pathname = $filename . '.jpg'; | |
$pathname = __DIR__ . '/../../../../../../../web/medias/' . $filename; | |
file_put_contents($pathname, $pic); | |
// file_put_contents(__DIR__ . '/../../../../../../../web/medias/'); | |
$handler = ImageProcessor::defaultHandler(new Filesystem()); | |
$imageProcessor = new ImageProcessor($pathname, $handler, true); | |
$imageProcessor->process(ImageProcessor::SIZE_SET_ALL); | |
// exit($test); | |
// $fs->mkdir(__DIR__ . '/../../../../../../../web/medias/' . $f1 . '/' . $f2 . '/'); | |
// file_put_contents(__DIR__ . '/../../../../../../../web/medias/' . $f1 . '/' . $f2 . '/' . $filenamew . '.jpg', $pic); | |
} | |
//when the user is registrating | |
$id = $response->getId(); | |
$accesstoken = $response->getAccessToken(); | |
$service = $response->getResourceOwner()->getName(); | |
$setter = 'set' . ucfirst($service); | |
$setter_id = $setter . 'Id'; | |
$setterLink = $setter . 'Link'; | |
$setter_token = $setter . 'AccessToken'; | |
// create new user here | |
$user = $this->userManager->createUser(); | |
$user->$setter_id($id); | |
$user->$setter_token($accesstoken); | |
$user->$setterLink($link); | |
//I have set all requested data with the user's username | |
//modify here with relevant data | |
$user->setUsername(str_replace(' ', '.', strtolower($username)) . substr($id, -3, 3)); | |
$user->setEmail($email ?: "to-set-" . uniqid() . "@mail.com"); | |
$user->setLastname($lastname); | |
$user->setFirstname($firstname); | |
$user->setAvatar($filename . '.jpg'); | |
// $user->setGender($gender); | |
$user->setRoles(array('ROLE_USER')); | |
$user->setPassword(uniqid()); | |
$user->setEnabled(true); | |
// $this->userManager->updateUser($user); | |
return $user; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function loadUserByOAuthUserResponse(UserResponseInterface $response) | |
{ | |
$data = $response->getResponse(); | |
//TODO add scope for full user info | |
$userProvider = $response->getResourceOwner()->getName(); | |
if ($userProvider == "facebook") { | |
$firstname = $response->getFirstName(); | |
$lastname = $response->getLastName(); | |
$email = $response->getEmail(); | |
$username = $firstname . " " . $lastname; | |
$user = $this->userManager->findUserBy(array('email' => $email)); | |
//when the user is registring | |
if (null === $user) { | |
$user = $this->facebookProvider($response); | |
} | |
//if user exists - go with the HWIOAuth way | |
// $user = parent::loadUserByOAuthUserResponse($response); | |
$serviceName = $response->getResourceOwner()->getName(); | |
$setter = 'set' . ucfirst($serviceName) . 'AccessToken'; | |
//update access token | |
$user->$setter($response->getAccessToken()); | |
return $user; | |
} else { | |
exit('provider not exist'); | |
} | |
$serviceName = $response->getResourceOwner()->getName(); | |
$setter = 'set' . ucfirst($serviceName) . 'AccessToken'; | |
//update access token | |
$user->$setter($response->getAccessToken()); | |
return $user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment