diff --git a/HISTORY.rst b/HISTORY.rst index 83550cf5f6..378cb8013e 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -112,6 +112,8 @@ solution. - Update the role permission edit view require the permission grant or permission revoke permissions for the selected role. +- Only show the new document link if the user has access to create documents of + at least one document type. GitLab Issue #302. Thanks to kg @kgraves. 2.7.3 (2017-09-11) ================== diff --git a/docs/releases/3.0.rst b/docs/releases/3.0.rst index 017388acba..07be7ad7cf 100644 --- a/docs/releases/3.0.rst +++ b/docs/releases/3.0.rst @@ -372,7 +372,6 @@ Other changes worth mentioning - Sort permission namespaces and permissions in the role permission views. - Invert the columns in the ACL detail view. - Removals -------- * None @@ -431,6 +430,7 @@ Bugs fixed or issues closed =========================== * `GitLab issue #262 `_ Event notifications +* `GitLab issue #302 `_ 'New Document' button available to users who do not have permission * `GitLab issue #454 `_ Invalid next month calculation in statistics app, causes failstop .. _PyPI: https://pypi.python.org/pypi/mayan-edms/ diff --git a/mayan/apps/sources/links.py b/mayan/apps/sources/links.py index 793e3a59bf..8141a3b6ad 100644 --- a/mayan/apps/sources/links.py +++ b/mayan/apps/sources/links.py @@ -18,6 +18,23 @@ from .permissions import ( ) +def condition_check_document_creation_acls(context): + AccessControlList = apps.get_model( + app_label='acls', model_name='AccessControlList' + ) + DocumentType = apps.get_model( + app_label='documents', model_name='DocumentType' + ) + + queryset = AccessControlList.objects.filter_by_access( + permission=permission_document_create, user=context['user'], + queryset=DocumentType.objects.all() + ) + + if queryset: + return True + + def document_new_version_not_blocked(context): NewVersionBlock = apps.get_model( app_label='checkouts', model_name='NewVersionBlock' @@ -27,8 +44,8 @@ def document_new_version_not_blocked(context): link_document_create_multiple = Link( - icon='fa fa-upload', text=_('New document'), - view='sources:document_create_multiple' + condition=condition_check_document_creation_acls, icon='fa fa-upload', + text=_('New document'), view='sources:document_create_multiple' ) link_setup_sources = Link( icon='fa fa-upload', permissions=(permission_sources_setup_view,), diff --git a/mayan/apps/sources/tests/test_links.py b/mayan/apps/sources/tests/test_links.py new file mode 100644 index 0000000000..e23dc96695 --- /dev/null +++ b/mayan/apps/sources/tests/test_links.py @@ -0,0 +1,31 @@ +from __future__ import unicode_literals + +from django.contrib.contenttypes.models import ContentType +from django.urls import reverse + +from acls.models import AccessControlList +from documents.tests import GenericDocumentViewTestCase +from documents.permissions import permission_document_create + +from ..links import link_document_create_multiple + + +class SourcesLinksTestCase(GenericDocumentViewTestCase): + def setUp(self): + super(SourcesLinksTestCase, self).setUp() + self.login_user() + + def _get_document_create_link(self): + self.add_test_view(test_object=self.document) + context = self.get_test_view() + context['user'] = self.user + return link_document_create_multiple.resolve(context=context) + + def test_document_create_link_no_access(self): + resolved_link = self._get_document_create_link() + self.assertEqual(resolved_link, None) + + def test_document_create_link_with_access(self): + self.grant_access(permission=permission_document_create, obj=self.document_type) + resolved_link = self._get_document_create_link() + self.assertNotEqual(resolved_link, None)