Can we set download permission to Filefield?

What I need is to create a downloadable article that allows public user to see the abstract. Only when they are logged in, they are able to download the article's pdf.

It will also be great if someone can advice me on how I can approach this.

Thank you

Comments

yohaneszh’s picture

Anyone can help me out with this? :)

bmango’s picture

Hi,

I was just trying to do the same thing and solved it by doing the following:

You need to set your file field to use the private files for the upload settings. This means the files cannot be accessed directly by a url but have to be accessed through Drupal (see http://drupal.org/documentation/modules/file).

You can then use hook_file_download() to check if the user is logged in. You will need to put hook_file_download in a custom module that you create (this is quite straightforward - just google it). I used the following code in a custom module which worked for me:

<?php

function mymodule_file_download($uri) {
	global $user;
	 if ( !$user->uid )  {
     return -1;
	}
}
?>

This code checks if the user is not logged in and then prevents them from accessing the attachment.