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

Sync User Profile Photo #124

Open
nyulawbrian opened this issue Feb 11, 2023 · 1 comment
Open

Sync User Profile Photo #124

nyulawbrian opened this issue Feb 11, 2023 · 1 comment

Comments

@nyulawbrian
Copy link

This is a feature request to add support for pulling the user's photo for their WP profile. For example, in an LDAP connection to Microsoft Active Directory, this would be the thumbnailPhoto attribute (byte encoded). The string would need to be converted to an image file and set as the user's profile photo. This would need to be updated upon login, same as first and last name.

Thanks so much!

@figureone
Copy link
Member

By default WordPress uses gravatar for profile photos so there's not a built-in way of editing profile images. That said, you should be able to get this behavior using a few filters:

Tell Authorizer to fetch the thumbnailPhoto attribute:

add_filter( 'authorizer_additional_ldap_attributes_to_retrieve', function ( $attributes ) {
	$attributes[] = 'thumbnailPhoto';

	return $attributes;
} );

Upload that photo to wp-content/uploads/ldap/01/profile-photo-{username}.jpg on login:

add_filter( 'authorizer_custom_role', function ( $default_role, $user_data ) {
	if ( ! empty( $user_data['ldap_attributes'][ 0 ]['thumbnailphoto'][ 0 ] ) ) {
		$jpeg_data      = $user_data['ldap_attributes'][ 0 ]['thumbnailphoto'][ 0 ];
		$filename       = 'profile-photo-' . $user_data['username'] . '.jpg';
		$uploads_subdir = 'ldap/01';
		$uploads_dir    = wp_upload_dir();
		$full_path      = $uploads_dir['basedir'] . '/' . $uploads_subdir . '/' . $filename;
		if ( file_exists( $full_path ) ) {
			wp_delete_file( $full_path );
		}
		$file = wp_upload_bits( $filename, null, $jpeg_data, $uploads_subdir );
	}

	return $default_role;
}, 10, 2 );

Override the gravatar with the custom photo (if it exists):

add_filter( 'pre_get_avatar_data', function ( $args, $id_or_email ) {
	// Process the user identifier.
	$user = false;
	if ( is_numeric( $id_or_email ) ) {
		$user = get_user_by( 'id', absint( $id_or_email ) );
	} elseif ( is_string( $id_or_email ) ) {
		if ( ! strpos( $id_or_email, '@md5.gravatar.com' ) ) {
			$user = get_user_by( 'email', $id_or_email );
		}
	} elseif ( $id_or_email instanceof WP_User ) {
		// User object.
		$user = $id_or_email;
	} elseif ( $id_or_email instanceof WP_Post ) {
		// Post object.
		$user = get_user_by( 'id', (int) $id_or_email->post_author );
	} elseif ( $id_or_email instanceof WP_Comment && is_avatar_comment_type( get_comment_type( $id_or_email ) ) ) {
		if ( ! empty( $id_or_email->user_id ) ) {
			$user = get_user_by( 'id', (int) $id_or_email->user_id );
		}
		if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
			$user = get_user_by( 'email', $id_or_email->comment_author_email );
		}
	}

	if ( ! empty( $user->user_login ) ) {
		$filename       = 'profile-photo-' . $user->user_login . '.jpg';
		$uploads_subdir = 'ldap/01';
		$uploads_dir    = wp_upload_dir();
		$full_path      = $uploads_dir['basedir'] . '/' . $uploads_subdir . '/' . $filename;
		$full_url       = $uploads_dir['baseurl'] . '/' . $uploads_subdir . '/' . $filename;
		if ( file_exists( $full_path ) ) {
			$args['url'] = $full_url;
			$image_size = getimagesize( $full_path );
			// If not 1:1 aspect ratio, update width/height to prevent stretching.
			if ( $image_size[ 1 ] > $image_size[ 0 ] ) {
				$args['height'] = $args['size'];
				$args['width']  = $args['size'] * $image_size[ 0 ] / $image_size[ 1 ];
			} else {
				$args['height'] = $args['size'] * $image_size[ 1 ] / $image_size[ 0 ];
				$args['width']  = $args['size'];
			}
		}
	}

	return $args;
}, 10, 2 );

I tested that on OpenLDAP which uses jpegPhoto attribute and it worked. There may be a few differences for AD, you might have to inspect some of that data.

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

No branches or pull requests

3 participants