Domanda

Ho installato FosUserBundle e FosFaceBookBundle usando questo metodo: Come fare un URL separato per la firma tramite Facebook utilizzando FosFaceBookBundle in Symfony2. In realtà non ho davvero capito l'intera cosa del processo di sicurezza, ma ora funziona bene.

Quando qualcuno sta usando Facebook iscrizione, vorrei la possibilità di scegliere un nome utente prima di essere registrato (invece dell'ID Facebook come nome utente) ... quello che faccio è che invio un parametro post sul percorso di accesso di Facebook ma posso " T Trova il controller in cui viene elaborata la registrazione.

Quale sarebbe la migliore pratica? Dove devo recuperare il nome utente (il parametro post) e impostarlo?

Ecco la mia configurazione in Security.yml:

firewalls:

public:
  pattern: ^/

  fos_facebook:
    app_url: "http://www.appName.com"
    server_url: "http://local.appName.com/app_dev.php/"
    login_path: /login
    check_path: /login_check/facebook
    provider: appName.facebook.provider

  form_login:
    login_path: /login
    check_path: /login_check/form
    provider: fos_userbundle
    csrf_provider: form.csrf_provider  

Ed ecco il routing che uso per iscriverti con Facebook:

_security_check_facebook:
    pattern: /login_check/facebook
È stato utile?

Soluzione

There is no controller processing authentication, ever authentication method add a listener that will trigger on check_path, and do the trick.

Authentication process shouldn't be responsible to add custom user data. If you followed FOSFacebookBundle documentation you should have a custom user provider that store the user on database, this may confuse a bit but this is not a registration step, is an authentication step. Adding a username is much similar to a profile editing.

To help you i should see your implementation (how do you send post parameter ? ) but you could try something like this:

  1. send post parameter to a custom controller (implemented by you)
  2. temporary store username (maybe user session data ?)
  3. trigger authentication process (this depend on your implementation but essentially we are talking about authenticate on facebook and then post to login_check path)
  4. add username at the authenticated user

This could work, but I never did something like this.

Altri suggerimenti

Old question, but I was working on a similar problem. I wanted to add a role to a user when they were registered on the first time they connected with Facebook.

If you follower these steps from the FOSFacebookBundle documentation, then you can tap into the Facebook connected user's "registration", take a look at the FacebookProvider class.

In loadUserByUsername method there is this part:

if (!empty($fbdata)) {
    if (empty($user)) {
        $user = $this->userManager->createUser();
        $user->setEnabled(true);
        $user->setPassword('');
    }

    $user->setFBData($fbdata);
    ...

After the setPassword within the if is where I added the role for my user.

For your original problem of choosing the username... notice that call to setFBData?

In the User entity the method looks like this:

public function setFBData($fbdata)
{
    if (isset($fbdata['id'])) {
        $this->setFacebookId($fbdata['id']);
        $this->addRole('ROLE_FACEBOOK');
    }
    if (isset($fbdata['first_name'])) {
        $this->setFirstname($fbdata['first_name']);
    }
    if (isset($fbdata['last_name'])) {
        $this->setLastname($fbdata['last_name']);
    }
    if (isset($fbdata['email'])) {
        $this->setEmail($fbdata['email']);
    }
}

and setFacebookId() looks like this:

public function setFacebookId($facebookId)
{
    $this->facebookId = $facebookId;
    $this->setUsername($facebookId);
}

So that's where the username comes from. I guess that is a flow where you could plug your chosen username into and set it instead of the $facebookId.

I did another way for this. I used GraphAPI.

public function setFacebookId($facebookId)
{
    $this->facebookId = $facebookId;
    $username = json_decode(file_get_contents('http://graph.facebook.com/'.$this->facebookId))->username;
    $this->setUsername($username);
}

It takes the Facebook username, an unique one.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top