简体   繁体   English

Drupal:按权限限制文件上传

[英]Drupal: Restrict File Uploads by Permission

On my Drupal 7 site, I am looking for a way to only allow users with a specific role to upload PDF files. 在我的Drupal 7网站上,我正在寻找一种仅允许具有特定角色的用户上载PDF文件的方法。 All users, however, will be able to upload other file types (eg jpg, mp4). 但是,所有用户都可以上传其他文件类型(例如jpg,mp4)。

Is there a module or some code available that can accomplish that tool for me? 是否有可用的模块或代码可以为我完成该工具?

You dont need a module for that. 您不需要为此的模块。 You can write a hook_form_alter and do the following 您可以编写一个hook_form_alter并执行以下操作

 1. add help-text based on role.
if role = 1
#form['field_name']['#description'] = t('Help test 1')
elseif role = 2
#form['field_name']['#description'] = t('Help test 2')
  1. Add custom validation to check file extension based on roles. 添加自定义验证以根据角色检查文件扩展名。

    $form['#validate'][] = 'validate_function'; $ form ['#validate'] [] ='validate_function'; function validate_function if role = 1 Validate file type 1 elseif role = 2 Validate file type 2 函数validate_function如果角色= 1验证文件类型1 elseif角色= 2验证文件类型2

cheers! 干杯!

You can use hook_form_alter to achieve this. 您可以使用hook_form_alter实现此目的。

load your user object 加载您的用户对象

global $user;
$valid_role = 'yourGivenRole';

if(in_array($valid, $user->roles)){
     $form['field_name']['und'][0]['#upload_validators']['file_validate_extensions'][0]  = 'pdf';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM