def test_internal_user_has_necessary_permissions(self):
     internal_user = get_internal_user()
     all_codenames = internal_user.user_permissions.all().values_list(
         'codename', flat=True)
     self.assertTrue(u'read_dummymodel' in all_codenames)
     self.assertTrue(u'export_dummymodel' in all_codenames)
     self.assertTrue(u'read_attachment' in all_codenames)
 def setUp(self):
     self.middleware = AutoLoginMiddleware()
     self.request = RequestFactory()
     self.request.user = AnonymousUser()  # usually set by other middleware
     self.request.META = {'REMOTE_ADDR': '6.6.6.6'}
     self.internal_user = get_internal_user()
     call_command('update_permissions')
Example #3
0
    def setUp(self):
        middleware.clear_internal_user_cache()
        self.middleware = AutoLoginMiddleware(fake_view)
        self.request = RequestFactory()

        self.request.user = AnonymousUser()  # usually set by other middleware
        self.request.session = {}
        self.request.META = {'REMOTE_ADDR': '6.6.6.6'}
        self.internal_user = get_internal_user()
        call_command('update_geotrek_permissions')
Example #4
0
def create_mapentity_model_permissions(model):
    """
    Create all the necessary permissions for the specified model.

    And give all the required permission to the ``internal_user``, used
    for screenshotting and document conversion.

    :notes:

        Could have been implemented a metaclass on `MapEntityMixin`. We chose
        this approach to avoid problems with inheritance of permissions on
        abstract models.

        See:
            * https://code.djangoproject.com/ticket/10686
            * http://stackoverflow.com/a/727956/141895
    """
    if not issubclass(model, mapentity_models.MapEntityMixin):
        return

    db = DEFAULT_DB_ALIAS

    internal_user = get_internal_user()
    perms_manager = Permission.objects.using(db)

    permissions = set()
    for view_kind in mapentity_models.ENTITY_KINDS:
        perm = model.get_entity_kind_permission(view_kind)
        codename = auth.get_permission_codename(perm, model._meta)
        name = "Can %s %s" % (perm, model._meta.verbose_name_raw)
        permissions.add((codename, _(name)))

    ctype = ContentType.objects.db_manager(db).get_for_model(model)
    for (codename, name) in permissions:
        p, created = perms_manager.get_or_create(codename=codename,
                                                 content_type=ctype)
        if created:
            p.name = name[:50]
            p.save()
            logger.info("Permission '%s' created." % codename)

    for view_kind in (mapentity_models.ENTITY_LIST,
                      mapentity_models.ENTITY_DOCUMENT):
        perm = model.get_entity_kind_permission(view_kind)
        codename = auth.get_permission_codename(perm, model._meta)

        internal_user_permission = internal_user.user_permissions.filter(codename=codename,
                                                                         content_type=ctype)

        if not internal_user_permission.exists():
            permission = perms_manager.get(codename=codename, content_type=ctype)
            internal_user.user_permissions.add(permission)
            logger.info("Added permission %s to internal user %s" % (codename,
                                                                     internal_user))

    attachmenttype = ContentType.objects.db_manager(db).get_for_model(get_attachment_model())
    read_perm = dict(codename='read_attachment', content_type=attachmenttype)
    if not internal_user.user_permissions.filter(**read_perm).exists():
        permission = perms_manager.get(**read_perm)
        internal_user.user_permissions.add(permission)
        logger.info("Added permission %s to internal user %s" % (permission.codename,
                                                                 internal_user))
 def test_internal_user_permissions_work_as_others(self):
     internal_user = get_internal_user()
     self.assertTrue(user_has_perm(internal_user, 'tests.read_dummymodel'))
 def test_internal_user_has_necessary_permissions(self):
     internal_user = get_internal_user()
     all_codenames = internal_user.user_permissions.all().values_list('codename', flat=True)
     self.assertTrue(u'read_dummymodel' in all_codenames)
     self.assertTrue(u'export_dummymodel' in all_codenames)
     self.assertTrue(u'read_attachment' in all_codenames)
Example #7
0
 def test_internal_user_permissions_work_as_others(self):
     internal_user = get_internal_user()
     self.assertTrue(user_has_perm(internal_user, 'tests.read_dummymodel'))