• Hi,

    great plugin! Good work!

    But I want to ask if it would be possible to restrict access to a file to only a single or some more specific users? This would be extremely helpful.

    My first intention was to select a different author for the files but unfortunately a subscriber for example can′t be the author of a file.

    I read in the last topic
    https://www.ads-software.com/support/topic/restrict-only-for-subscribers?replies=7

    that there is a function mgjp_mv_add_permission() for customizing access permissions. But I am not sure wether this could be what I need since I’d need some sort of dropdown or sth for the admin user to select the users that should have access to a file

    Thanks in advance!

    Regards,
    Jonathan

    https://www.ads-software.com/plugins/media-vault/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter mjHollerATme

    (@mjholleratme)

    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

    Thread Starter mjHollerATme

    (@mjholleratme)

    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

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Allow acces only by some users’ is closed to new replies.