Skip to content

Commit

Permalink
Merge pull request #143 from bcc-code/feature/bcc-groups
Browse files Browse the repository at this point in the history
BCC Groups filtering
  • Loading branch information
rvanoord authored Feb 26, 2024
2 parents 229a0d5 + bc594a6 commit 4b797d6
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 85 deletions.
2 changes: 1 addition & 1 deletion plugins/bcc-login/bcc-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private function __construct(){
$this->_feed = new BCC_Login_Feed( $this->_settings, $this->_client );
$this->_updater = new BCC_Login_Updater( $this->plugin, $this->plugin_slug, $this->plugin_version, $this->plugin_name );

if (!empty($this->_settings->site_groups)) {
if (!empty($this->_settings->site_groups) || !empty($this->_settings->full_content_access_groups)) {
$this->_coreapi->ensure_subscription_to_person_updates();
}

Expand Down
20 changes: 9 additions & 11 deletions plugins/bcc-login/includes/class-bcc-coreapi-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ function __construct(BCC_Login_Settings $login_settings, BCC_Storage $storage)
}

function get_site_groups() {
if(isset($this->_site_groups)) {
if (isset($this->_site_groups)) {
return $this->_site_groups;
}
$group_uids = $this->_settings->site_groups;

$cache_key = 'coreapi_groups_' . implode($group_uids);

$cached_response = get_transient($cache_key);
if($cached_response) {
if ($cached_response !== false) {
return $cached_response;
}

Expand Down Expand Up @@ -65,7 +65,7 @@ function get_groups_for_user($user_uid) {
$cache_key = 'coreapi_user_groups_'.$user_uid;

$cached_response = get_transient($cache_key);
if($cached_response !== false) {
if ($cached_response !== false) {
return $cached_response;
}

Expand All @@ -77,13 +77,12 @@ function get_groups_for_user($user_uid) {
return $user_groups;
}

function fetch_groups_for_user($user_uid)
{
if(empty( $this->_settings->site_groups)) return array();
function fetch_groups_for_user($user_uid) {
if (empty($this->_settings->site_groups)) return array();

$token = $this->get_coreapi_token();

$request_url = $this->_settings->coreapi_base_url . "/v2/persons/". $user_uid . "/checkGroupMemberships";
$request_url = $this->_settings->coreapi_base_url . "/v2/persons/". $user_uid . "/checkGroupMemberships";
$request_body = array(
"groupUids" => $this->_settings->site_groups
);
Expand All @@ -95,9 +94,8 @@ function fetch_groups_for_user($user_uid)
)
));


if ( is_wp_error( $response ) ) {
wp_die( $response->get_error_message() );
if (is_wp_error($response)) {
wp_die($response->get_error_message());
}

if ($response['response']['code'] != 200) {
Expand All @@ -111,7 +109,7 @@ function fetch_groups_for_user($user_uid)
}

public function ensure_subscription_to_person_updates() {
if(str_contains(site_url(), "localhost")){
if (str_contains(site_url(), "localhost") || str_contains(site_url(), ".local") ) {
return;
}

Expand Down
31 changes: 28 additions & 3 deletions plugins/bcc-login/includes/class-bcc-login-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BCC_Login_Settings {
public $feed_key;
public $show_protected_menu_items;
public $site_groups = array();
public $full_content_access_groups = array();
public $coreapi_audience;
public $coreapi_base_url;
}
Expand Down Expand Up @@ -87,6 +88,11 @@ function __construct () {
$settings->site_groups = explode(",", $site_groups_option);
}

$full_content_access_groups_option = get_option('bcc_full_content_access_groups');
if ($full_content_access_groups_option) {
$settings->full_content_access_groups = explode(",", $full_content_access_groups_option);
}

// Backwards compatibility with old plugin configuration.
if ( ! isset( $settings->client_id ) ) {
$old_oidc_settings = (array) get_option( 'openid_connect_generic_settings', array () );
Expand Down Expand Up @@ -138,6 +144,7 @@ function register_settings() {
register_setting( $this->option_name, 'bcc_member_organization_name' );
register_setting( $this->option_name, 'bcc_feed_key' );
register_setting( $this->option_name, 'bcc_site_groups' );
register_setting( $this->option_name, 'bcc_full_content_access_groups' );
register_setting( $this->option_name, 'show_protected_menu_items' );

add_settings_section( 'general', '', null, $this->options_page );
Expand Down Expand Up @@ -215,7 +222,7 @@ function register_settings() {
)
);

if(!empty($this->_settings->site_groups)|| BCC_Coreapi_Client::check_groups_access(
if (!empty($this->_settings->site_groups) || BCC_Coreapi_Client::check_groups_access(
$this->_settings->token_endpoint,
$this->_settings->client_id,
$this->_settings->client_secret,
Expand All @@ -230,12 +237,30 @@ function register_settings() {
array(
'name' => 'bcc_site_groups',
'value' => join(",", $this->_settings->site_groups),
'description' => 'Provide group uids for groups you\'re going to use (comma delimtied)'
'description' => 'Provide group uids for groups you\'re going to use (comma delimited).'
)
);

}

if (!empty($this->_settings->full_content_access_groups) || BCC_Coreapi_Client::check_groups_access(
$this->_settings->token_endpoint,
$this->_settings->client_id,
$this->_settings->client_secret,
$this->_settings->coreapi_audience
)) {
add_settings_field(
'bcc_full_content_access_groups',
'Full Content Access Groups',
array( $this, 'render_text_field' ),
$this->options_page,
'general',
array(
'name' => 'bcc_full_content_access_groups',
'value' => join(",", $this->_settings->full_content_access_groups),
'description' => 'Groups that always can see published content regardless of group settings on content.'
)
);
}

add_settings_field(
'show_protected_menu_items',
Expand Down
Loading

0 comments on commit 4b797d6

Please sign in to comment.