From da87b937a17bfa1ea01b6f5ac80fbd789e451c1d Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Mon, 3 Jul 2017 15:02:20 +1000 Subject: [PATCH 1/6] Store creation time in the token --- inc/tokens/class-access-token.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/inc/tokens/class-access-token.php b/inc/tokens/class-access-token.php index f3f7836..cb40cac 100644 --- a/inc/tokens/class-access-token.php +++ b/inc/tokens/class-access-token.php @@ -27,6 +27,15 @@ public function get_client() { return Client::get_by_id( $this->value['client'] ); } + /** + * Get creation time for the token. + * + * @return int Creation timestamp. + */ + public function get_creation_time() { + return $this->value['created']; + } + /** * Revoke the token. * @@ -116,7 +125,8 @@ public static function create( Client $client, WP_User $user ) { } $data = array( - 'client' => $client->get_id(), + 'client' => $client->get_id(), + 'created' => time(), ); $key = wp_generate_password( static::KEY_LENGTH, false ); $meta_key = static::META_PREFIX . $key; From e434ecfa80e2f7ab006a0790f34914f2a97c25e6 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Mon, 3 Jul 2017 15:02:47 +1000 Subject: [PATCH 2/6] Display token creation time in list --- inc/admin/profile/namespace.php | 54 ++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/inc/admin/profile/namespace.php b/inc/admin/profile/namespace.php index 34f65cf..103747c 100644 --- a/inc/admin/profile/namespace.php +++ b/inc/admin/profile/namespace.php @@ -40,17 +40,11 @@ function render_profile_section( WP_User $user ) { - - get_client(); - ?> - - get_name() ?> - - - - + @@ -63,6 +57,44 @@ function render_profile_section( WP_User $user ) { get_client(); + + $details = [ + sprintf( + /* translators: %s: formatted date */ + esc_html__( 'Authorized %s', 'oauth2' ), + date( get_option( 'date_format' ), $token->get_creation_time() ) + ), + ]; + $button_title = sprintf( + /* translators: %s: app name */ + __( 'Revoke access for "%s"', 'oauth2' ), + $client->get_name() + ); + ?> + + +

get_name() ?>

+

+ + + + + + Date: Mon, 3 Jul 2017 15:03:39 +1000 Subject: [PATCH 3/6] Break token list out of table --- inc/admin/profile/namespace.php | 43 ++++++++++++++------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/inc/admin/profile/namespace.php b/inc/admin/profile/namespace.php index 103747c..06240ab 100644 --- a/inc/admin/profile/namespace.php +++ b/inc/admin/profile/namespace.php @@ -12,7 +12,8 @@ * Bootstrap actions for the profile screen. */ function bootstrap() { - add_action( 'personal_options', __NAMESPACE__ . '\\render_profile_section', 50 ); + add_action( 'show_user_profile', __NAMESPACE__ . '\\render_profile_section' ); + add_action( 'edit_user_profile', __NAMESPACE__ . '\\render_profile_section' ); add_action( 'all_admin_notices', __NAMESPACE__ . '\\output_profile_messages' ); add_action( 'personal_options_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 ); add_action( 'edit_user_profile_update', __NAMESPACE__ . '\\handle_revocation', 10, 1 ); @@ -26,34 +27,26 @@ function bootstrap() { function render_profile_section( WP_User $user ) { $tokens = Access_Token::get_for_user( $user ); ?> - - +

+ +
+ - - + + + + +
- - - - - - - - - - - -
- -

- -
+ +

+ Date: Mon, 3 Jul 2017 15:06:46 +1000 Subject: [PATCH 4/6] Add details filter --- inc/admin/profile/namespace.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/inc/admin/profile/namespace.php b/inc/admin/profile/namespace.php index 06240ab..4a13fa5 100644 --- a/inc/admin/profile/namespace.php +++ b/inc/admin/profile/namespace.php @@ -39,7 +39,7 @@ function render_profile_section( WP_User $user ) { @@ -53,7 +53,7 @@ function render_profile_section( WP_User $user ) { /** * Render a single row. */ -function render_token_row( Access_Token $token ) { +function render_token_row( WP_User $user, Access_Token $token ) { $client = $token->get_client(); $details = [ @@ -63,6 +63,16 @@ function render_token_row( Access_Token $token ) { date( get_option( 'date_format' ), $token->get_creation_time() ) ), ]; + + /** + * Filter details shown for an access token on the profile screen. + * + * @param string[] $details List of HTML snippets to render in table. + * @param Access_Token $token Token being displayed. + * @param WP_User $user User whose profile is being rendered. + */ + $details = apply_filters( 'oauth2.admin.profile.render_token_row.details', $details, $token, $user ); + $button_title = sprintf( /* translators: %s: app name */ __( 'Revoke access for "%s"', 'oauth2' ), From 43b5e3cd18fbf50e87c202c07b5a2881a1bd0939 Mon Sep 17 00:00:00 2001 From: Ryan McCue Date: Mon, 3 Jul 2017 15:09:49 +1000 Subject: [PATCH 5/6] Allow filtering actions for an access token --- inc/admin/profile/namespace.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/inc/admin/profile/namespace.php b/inc/admin/profile/namespace.php index 4a13fa5..5b9e7a9 100644 --- a/inc/admin/profile/namespace.php +++ b/inc/admin/profile/namespace.php @@ -73,11 +73,29 @@ function render_token_row( WP_User $user, Access_Token $token ) { */ $details = apply_filters( 'oauth2.admin.profile.render_token_row.details', $details, $token, $user ); + // Build actions. $button_title = sprintf( /* translators: %s: app name */ __( 'Revoke access for "%s"', 'oauth2' ), $client->get_name() ); + $actions = [ + sprintf( + '', + $button_title, + esc_attr( $token->get_key() ), + esc_html__( 'Revoke', 'oauth2' ) + ), + ]; + + /** + * Filter actions shown for an access token on the profile screen. + * + * @param string[] $actions List of HTML snippets to render in table. + * @param Access_Token $token Token being displayed. + * @param WP_User $user User whose profile is being rendered. + */ + $actions = apply_filters( 'oauth2.admin.profile.render_token_row.actions', $actions, $token, $user ); ?> @@ -85,14 +103,7 @@ function render_token_row( WP_User $user, Access_Token $token ) {

- + Date: Mon, 3 Jul 2017 15:12:55 +1000 Subject: [PATCH 6/6] Display authorization time --- inc/admin/profile/namespace.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/inc/admin/profile/namespace.php b/inc/admin/profile/namespace.php index 5b9e7a9..f097d3f 100644 --- a/inc/admin/profile/namespace.php +++ b/inc/admin/profile/namespace.php @@ -56,11 +56,13 @@ function render_profile_section( WP_User $user ) { function render_token_row( WP_User $user, Access_Token $token ) { $client = $token->get_client(); + $creation_time = $token->get_creation_time(); $details = [ sprintf( - /* translators: %s: formatted date */ - esc_html__( 'Authorized %s', 'oauth2' ), - date( get_option( 'date_format' ), $token->get_creation_time() ) + /* translators: %1$s: formatted date, %2$s: formatted time */ + esc_html__( 'Authorized %1$s at %2$s', 'oauth2' ), + date( get_option( 'date_format' ), $creation_time ), + date( get_option( 'time_format' ), $creation_time ) ), ];