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

Each user oauth account connect #88

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions includes/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ protected function load_dependencies() {
require_once ZVC_PLUGIN_INCLUDES_PATH . '/admin/class-zvc-admin-addons.php';
require_once ZVC_PLUGIN_INCLUDES_PATH . '/admin/class-zvc-admin-sync.php';
require_once ZVC_PLUGIN_INCLUDES_PATH . '/admin/class-zvc-admin-setup-wizard.php';
require_once ZVC_PLUGIN_INCLUDES_PATH . '/admin/class-zvc-admin-connect-user-account.php';

//Timezone
Timezone::get_instance();
Expand Down
16 changes: 16 additions & 0 deletions includes/Data/Datastore.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,20 @@ public static function get_meetings( $args = false, $wp_query = true ) {

return $result;
}

/**
* Retrives zoom setting options.
*
* @param $key
*
* @return false|mixed
*/
public static function get_vczapi_zoom_settings( $key = '' ) {
$setting = get_option( '_vczapi_zoom_settings' );
if ( ! empty( $setting ) && ! empty( $key ) ) {
return ! empty( $setting[ $key ] ) ? $setting[ $key ] : false;
}

return ! empty( $setting ) ? $setting : false;
}
}
71 changes: 71 additions & 0 deletions includes/admin/class-zvc-admin-connect-user-account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

class Zoom_Connect_User_Account {
public static ?Zoom_Connect_User_Account $instance = null;

public static function get_instance(): ?Zoom_Connect_User_Account {
return is_null( self::$instance ) ? self::$instance = new self() : self::$instance;
}

public function __construct() {
}

public function render() {
$account_id = get_user_meta( get_current_user_id(), 'zoom_user_account_id', true );
$client_id = get_user_meta( get_current_user_id(), 'zoom_user_client_id', true );
$client_secret = get_user_meta( get_current_user_id(), 'zoom_user_client_secret', true );

//saving the host id to the hostid meta
$list_user = json_decode(zoom_conference()->listUsers());
$host_id = $list_user->users[0]->id;
update_user_meta(get_current_user_id(), 'user_zoom_hostid', $host_id);

// Output the form
?>
<div style="padding: 20px; background: #cccccc">
<h2>Users Server to Server Oauth Credentials</h2>
<form method="post" action="" style="display: flex; flex-direction:column; align-items: flex-start; gap: 20px;">
<div style=" display: flex;align-items: center;">
<label for="client_id" style="width: 150px">Oauth Account ID:</label>
<input type="text" name="account_id" value="<?php echo esc_attr( $account_id ); ?>" required/>
</div>
<div style=" display: flex;align-items: center;">
<label for="client_id" style="width: 150px">Oauth Client ID:</label>
<input type="text" name="client_id" value="<?php echo esc_attr( $client_id ); ?>" required/>
</div>
<div style=" display: flex;align-items: center;">
<label for="client_secret" style="width: 150px">Oauth Client Secret:</label>
<input type="text" name="client_secret" value="<?php echo esc_attr( $client_secret ); ?>" required/>
</div>
<input type="submit" name="save_user_credentials" value="Save Credentials"/>
</form>
</div>
<?php
$access_token = \Codemanas\VczApi\Api\S2SOAuth::get_instance()->generateAndSaveAccessToken( $account_id, $client_id, $client_secret, true );
if ( is_wp_error( $access_token ) ) {
// Handle the error appropriately
$error_message = $access_token->get_error_message();
// Display the error message to the user
echo '<p>Error: ' . esc_html( $error_message ) . '</p>';
} else {
// Access token generated successfully
echo '<p>Access token generated and saved successfully!</p>';
}
}
public function save_api_credentials() {
if ( isset( $_POST['save_user_credentials'] ) ) {
$account_id = sanitize_text_field( $_POST['account_id'] );
$client_id = sanitize_text_field( $_POST['client_id'] );
$client_secret = sanitize_text_field( $_POST['client_secret'] );

// Save the API credentials for the current user
update_user_meta( get_current_user_id(), 'zoom_user_account_id', $account_id );
update_user_meta( get_current_user_id(), 'zoom_user_client_id', $client_id );
update_user_meta( get_current_user_id(), 'zoom_user_client_secret', $client_secret );
}
}
}




