Firstly I want to say what a great module this is. I have tried all of the FB connect modules and this is by far the easiest to set up and includes great functionality. Well done!

The one thing I cannot figure out is how to assign the role of the the new user created.

Any help would be appreciated.

Thanks

CommentFileSizeAuthor
#21 1335856-21.patch6.1 KBjrb
#10 1335856-10.patch5.99 KBjrb
#11 1335856-11.patch6.4 KBjrb
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Title: Assigning role of new connected users » Automatically assign specific roles of new connected users
Component: Documentation » Code
Category: support » feature

This module does not currently have this functionality, so I'm moving this to a feature request.

flosuter’s picture

You could probably us LoginToboggan to achieve this, if you can bear yet another module. I use both Facebook OAuth and LoginToboggan together without issues so far.

Louis Bob’s picture

Hello flosuter,

I am very interested in understanding how you use Facebook OAuth and LoginToboggan together?

I would like to give users the ability to either register "normally" with my website, or with their FB account, in both cases using LT functionalities :
- temporary non-authenticated role
- redirect path on registration and on confirmation

Is it possible ?

flosuter’s picture

I think assigning a role does work, but at the moment the redirection seems to be kinda broken, since facebook just redirects you to http://www.yourdomain.com/#_=_

We just changed everything around so the facebook login happens in a popup window, which we monitor through javascript and redirect when the window is closed.

This is the JS we use, we also changed the login form and submit actions.

// Open new window for facebook connect
    $('#facebook-connect').click(function() {
      win = window.open(this, "fbwindow","menubar=1,resizable=1,width=1050,height=750");
      win.onunload = function() {
        if(win.location == "http://yourdomain.com/#_=_") {
          onclose();
        }
      };
      return false;
    });
    // If user was logged in sucessfully show message and close link
    if (window.location.hash == '#_=_' && window.name == 'fbwindow' && Drupal.settings.naLogin.logged_in) {
      $('#page').html('<h1>You\'re logged in</h1><p>Please <a id="fbwindow-close" href="javascript:window.close()">close</a> this window.</p>');
    }
    // close window and reload parent page.
    $('#fbwindow-close').click(function() {
      if (opener.location.hash == '#id=confirm') {
        opener.Drupal.settings.naLogin.user = Drupal.settings.naLogin.user;
        opener.jQuery('#order-confirmation').html('<ul><li>' + Drupal.settings.naLogin.user.uid + '</li><li>' + Drupal.settings.naLogin.user.mail + '</li></ul>');
      }
      window.close();
    });
    
    var onclose = function() {
      Drupal.settings.naLogin.user = win.Drupal.settings.naLogin.user;
      $('#order-confirmation').html('<ul><li>' + Drupal.settings.naLogin.user.uid + '</li><li>' + Drupal.settings.naLogin.user.mail + '</li></ul>');
    };

This is still quite quick and dirty and has lots of room for improvement.

guy_schneerson’s picture

I use the hook_user_insert() to add a role as this gets called for all new users

    $new_user = module_invoke('user', 'load', $account->uid);
    $edit = array();
    $edit['roles'][n] = 'newrole';
    module_invoke('user', 'save', $new_user, $edit);

replace n by your role id and newrole with its name, you can add conditional code for this to happen only for the facebook connect if you need to as all the info you need gets pathed into the hook.

you can probably also use rules to do the same.

grasmash’s picture

The above example will apply the specified role to all users, regardless of how they register.

If you're interested in granting a new role to a user only when the register via fboauth, try this:

<?php
/**
 * Implements hook_user_presave().
 */
function yourmodule_user_presave(&$edit, $account, $category) {
  // 'n' should equal the role id.
  if ($account->is_new && isset($edit['fboauth']) && $edit['fboauth']) {
    $edit['roles'][n] = TRUE;
  }
}
?>
Prancz_Adam’s picture

How can we implement a manualy choosen way to select user's role?
Without that the user haven't got too much help and the administrator has to be work more...

imoreno’s picture

Where do i implement this code snipest?
with rules?

BR
Itzhak

imoreno’s picture

Also,
what do i need to change in this code (applied by rule) to work when a regular authenticated user is now connected via fbOAuth? i would like to set a rule to do so when any user is logged in.

jrb’s picture

FileSize
5.99 KB

Here is a patch to do this. It will allow you to select role(s) to be automatically assigned to users when they connect with Facebook. It also gives the option to remove the role(s) if they deauthorize.

This could be done with a separate module if there were a hook that got called in fboauth_action_connect() when the user already exists. If that's a better option, I can add that hook instead and create this as a separate module.


*** Don't use this patch-- see the one in the comment below ***

jrb’s picture

Status: Active » Needs review
FileSize
6.4 KB

Better patch attached. I forgot the calls to variable_del() in the .install file-- they're added now.

imoreno’s picture

Status: Needs review » Reviewed & tested by the community

Hi,
Thank you JRB.
I can confirm this patch is working as expected. i have a "connected via facebook" role which is automatically assign by FBAuth. very useful, especially for rules integration.

I think this patch can be committed.

BR
Itzhak

imoreno’s picture

Status: Reviewed & tested by the community » Needs work

Soory,
this patch is not working for me, it breaks FBAuth Module on line 117.

Itzhak

jrb’s picture

imoreno,

Can you give some more details? Are you seeing any error messages? When?

pinkonomy’s picture

Is this possible with this patch to assign roles based on the Path (with profile2 path reg module)?
E.g. if path is : user/simple-account/register ,assign the simple account role to user.
If path is user/admin-account/register ,assign to admin account role to user.

thanks

idealform01’s picture

subscribing, this feature would be really useful to me

lalosg’s picture

Issue summary: View changes

pinkonomy CreditAttribution: pinkonomy commented 2 years ago:

Is this possible with this patch to assign roles based on the Path (with profile2 path reg module)?
E.g. if path is : user/simple-account/register ,assign the simple account role to user.
If path is user/admin-account/register ,assign to admin account role to user.

thanks

____________________________

Anyone can solve this???

anil1591’s picture


This issue can resolve by going to some basic step with code

1. Where you are using this facebook connect button :

print fboauth_action_display('connect');

replace this with

print str_replace("your_fb_appid",trim("your_fb_appid&state=$role_id"), fboauth_action_display('connect'));

$role_id = "" //go to database and search 'role' table and get id of role and assign role for all user type
 

2. Then go to /sites/all/modules/fboauth/fboauth.module
and edit it and go to function

function fboauth_user_insert(&$edit, &$account, $category) {
if (isset($edit['fboauth_fbid'])) {
fboauth_save($account->uid, $edit['fboauth_fbid']);
}

And replace with this function :

function fboauth_user_insert(&$edit, &$account, $category) {
if (isset($edit['fboauth_fbid'])) {
fboauth_save($account->uid, $edit['fboauth_fbid']);

user_multiple_role_edit(array($account->uid), 'add_role', $_GET['state']);

}

and now you are ready to rock and role :)

tunedin’s picture

Has anyone been able to assign roles to FB Oauth with profile2 regpath? I have two paths, regular/register and advanced/register and want to also assign roles per Facebook login. I am using custom register template form and I am using version 7.x-2 of this module and not sure if adding patch above is good enough.

nadiTime’s picture

tunedin, have you found a solution?

jrb’s picture

FileSize
6.1 KB

Patch in #11 re-rolled for 7.x-2.0-rc3.