bp_document_user_can_manage_document( int $document_id, int $user_id )

Check user have a permission to manage the document.

Description

Parameters

$document_id

(Required)

$user_id

(Required)

Return

(mixed|void)

Source

File: bp-document/bp-document-functions.php

function bp_document_user_can_manage_document( $document_id = 0, $user_id = 0 ) {

	$can_manage   = false;
	$can_view     = false;
	$can_download = false;
	$can_add      = false;
	$document     = new BP_Document( $document_id );
	$data         = array();

	switch ( $document->privacy ) {

		case 'public':
			if ( $document->user_id === $user_id ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = true;
			} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = false;
			} else {
				$can_manage   = false;
				$can_view     = true;
				$can_download = true;
			}
			break;

		case 'grouponly':
			if ( bp_is_active( 'groups' ) ) {

				$manage   = groups_can_user_manage_document( $user_id, $document->group_id );
				$status   = bp_group_get_document_status( $document->group_id );
				$is_admin = groups_is_user_admin( $user_id, $document->group_id );
				$is_mod   = groups_is_user_mod( $user_id, $document->group_id );

				if ( $manage ) {
					if ( $document->user_id === $user_id ) {
						$can_manage   = true;
						$can_add      = true;
					} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
						$can_manage   = true;
						$can_add      = false;
					} elseif ( 'members' == $status && ( $is_mod || $is_admin ) ) {
						$can_manage   = true;
						$can_add      = false;
					} elseif ( 'mods' == $status && ( $is_mod || $is_admin ) ) {
						$can_manage   = true;
						$can_add      = false;
					} elseif ( 'admins' == $status && $is_admin ) {
						$can_manage   = true;
						$can_add      = false;
					}
					$can_view     = true;
					$can_download = true;
				} else {
					$the_group = groups_get_group( (int) $document->group_id );
					if ( $the_group->id > 0 && $the_group->user_has_access ) {
						$can_view     = true;
						$can_download = true;
					}
				}
			}

			break;

		case 'loggedin':
			if ( $document->user_id === $user_id ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = true;
			} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = false;
			} elseif ( bp_loggedin_user_id() === $user_id ) {
				$can_manage   = false;
				$can_view     = true;
				$can_download = true;
			}
			break;

		case 'friends':

			$is_friend = ( bp_is_active( 'friends' ) ) ? friends_check_friendship( $document->user_id, $user_id ) : false;
			if ( $document->user_id === $user_id ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = true;
			} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = false;
			} elseif ( $is_friend ) {
				$can_manage   = false;
				$can_view     = true;
				$can_download = true;
			}
			break;

		case 'forums':
			$args       = array(
				'user_id'         => $user_id,
				'forum_id'        => bp_document_get_meta( $document_id, 'forum_id', true ),
				'check_ancestors' => false,
			);
			$has_access = bbp_user_can_view_forum( $args );
			if ( $document->user_id === $user_id ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = true;
			} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = false;
			} elseif ( $has_access ) {
				if ( bp_current_user_can( 'bp_moderate' ) ) {
					$can_manage   = true;
				}
				$can_view     = true;
				$can_download = true;
			}
			break;

		case 'message':
			$thread_id  = bp_document_get_meta( $document_id, 'thread_id', true );
			$has_access = messages_check_thread_access( $thread_id, $user_id );
			if ( ! is_user_logged_in() ) {
				$can_manage   = false;
				$can_view     = false;
				$can_download = false;
			} elseif ( ! $thread_id ) {
				$can_manage   = false;
				$can_view     = false;
				$can_download = false;
			} elseif ( $document->user_id === $user_id ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = true;
			} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = false;
			} elseif ( $has_access > 0 ) {
				$can_manage   = false;
				$can_view     = true;
				$can_download = true;
			}
			break;

		case 'onlyme':
			if ( $document->user_id === $user_id ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = true;
			} elseif ( bp_current_user_can( 'bp_moderate' ) ) {
				$can_manage   = true;
				$can_view     = true;
				$can_download = true;
				$can_add      = false;
			}
			break;

	}

	$data['can_manage']   = $can_manage;
	$data['can_view']     = $can_view;
	$data['can_download'] = $can_download;
	$data['can_add']      = $can_add;

	return apply_filters( 'bp_document_user_can_manage_folder', $data, $document_id, $user_id );
}

Changelog

Changelog
Version Description
BuddyBoss 1.4.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.