Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add youth member role #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

steff-mueller
Copy link

@steff-mueller steff-mueller commented Jan 15, 2023

Implemented a youth member role for content access which is assigned if the visitor is a member of your local church and has an age <=36.

Implemented a youth member role which is assigned if the visitor is a member
and has an age  <=36.
@cs1m0n
Copy link
Member

cs1m0n commented Jan 16, 2023

Hey Steffan,
and thank you so much for your contribution!

The code looks fine to me, but I wonder if that's the way we actually want to go ..
E.g. what happens if you want to target a youth member only from your local church? Will we then add another role for that?

I fear that without a clear goal for this, we'll continue adding more and more roles and end up having no control over the logic ..

Can you maybe describe the reason you need to identify youth members so we understand your use case?
Another solution to this is to have the logic inside the widget / plugin itself.

Only my 2 cents ..

@steff-mueller
Copy link
Author

steff-mueller commented Jan 16, 2023

Hi @cs1m0n, as I have implemented it right now, the role only targets youth members from your local church. I agree with you that a more flexible logic is the way to go. We needed a quick-n-dirty solution to limit access of our youth meeting livestreams to youth members of our church.

@steff-mueller
Copy link
Author

I found a way to accomplish our use case without changing the bcc-login plugin. I created a new separate plugin which applies the age restriction on the post/page. Maybe you want to add this snippet to the documentation?

wp-content/plugins/bcc-login-age-restriction/bcc-login-age-restriction.php:

<?php

/*
Plugin Name: BCC Login Age Restriction

Extension plugin for https://github.com/bcc-code/bcc-wp
*/

const dwjz_post_id = 1758; // the id of the post where to apply the age restriction
const dwjz_max_age = 36;

function dwjz_on_template_redirect() {
	// check post id
	if (get_the_ID() === dwjz_post_id && !dwjz_is_youth()) {
		return dwjz_not_allowed_to_view_page();
	}
}

add_action( 'template_redirect', 'dwjz_on_template_redirect', 1 );

function dwjz_filter_menu_items( $items ) {
	foreach ( $items as $key => $item ) {
		if ($item->object_id == dwjz_post_id && !dwjz_is_youth()) {
			unset( $items[ $key ] );
		}
	}
	
	return $items;
}

add_filter( 'wp_get_nav_menu_items', 'dwjz_filter_menu_items', 21 );

/*
Returns `true` if the current user's age is <= `dwjz_max_age`,
otherwise the function returns `false`.
*/
function dwjz_is_youth() {
	// Check if user is logged in to Wordpress
	if (!is_user_logged_in()) {
		return false;
	}

	// Check if oidc token is available
	if (!isset($_COOKIE['oidc_token_id'])) {
		return false;
	}

	// Check if oidc token is valid (still stored in transient)
	$token_id = $_COOKIE['oidc_token_id'];
	if ( ! empty( $token_id ) ) {
		$token = get_transient( 'oidc_id_token_' . $token_id );
	}
	if ( empty( $token ) ) {
		return false;
	}

	// Get token claims
	$claims = BCC_Login_Token_Utility::get_token_claims( $token );

	$birthdate = date_create($claims['birthdate']);
	$current_date = date_create();
	$diff = date_diff($birthdate, $current_date);
	$age = $diff->y;
	return $age <= dwjz_max_age;
}

function dwjz_not_allowed_to_view_page(){
	wp_die(
		sprintf(
			'%s<br><a href="%s">%s</a>',
			__( 'Sorry, you are not allowed to view this page.', 'bcc-login' ),
			site_url(),
			__( 'Go to the front page', 'bcc-login' )
		),
		__( 'Unauthorized' ),
		array(
			'response' => 401,
		)
	);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants