Protected content based on user role

Is there a way to do a multi-tier content protection based on the user role? The site I'm developing uses a SSO and transfers user access level when the user signs on. I already have the single sign on updating a user role, but now I need to limit what they see based on that role.

    PhilYourBrain

    I have Protected Content installed and now I'm working the integration. I receive the user access level and I can map them to roles. In this case I want to circumvent the assigning a role and give them a membership level directly. Below is a code snippet from the SSO that I believe is most relevant. It's where one assigns the roles.

    I setup 4 membership levels in Protected Content (guest, customer, partner, employee.) When someone logs in, what command/piece of code do I use to tell it to assign/edit a particular membership level?

    if (!empty($roleMapping) && isset($attrs[$roleMapping])){
    $adminsRole = explode(',', get_option('onelogin_saml_role_mapping_administrator'));
    $editorsRole = explode(',', get_option('onelogin_saml_role_mapping_editor'));
    $authorsRole = explode(',', get_option('onelogin_saml_role_mapping_author'));
    $contributorsRole = explode(',', get_option('onelogin_saml_role_mapping_contributor'));
    $subscribersRole = explode(',', get_option('onelogin_saml_role_mapping_subscriber'));

    /* In order to use custom roles, you only need to uncomment those lines and replace the values
    * First we assign possible OneLogin roles that we want to map with Wordpress Roles
    * Then we asigned to the $userdata['role'] the name of the Wordpress role
    */

    //$customRole1 = array('value1', 'value2'); // value1 and value2 are roles of OneLogin platform that will be mapped to customRole1
    //$customRole2 = array('value3'); // value3 is a role of OneLogin platformthat will be mapped to customRole2

    $foundCustomizedRole = false;

    /*
    foreach ($attrs[$roleMapping] as $samlRole) {
    if (in_array($samlRole, $customRole1)) {
    $userdata['role'] = 'customrole1'; // Name of the role -> customrole1
    $foundCustomized = true;
    break;
    } else if (in_array($samlRole, $customRole2)) {
    $userdata['role'] = 'customrole2'; // Name of the role -> customrole2
    $foundCustomized = true;
    break;
    }
    }
    */

    PhilYourBrain

    I got it working but it's kind of clunky working with the Single Sign On code. The SSO is extremely sensitive to added code. So sometimes during heavy load, the SSO fails to a white screen.
    Is there a way that you could see whereby we check the user role and assign the membership level based on that role not during the sign on? i.e. if they are a subscriber, they get guest access... Contributor, they get customer access....

    PhilYourBrain

    I'm thinking of putting a php script just on the home page. Something like this:

    $handle = new MS_Controller_Member ;
    $userid = get_current_user_id();
    $user_info =get_userdata( $userid );
    
    switch ($user_info->roles) {
    					case "administrator":
    					case "editor":
    						$handle -> assign_memberships( $user_id, intval('2182')) ;
    						break;
    					case  "author":
    						$userdata['role'] = 'author';
    						$handle -> assign_memberships( $user_id, intval('2181')) ;
    						break;
    					case "contributor":
    						$handle -> assign_memberships( $user_id, intval('2182')) ;
    						break;
    					case "subscriber":
    					default:
    						$userdata['role'] = 'subscriber';
    						$handle -> assign_memberships( $user_id, intval('2156')) ;
    						break;
    }

    Do you think that'll work?

    Jude

    Hi there @poomah

    Hope you're doing great !!

    No don't put that in the front page. It'll overload and crash the site. Here is some wrapper code, Just copy/paste the code into the functions.php file of your child theme or a site specific plugin if you use one.

    function set_membership_roles_manually( $user_id ) {
    
    	$handle = new MS_Controller_Member ;
    	$user_info = get_userdata( $userid );
    
    		switch ($user_info->roles) {
    					case "administrator":
    					case "editor":
    						$handle -> assign_memberships( $user_id, intval('2182')) ;
    						break;
    					case  "author":
    						$userdata['role'] = 'author';
    						$handle -> assign_memberships( $user_id, intval('2181')) ;
    						break;
    					case "contributor":
    						$handle -> assign_memberships( $user_id, intval('2182')) ;
    						break;
    					case "subscriber":
    					default:
    						$userdata['role'] = 'subscriber';
    						$handle -> assign_memberships( $user_id, intval('2156')) ;
    						break;
    		}
    
    }
    
    add_action( 'user_register', 'set_membership_roles_manually', 50, 1 );

    Hope that helps

    Cheers
    Jude