Support Forum

mapping wp roles to spf user groups

MP Mr Papa
Mr Papa
Member

currently, we have two default mappings for auto user group assignment – members and guests.

but its been asked for multiple times and is a good idea to break out the members assignment to user groups by WP role.  Several have asked for it in the form of role manager plugin support, but it doesnt really involve it and wont be required.  The real issue is to grant membership into a user group based upon WP role assignment.

I propose no changes to the default guest usergroup assignment.  That works nicely and have no improvements in mind.

Currently, the assignment of these two default usergroups is in the forum – options – members tab.  I propose it stay there.

In addition to the default usergroup for guests and members (that will remain – more later on why), I proposed adding drop downs for all WP Roles that exist.  In each dropdown, you can select which user group you want that member to be in for that role.  Then when a user is created (hook exists), or the role is later updated (hook exists), membership can be extended to that user group.  As long as other plugins (role manager for example does) follow the WP standard way here, movement between roles can be handled.

the default member user group would remain as is to handle the case where no usergroup is selected for a given WP Role or the case where a new role is created (say with role manager) but again, no usergroup is selected.  And it maintains backwards compatibility.

I still need to think about a good way to store the new default memberships.  Its currently done in the options table but that may not be the best place now – perhaps a new table.  also need to consider what to do about removal of WP roles which is possible but there isnt a hook in place to know that.  could possible do a cleanup check for non existent role to usergroup mappings on some admin page.  this might be another reason for separate table.  thoughts?

Also, need to think about or offer options for current memberships when a role is changed.  do we remove other memberships?  or just add the new membership?  or some way for users to select the behaviour. thoughts?

I have not looked at wpmu and what effect this might have.  If wpmu goes against wp standards again, it could be hard or problematic to support both.  My plan would be to get it working for wp and then see (or get radio to help) how it does with wpmu.

17 Answers

New Answer

MP Mr Papa
Mr Papa
Member

the UI for usergroup assignment based on WP Role would look something like the image below…  a bit more write up help would be needed…  btw, the dummy role was added with role manager…

MP Mr Papa
Mr Papa
Member

After thinking about the storage a bit more this afternoon, I am leaning towards using sfmeta table to store the default usergroup assignments.  A new table, while cheap, would be overkill especially if users dont utiilize this feature.

I am thinking a new sfmeta row for each default role assignment, plus the two (guests and members) default user types.  this would be straight forward. 

the meta type would be something like “auto usergroup”.  the meta key would be set to the WP role.  the meta value would then be the usergroup id to be used for memberships.  The meta value could potentially become a serialized array if more info (such as other membership behaviour) is required.

thoughts?

YS Yellow Swordfish
Yellow Swordfish
Member

My suggestion after reading the first two posts was going to be sfmeta – it's the sort of thing it's for really so if it is a fit then I would go for it. All looks and sounds good to me.

MP Mr Papa
Mr Papa
Member

okay, will press on…

for now, will simply add the new user group membership if the role is changed, but still need to come up with strategy for existing user group memberships.

MP Mr Papa
Mr Papa
Member

oh, this was just going to smooth to be true…  but of course, wp in its infinite wisdom has to throw a monkey wrench in the mix…

this is working fine for user registrations via wp-register.php and for profile updates done via the wp admin user profile.

unfortunately, it doesn't work for users created by wp admin in the add user admin panel.  for some very odd reason, when a user is created this way, the user_register hook is called twice.  The first time, it has the new role of the user as selected from a dropdown on the add user form.  however, the hook is fired a second time this time with a role of the default role for new wp users.  the wp routines for adding users have special code to ignore this second update with a default role, but it sure does mess up any function waiting on that hook firing…

MP Mr Papa
Mr Papa
Member

well, maybe not.. this is a mess for wp admin created users.  may not work right since they appear to update the role after calling the hook.

YS Yellow Swordfish
Yellow Swordfish
Member

We have some users who are goats? Smile

Actually judgoing by some of the questions we get this wouldn't suprise me too much!

MP Mr Papa
Mr Papa
Member

well luckily they add their own temp routine on the same hook to fix up their initially incorrect role…  so I just changed the priority of my hook action to let their hook actions run before mine.  this should also let other plugins that might muck with the new user data run first too.

but you got to love it when their comment on what they are doing says “The hackiest hack that ever did hack”

MP Mr Papa
Mr Papa
Member

okay, I got the basic stuff here working and have committed my changes for others to play around with.

Its working fine with role manager too… any time a role of a user is updated via the wp admin, new user group assignment if defined is made.  If the user is already in that usergroup, nothing is done.

Whats left?

Cleaning up the UI for setting the role/usergroup mappings.

writing the help

still need to decide what to do about existing memberships.  Currently, if the role is changed, the new usergroup membership is given (if applicable).  any existing memberships remain. kind of torn as to whether that is optimal or should offer perhpas a check box next to the pull down in the options setting which says remove all other memberships when the assignment is made.  the way it currently is going to be fine for most cases.  however, if a user role is “downgraded” at a later time, folks will want the other membershi revoked and the new one to be in place from the new role.  The checkbox idea complicates stuff a bit, but would handle this situation.  other UI ideas for this?

missing anything else?

YS Yellow Swordfish
Yellow Swordfish
Member

Could this be a global option along the lines of 'on my forum users can only belong to one user group'?

MP Mr Papa
Mr Papa
Member

interesting option worth considering. 

what would be the downside to this?  might be limiting for multiple private forums where users can do some things in one forum but more/less in another forum based on some sort of subscription level.

certainly a simple implementation.

YS Yellow Swordfish
Yellow Swordfish
Member

Well it would just mean that for most users who have a simple, straightforward forum the option would be checked.

For those that want to have a more sophisticated arrangement then iot would not be and they would have to make manual adjustments as required. But – that is what they have to do now in any case so it might not be that bad…

MP Mr Papa
Mr Papa
Member

seems logical…  so a global option for limiting membership to single user group?  add the check box for that at the bottom of the section where I have the wp role to usergroup mapping?  would think the default is not checked?