Problem with role based restriction validation
-
Hi, I noticed that if restriction by role is enabled, and more than one role are set for the user, then even is the indicated user role is included the verification fails and the user cant start the chat.
So in my case I had an user with “subscriber” and “member”, manually deleting “subscriber” role does validate correctly.
Kind regards
-
This topic was modified 1 year, 2 months ago by
snippet24.
-
This topic was modified 1 year, 2 months ago by
-
Update: I was reading I think the standard way is not to have multiple roles but a single one, I will contact the plugin PropfilePress dev in the respective forum since that’s the plugin that assigned an extra role instead of switching to it…
Okey so it is required to have multiple roles for the plugin to work, is it possible to add support for chequing multiple roles when validating?
Hi there!
From the plugin settings you can configure only that way, unfortunately there is no other way available.
Only way is to use custom php filters to modify user role:
https://www.better-messages.com/docs/development/php-filters/better_messages_get_user_roles/Or set custom restrictions for conversation start:
https://www.better-messages.com/docs/development/php-actions/better_messages_before_new_thread/
Thanks!Hi, thnak you. Question how should I leave the settings when applying the filter? tried this with no effect though I’m still figuring it out
add_action( 'better_messages_before_new_thread', 'custom_restrict_new_thread', 10, 2 );
function custom_restrict_new_thread( &$args, &$errors ){
// User id who is trying to create conversation
$user_id = Better_Messages()->functions->get_current_user_id();
// Get array with recipients user ids, which user trying to start conversation with
$recipients = $args['recipients'];
/**
* Your logic to determine if this user can create thread
*/
if (!(ppress_has_active_subscription(($user_id), 2)) && !(ppress_has_active_subscription(($user_id), 3))){
$can_create_thread = false;
} else {
$can_create_thread = true;
}
if( ! $can_create_thread ){
/**
* Adding error which will be shown to user
*/
$errors['uniqueErrorSlug'] = 'You cant start new thread now.';
}
}and this other one crashed the site and im unable to save preferences even after removing the filter
add_filter('better_messages_get_user_roles', 'custom_user_roles', 20, 2);
function custom_user_roles($roles, $user_id) {
// Define allowed custom roles
$allowed_roles = ['Monthly plan', 'Anual plan'];
// Get the user's current roles
$user = get_userdata($user_id);
$user_roles = !empty($user) ? $user->roles : [];
// Filter the roles, keeping only the allowed ones
$filtered_roles = array_intersect($user_roles, $allowed_roles);
return $filtered_roles;}well this line
$user->roles : [];seems to have killed the role data :/Will fix them using :
if ( !function_exists( 'populate_roles' ) ) { require_once( ABSPATH . 'wp-admin/includes/schema.php' ); } populate_roles();I think I need some guide on how to use the role restriction filter…
Unfortunately I do not able to provide personal assistance with customizations, I would suggest to use ChatGPT, which is very helpful in that.
$allowed_roles = ['Monthly plan', 'Anual plan'];This is most likely wrong, user roles usually looks like that:
'user_role','annual_plan', etc.Thanks printed the slugs and they actually are
ppress_plan_2andppress_plan_3.Still though I cant get it to work , how can i tell is in effect the filter?
function custom_user_roles($roles, $user_id) {
$user = get_userdata($user_id);
// Debug: Log if user data is not found
if (!$user) {
error_log("Better Messages Role Debug: User ID {$user_id} not found.");
return $roles;
}
$all_roles = (array) $user->roles; // Get all assigned roles
// Debug: Log user roles
error_log("Better Messages Role Debug: User ID {$user_id} Roles: " . implode(', ', $all_roles));
// Define the priority order
if (in_array('ppress_plan_3', $all_roles, true)) {
error_log("Better Messages Role Debug: User ID {$user_id} assigned role 'ppress_plan_3'.");
return ['ppress_plan_3']; // Prefer ppress_plan_3 if available
}
if (in_array('ppress_plan_2', $all_roles, true)) {
error_log("Better Messages Role Debug: User ID {$user_id} assigned role 'ppress_plan_2'.");
return ['ppress_plan_2']; // Otherwise, use ppress_plan_2
}
error_log("Better Messages Role Debug: User ID {$user_id} has no relevant roles.");
return []; // If neither role is present, return an empty array (or modify as needed)
}
add_filter('better_messages_get_user_roles', 'custom_user_roles', 20, 2 );Unfortunately I do not able to provide personal assistance with customizations
I understand, is it an option to wait for support added for validation when there are multiple roles? which should improve compatibility with other plugins, in my case with ProfilePress.
Regards
-
This reply was modified 1 year, 2 months ago by
snippet24.
Hi there!
What exactly the issue and what are your role to role restrictions settings?Hi!
Please take a look here https://drive.google.com/file/d/1fraBJ_3jHsTpBWiAUmiH_6K10gYmWk4_/view?usp=sharing and for restriction settings here:
https://drive.google.com/file/d/1ydHzpMhm5kEtPrD6QLpPoiwLOqFTt6PM/view?usp=sharing
Basically if an user has more than one role set such as in the red marked rectangle at the right of the screenshot, then the validation always fails showing “You are not allowed to send messages” . On the other hand, if there’s only the one of the allowed roles set for an user as happens with the rectangle green, then it works fine. So my bet is that the checking/validation cant process an user that has more than one role set.
Hi there!
I see the thing now, will tune in next update.
Thanks!I just tried to reproduce, but I was not able to reproduce the issue, it correctly allows to send messages when users has multiple roles. Not sure what is the issue there.
I realized whats the cause I had “Restrict the creation of a new conversation” set to all roles except the ones from ProfilePress plugin.
Weird thing seems I was getting the message “You are not allowed to send messages” but that message is from the “role to role restriction” and not from the “restriction of creation of a new conversation” which reads as “You are not allowed to start new conversations”, hence the confusion about it. I will do further tests in relation to the messages being displayed during the day though.
Thank you.
Should had realized this sooner, I’m sorry about that
-
This reply was modified 1 year, 2 months ago by
The topic ‘Problem with role based restriction validation’ is closed to new replies.