14 changes: 11 additions & 3 deletions includes/admin/class-zvc-admin-post-type.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Codemanas\VczApi\Data\Datastore;

/**
* Meeting Post Type Controller
*
Expand Down Expand Up @@ -356,8 +358,7 @@ public function register_post_type() {
'not_found_in_trash' => __( 'No zoom events found in Trash.', 'video-conferencing-with-zoom-api' ),
) );

$settings = get_option( '_vczapi_zoom_settings' );
$settings = ! empty( $settings ) ? $settings : false;
Datastore::get_vczapi_zoom_settings();

$args = array(
'labels' => $labels,
Expand Down Expand Up @@ -431,10 +432,17 @@ public function render_metabox( $post ) {

$meeting_fields = get_post_meta( $post->ID, '_meeting_fields', true );

$is_individual_account = Datastore::get_vczapi_zoom_settings( 'enable_individual_zoom' );

do_action( 'vczapi_before_fields_admin', $post );

$current_user_id = get_current_user_id();
$is_post_author = ($current_user_id == $post->post_author);

//Get Template
require_once ZVC_PLUGIN_VIEWS_PATH . '/post-type/tpl-meeting-fields.php';
$template = $is_individual_account && !$is_post_author ? '/post-type/tpl-meeting-fields-view-only.php' : '/post-type/tpl-meeting-fields.php';
require_once ZVC_PLUGIN_VIEWS_PATH . $template;

}

/**
Expand Down
27 changes: 23 additions & 4 deletions includes/admin/class-zvc-admin-settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Codemanas\VczApi\Data\Logger;
use Codemanas\VczApi\Data\Datastore;

/**
* Registering the Pages Here
Expand Down Expand Up @@ -132,7 +133,7 @@ public function zoomConnectHandler() {
update_option( 'zoom_api_key', $zoom_api_key );
update_option( 'zoom_api_secret', $zoom_api_secret );

$OAuth_access_token = \vczapi\S2SOAuth::get_instance()->generateAndSaveAccessToken( $vczapi_oauth_account_id, $vczapi_oauth_client_id, $vczapi_oauth_client_secret, );
$OAuth_access_token = \Codemanas\VczApi\Api\S2SOAuth::get_instance()->generateAndSaveAccessToken( $vczapi_oauth_account_id, $vczapi_oauth_client_id, $vczapi_oauth_client_secret );

if ( is_wp_error( $OAuth_access_token ) ) {
self::$message = sprintf( __( 'Zoom Oauth Error Code: "%s" - %s ', 'video-conferencing-with-zoom-api' ), $OAuth_access_token->get_error_code(), $OAuth_access_token->get_error_message() );
Expand Down Expand Up @@ -223,6 +224,23 @@ public function zoom_video_conference_menus() {
'Zoom_Video_Conferencing_Admin_Sync',
'render',
) );

$enable_individual_zoom = Datastore::get_vczapi_zoom_settings('enable_individual_zoom');
//this condition only works on page refresh since data saved in same admin call
if ( $enable_individual_zoom == 'on' ) {
add_submenu_page(
'edit.php?post_type=zoom-meetings',
__( 'Connect User Account', 'video-conferencing-with-zoom-api' ),
__( 'Connect User Account', 'video-conferencing-with-zoom-api' ),
'read',
'zoom-video-conferencing-connect',
function () {
$zoom_connect_user_account = Zoom_Connect_User_Account::get_instance();
$zoom_connect_user_account->save_api_credentials();
$zoom_connect_user_account->render();
}
);
}
}

add_submenu_page( 'edit.php?post_type=zoom-meetings', __( 'Settings', 'video-conferencing-with-zoom-api' ), __( 'Settings', 'video-conferencing-with-zoom-api' ), 'manage_options', 'zoom-video-conferencing-settings', array(
Expand Down Expand Up @@ -315,7 +333,8 @@ class="nav-tab <?php echo ( 'debug' === $active_tab ) ? esc_attr( 'nav-tab-activ
'join_via_browser_default_lang' => sanitize_text_field( filter_input( INPUT_POST, 'meeting-lang' ) ),
'disable_auto_pwd_generation' => sanitize_text_field( filter_input( INPUT_POST, 'disable_auto_pwd_generation' ) ),
'debugger_logs' => sanitize_text_field( filter_input( INPUT_POST, 'zoom_api_debugger_logs' ) ),
'enable_direct_join_via_browser' => sanitize_text_field( filter_input( INPUT_POST, 'vczapi_enable_direct_join' ) )
'enable_direct_join_via_browser' => sanitize_text_field( filter_input( INPUT_POST, 'vczapi_enable_direct_join' ) ),
'enable_individual_zoom' => sanitize_text_field( filter_input( INPUT_POST, 'vczapi_enable_individual_zoom' ) ),
];

/**
Expand Down Expand Up @@ -379,8 +398,8 @@ class="nav-tab <?php echo ( 'debug' === $active_tab ) ? esc_attr( 'nav-tab-activ
* New Method
* @added in 4.1.0
*/
$settings = get_option( '_vczapi_zoom_settings' );
$settings = ! empty( $settings ) ? $settings : false;
Datastore::get_vczapi_zoom_settings();


