check if a user is enabled or not

This commit is contained in:
Matthias Pfefferle 2023-06-21 17:10:52 +02:00
parent 359cd57081
commit 58c04856c9
3 changed files with 72 additions and 18 deletions

View file

@ -30,15 +30,27 @@ class User_Factory {
* @return \Acitvitypub\Model\User The User.
*/
public static function get_by_id( $user_id ) {
if ( is_string( $user_id ) || is_numeric( $user_id ) ) {
$user_id = (int) $user_id;
}
if ( self::BLOG_USER_ID === $user_id ) {
if (
self::BLOG_USER_ID === $user_id &&
is_user_enabled( $user_id )
) {
return new Blog_User( $user_id );
} elseif ( self::APPLICATION_USER_ID === $user_id ) {
} elseif (
self::APPLICATION_USER_ID === $user_id &&
is_user_enabled( $user_id )
) {
return new Application_User( $user_id );
} else {
$user = get_user_by( 'ID', $user_id );
if ( ! $user || ! \user_can( $user, 'publish_posts' ) ) {
} elseif (
$user_id > 0 &&
is_user_enabled( $user_id )
) {
return new User( $user_id );
}
return new WP_Error(
'activitypub_user_not_found',
\__( 'User not found', 'activitypub' ),
@ -46,10 +58,6 @@ class User_Factory {
);
}
return new User( $user->ID );
}
}
/**
* Get the User by username.
*

View file

@ -277,3 +277,49 @@ function is_activitypub_request() {
function is_single_user_mode() {
return ACTIVITYPUB_SINGLE_USER_MODE;
}
/**
* This function checks if a user is enabled for ActivityPub.
*
* @param int $user_id The User-ID.
*
* @return boolean True if the user is enabled, false otherwise.
*/
function is_user_enabled( $user_id ) {
switch ( $user_id ) {
// if the user is the application user, it's always enabled.
case \Activitypub\User_Factory::APPLICATION_USER_ID:
return true;
// if the user is the blog user, it's only enabled in single-user mode.
case \Activitypub\User_Factory::BLOG_USER_ID:
if ( is_single_user_mode() ) {
return true;
}
return false;
// if the user is any other user, it's enabled if it can publish posts.
default:
if (
! is_single_user_mode() &&
\user_can( $user_id, 'publish_posts' )
) {
return true;
}
return false;
}
}
if ( ! function_exists( 'get_self_link' ) ) {
/**
* Get the correct self URL
*
* @return boolean
*/
function get_self_link() {
$host = wp_parse_url( home_url() );
return esc_url( apply_filters( 'self_link', set_url_scheme( 'http://' . $host['host'] . wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) );
}
}

View file

@ -197,7 +197,7 @@ class Post {
* @return int the User ID.
*/
public function get_user_id() {
return apply_filters( 'activitypub_post_user_id', $this->post_author, $this->post );
return apply_filters( 'activitypub_post_user_id', $this->get_post_author(), $this->post );
}
/**