Home › Forums › Legacy Support › Support queries › Setup issues › how to Limit user access to group/forum
- This topic has 7 replies, 3 voices, and was last updated 4 years ago by Anshuman Sahu.
Viewing 8 posts - 1 through 8 (of 8 total)
-
AuthorPosts
-
March 24, 2020 at 5:19 pm #247126albertParticipantHi, can we also limit the user to the course group and forums after his/her course expiration? Please send custom code.March 25, 2020 at 11:37 am #247264VeronicaModeratorhi, this is not available right now. but thanks for idea.March 26, 2020 at 7:35 am #247373albertParticipantHi previously I asked this in Evanto market comments section And WPLMS team said yes It can be done by some custom code so go and create a topic forum ... For this I paid and renewed my WPLMS Theme support !March 26, 2020 at 2:17 pm #247487VeronicaModeratorhi, ok then we will write code for this. we will provide it to you tomorrow after testing. please ping us tomorrow.March 27, 2020 at 6:17 am #247572albertParticipantHi thanks alotMarch 27, 2020 at 1:53 pm #247661VeronicaModeratorhi, add this code: add_action('bbp_template_redirect',array($this, 'wplms_bppenforce_permissions'), 1); customizer plugin>>customizer_class.php in construct refer: https://prnt.sc/rnu94g after that in class: add this code: function wplms_bppcan_user_view_post($post_id){ global $wpdb; if(current_user_can('manage_options')) return true; $user_id = get_current_user_id(); $parents = get_post_ancestors( $post_id ); $id = ($parents) ? $parents[count($parents)-1]: $post_id; $course_id = $wpdb->get_var($wpdb->prepare("SELECT m.post_id as post_id FROM {$wpdb->posts} as p LEFT JOIN {$wpdb->postmeta} as m ON p.ID = m.post_id WHERE p.post_type = 'course' AND p.post_status = 'publish' and m.meta_key = %s AND m.meta_value = %d",'vibe_forum',$id)); $this->temp = $course_id; $expiry = get_user_meta($user_id,$course_id,true); if(empty($expiry) || (!empty($expiry) && $expiry <= time())){ return false; } if(empty($course_id) || get_post_field('post_author',$course_id) == $user_id) return true; $post_type = get_post_type($course_id); if($post_type == 'course'){ }else if($post_type == 'unit'){ if(function_exists('bp_course_get_unit_course_id')){ $course_id = bp_course_get_unit_course_id($id); } }else{ $course_id = 0; } if(empty($course_id)) return true; $var = wplms_user_course_check($user_id,$course_id); return empty($var)?false:true; } function wplms_bppenforce_permissions(){ global $post; // Bail if not viewing a single item or if user has caps if (!is_singular() || bbp_is_user_keymaster() || current_user_can('read_hidden_forums') || bbp_is_forum_archive() || bp_is_my_profile()){ return; } if (!$this->wplms_bppcan_user_view_post($post->ID)) { if (!is_user_logged_in()) { if(is_numeric($this->temp)){ $link =get_permalink($this->temp).'?error=not-accessible'; wp_redirect($link,'302'); exit(); }else{ auth_redirect();exit; } }else { if(is_numeric($this->temp)){ wp_redirect(get_permalink($this->temp).'?error=not-accessible','302'); exit; }else{ bbp_set_404();exit; } } } }
after that in wplms-customizer.php add this code add_action('bp_before_group_header',function(){ $group_id = bp_get_group_id(); global $wpdb;$user_id = get_current_user_id(); $results = $wpdb->get_results($wpdb->prepare("SELECT post_id from {$wpdb->postmeta} WHERE meta_value = %s AND meta_key= %s ",$group_id,'vibe_group')); foreach ($results as $key => $value) { $expiry = get_user_meta( $user_id,$value->post_id,true); if(empty($expiry) || (!empty($expiry) && $expiry < time())){ echo '<meta http-equiv="refresh" content="0;URL=\''.get_permalink($value->post_id).'\'" />'; } } }); add_filter('bp_course_restrcited_forums_query',function($x){ $user_id = get_current_user_id(); return $wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = '%s' AND post_status = '%s' AND ID NOT IN (SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %d AND meta_value <= %d)",'course','publish',$user_id,$time); }); add_action('bbp_template_redirect', 'wplms_bppenforce_permissions', 1); function wplms_bppcan_user_view_post($post_id){ global $wpdb; if(current_user_can('manage_options')) return true; $user_id = get_current_user_id(); $parents = get_post_ancestors( $post_id ); $id = ($parents) ? $parents[count($parents)-1]: $post_id; $course_id = $wpdb->get_var($wpdb->prepare("SELECT m.post_id as post_id FROM {$wpdb->posts} as p LEFT JOIN {$wpdb->postmeta} as m ON p.ID = m.post_id WHERE p.post_type = 'course' AND p.post_status = 'publish' and m.meta_key = %s AND m.meta_value = %d",'vibe_forum',$id)); $expiry = get_user_meta($user_id,$course_id,true); if(empty($expiry) || (!empty($expiry) && $expiry <= time())) return false; if(empty($course_id) || get_post_field('post_author',$course_id) == $user_id) return true; $post_type = get_post_type($course_id); if($post_type == 'course'){ }else if($post_type == 'unit'){ if(function_exists('bp_course_get_unit_course_id')){ $course_id = bp_course_get_unit_course_id($id); } }else{ $course_id = 0; } if(empty($course_id)) return true; //$this->temp = $course_id; $var = wplms_user_course_check($user_id,$course_id); return empty($var)?false:true; } function wplms_bppenforce_permissions(){ global $post; // Bail if not viewing a single item or if user has caps if (!is_singular() || bbp_is_user_keymaster() || current_user_can('read_hidden_forums') || bbp_is_forum_archive() || bp_is_my_profile()) return; if (!wplms_bppcan_user_view_post($post->ID)) { if (!is_user_logged_in()) { if(is_numeric($course_id)){ $link =get_permalink($course_id).'?error=not-accessible'; wp_redirect($link,'302'); exit(); }else{ auth_redirect(); } }else { if(is_numeric($course_id)){ wp_redirect(get_permalink($course_id).'?error=not-accessible','302'); exit; }else{ bbp_set_404(); } } } } add_action('init',function(){ $init = WPLMS_tips::init(); remove_action('bbp_template_redirect', array($init,'bpp_enforce_permissions'), 1); });
make sure you have set the 404 page in wplms>>miscellaneous>>404 page. save the settings. please check this and revert. we have tested the code it works peroperly. best regards!April 13, 2020 at 6:18 am #250856albertParticipantI tried this code: But It redirects none-logged in users from home page to login page Can we make it in a way if user course subscription expired accordingly to remove the user automatically from the joined groups and forums either?April 14, 2020 at 11:02 am #251240Anshuman SahuKeymasteryes it will as they are not logged in , the code will redirect the users to course of 404 page . Im sorry we cannot remove the user on expiration as there is no such expiration hook available in wplms . The expiration can happen in background when user is not live on website , therefore there is no trigger point for this on which we can remove the user . So therefore this actually practically impossible as of now . -
AuthorPosts
Viewing 8 posts - 1 through 8 (of 8 total)
- The topic ‘how to Limit user access to group/forum’ is closed to new replies.