ok I think I have a solution, didn´t test it yet much but I think it wil work fine. I will run some more tests though
function wpst_mv_register_custom_permissions() {
if ( function_exists( 'mgjp_mv_add_permission' ) ) {
$users = get_users();
foreach($users as $user){
mgjp_mv_add_permission( 'user_' . $user->ID, array(
'description' => 'to only the user ' . $user->user_login,
'select' => 'User: ' . $user->user_login,
'logged_in' => true, // whether the user must be logged in
'run_in_admin' => false, // whether to run the access check in admin
'cb' => 'wpst_mv_restrict_only_for_user'
) );
}
}
}
add_action( 'after_setup_theme', 'wpst_mv_register_custom_permissions' );
function wpst_mv_restrict_only_for_user( $attachment_id ) {
if ( ! isset( $attachment_id ) || empty( $attachment_id ) )
return new WP_Error( 'no_id', __( 'There was an error determining this attachment\'s author. Please contact the website administrator.', 'media-vault' ) );
$permission_name = get_post_meta( $attachment_id, '_mgjp_mv_permission', true );
$user_permission_id = explode( "_", $permission_name);
$user_permission_id = 0 + $user_permission_id[1];
$curr_user_id = get_current_user_id();
if( $curr_user_id == $user_permission_id || current_user_can( 'manage_options' ) ) {
return true;
}else{
return new WP_Error( 'not_user', __( 'You do not have sufficient permissions to view this file.', 'media-vault' ) );
}
}
Hi Jonathan,
I want to do the same thing. I have test your code but it doesn’t works. If I set permission to a file for a specific user, an other logged in user can access to the file.
Have you test it and find a solution?
Regards,
Nicolas
Hello Nico,
No I didn’t test it any further. But what kind of user do you mean? If these are admin accounts they’ll always have access because in this line the function will return true also for users who can manage options (so usually just admins).
if( $curr_user_id == $user_permission_id || current_user_can( ‘manage_options’ ) )
Regards,
Jonathan