仅允许超级用户使用导入/导出功能

时间:2018-03-02 06:21:32

标签: django import export django-import-export

我想只为超级用户提供导入/导出功能。如何实现这一目标?

我设法通过自定义css隐藏导入/导出按钮。但我也想禁用相应的函数调用:

http://localhost:9012/admin/persons/person/import/ http://localhost:9012/admin/persons/person/export/

1 个答案:

答案 0 :(得分:2)

如果您使用的是基于功能的视图,则可以使用user_passes_test装饰器来控制哪些用户可以使用该视图。要求用户是超级用户,您可以执行此操作

// certificate from smtp.live.com
$cert = Certificate::fromPEM(PEM::fromString($certdata));
// list of trust anchors from https://curl.haxx.se/ca/cacert.pem
$trusted = CertificateBundle::fromPEMBundle(PEMBundle::fromFile('cacert.pem'));
// intermediate certificate from
// https://www.digicert.com/CACerts/DigiCertSHA2SecureServerCA.crt
$intermediates = new CertificateBundle(
    Certificate::fromDER(file_get_contents('DigiCertSHA2SecureServerCA.crt')));
// build certification path
$path_builder = new CertificationPathBuilder($trusted);
$certification_path = $path_builder->shortestPathToTarget($cert, $intermediates);
// validate certification path
$result = $certification_path->validate(PathValidationConfig::defaultConfig());
// failure would throw an exception
echo "Validation successful\n";

或者,如果您使用的是基于类的视图,则可以像这样使用UserPassesTestMixin

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.is_superuser)
def my_view(request):
    ...

文档链接: https://docs.djangoproject.com/en/2.0/topics/auth/default/#django.contrib.auth.decorators.user_passes_test

相关问题