//Get Template
require_once ZVC_PLUGIN_VIEWS_PATH . '/tabs/api-settings.php';
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-zvc-admin-setup-wizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function save_oauth_credentials() {
update_option( 'vczapi_oauth_client_id', $client_id );
update_option( 'vczapi_oauth_client_secret', $client_secret );

$result = \vczapi\S2SOAuth::get_instance()->generateAndSaveAccessToken( $account_id, $client_id, $client_secret );
$result = \Codemanas\VczApi\Api\S2SOAuth::get_instance()->generateAndSaveAccessToken( $account_id, $client_id, $client_secret );
if ( ! is_wp_error( $result ) ) {
//this can't be a cached request
$decoded_users = json_decode( zoom_conference()->listUsers() );
Expand Down
31 changes: 22 additions & 9 deletions includes/api/S2SOAuth.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?php

namespace vczapi;
namespace Codemanas\VczApi\Api;

use Codemanas\VczApi\Data\Datastore;

class S2SOAuth {
public static $instance = null;

public bool $enable_individual_zoom = false;

public static function get_instance() {
return is_null( self::$instance ) ? self::$instance = new self() : self::$instance;
}

public function __construct() {

$this->enable_individual_zoom = Datastore::get_vczapi_zoom_settings('enable_individual_zoom');
}

/**
Expand Down Expand Up @@ -70,11 +74,15 @@ private function generateAccessToken( $account_id, $client_id, $client_secret )
*
* @return mixed
*/
public function generateAndSaveAccessToken( $account_id, $client_id, $client_secret ) {
public function generateAndSaveAccessToken( $account_id, $client_id, $client_secret, $save_to_user = false ) {
$result = $this->generateAccessToken( $account_id, $client_id, $client_secret );
if ( ! is_wp_error( $result ) ) {
//@todo - implement a per person option to allow other users to add their own API Credentials and generate own access token
update_option( 'vczapi_global_oauth_data', $result );
if ( ! $save_to_user ) {
update_option( 'vczapi_global_oauth_data', $result );
} else if ( current_user_can( 'edit_posts' ) && $this->enable_individual_zoom ) {
update_user_meta( get_current_user_id(), 'zoom_user_access_token', $result );
}
}

return $result;
Expand All @@ -86,11 +94,16 @@ public function generateAndSaveAccessToken( $account_id, $client_id, $client_sec
* @return void
*/
public function regenerateAccessTokenAndSave() {
$account_id = get_option( 'vczapi_oauth_account_id' );
$client_id = get_option( 'vczapi_oauth_client_id' );
$client_secret = get_option( 'vczapi_oauth_client_secret' );

$result = $this->generateAndSaveAccessToken( $account_id, $client_id, $client_secret );
if ( $this->enable_individual_zoom ) {
$account_id = get_user_meta( get_current_user_id(), 'zoom_user_account_id', true );
$client_id = get_user_meta( get_current_user_id(), 'zoom_user_client_id', true );
$client_secret = get_user_meta( get_current_user_id(), 'zoom_user_client_secret', true );
} else {
$account_id = get_option( 'vczapi_oauth_account_id' );
$client_id = get_option( 'vczapi_oauth_client_id' );
$client_secret = get_option( 'vczapi_oauth_client_secret' );
}
$result = $this->generateAndSaveAccessToken( $account_id, $client_id, $client_secret, $this->enable_individual_zoom );
if ( is_wp_error( $result ) ) {
//@todo log error if regenerating access token unsuccessful
}
Expand Down
14 changes: 10 additions & 4 deletions includes/api/class-zvc-zoom-api-v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use \Firebase\JWT\JWT;
use Codemanas\VczApi\Data\Logger;
use Codemanas\VczApi\Data\Datastore;

/**
* Class Connecting Zoom API V2
Expand Down Expand Up @@ -122,7 +123,7 @@ protected function sendRequest( $calledFunction, $data, $request = "GET" ) {

if ( $responseCode == 401 && vczapi_is_oauth_active() ) {
//only regenerate access token if it's already active;
\vczapi\S2SOAuth::get_instance()->regenerateAccessTokenAndSave();
\Codemanas\VczApi\Api\S2SOAuth::get_instance()->regenerateAccessTokenAndSave();
//only retry twice;
if ( self::$OAuth_revalidate_attempts <= 2 ) {
self::$OAuth_revalidate_attempts ++;
Expand Down Expand Up @@ -235,19 +236,24 @@ public function logMessage( $responseBody, $responseCode, $request ) {

private function getBearerToken() {
//@todo this will need to be modified for each user scenario
$OauthData = get_option( 'vczapi_global_oauth_data' );
if ( ! empty( $OauthData ) ) {
$OauthData = get_option( 'vczapi_global_oauth_data' );
$userOauthData = get_user_meta( get_current_user_id(), 'zoom_user_access_token', true );
$enable_individual_zoom = Datastore::get_vczapi_zoom_settings('enable_individual_zoom');

if (current_user_can( 'edit_posts' ) && $enable_individual_zoom == "on" && ! empty( $userOauthData ) ) {
return $userOauthData->access_token;
} elseif ( ! empty( $OauthData ) ) {
return $OauthData->access_token;
} else {
return $this->generateJWTKey();
}

}

/**
* Generate JWT key
*
* @return string
* @deprcated 4.2.2.
*/
private function generateJWTKey() {
$key = $this->zoom_api_key;
Expand Down
7 changes: 4 additions & 3 deletions includes/template-hooks.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
use Codemanas\VczApi\Data\Datastore;

/**
* @author Deepen.
* @created_on 11/20/19
Expand All @@ -18,9 +20,8 @@
add_action( 'vczoom_single_content_left', 'video_conference_zoom_main_content', 20 );

//Right Section Single Content
$settings = get_option( '_vczapi_zoom_settings' );
$settings = ! empty( $settings ) ? $settings : false;
if ( empty( $settings['disable_countdown_timer'] ) ) {
$disable_counter_timer = Datastore::get_vczapi_zoom_settings('disable_countdown_timer');
if ( empty( $disable_counter_timer) ) {
add_action( 'vczoom_single_content_right', 'video_conference_zoom_countdown_timer', 10 );
}
add_action( 'vczoom_single_content_right', 'video_conference_zoom_meeting_details', 20 );
Expand Down
Loading