Ignore:
Timestamp:
3 May 2015, 06:49:12 (10 years ago)
Author:
Henrik Bettermann
Message:

Add ReportsOfficer? role. The RO is allowed to view and remove only his/her reports.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/browser
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/reports.py

    r12603 r12900  
    2121from zope.component import getUtility, queryUtility
    2222from zope.location.location import located
     23from zope.security import checkPermission
    2324from waeup.kofa.interfaces import IJobManager, IKofaUtils
    2425from waeup.kofa.interfaces import MessageFactory as _
    2526from waeup.kofa.browser.layout import KofaPage
    26 from waeup.kofa.reports import IReportsContainer, IReportGenerator
    27 from waeup.kofa.reports import get_generators
     27from waeup.kofa.utils.helpers import get_current_principal
     28from waeup.kofa.reports import (
     29    IReportsContainer, IReportGenerator, get_generators)
    2830
    2931
     
    3638    grok.name('index')
    3739    grok.context(IReportsContainer)
    38     grok.require('waeup.manageReports')
     40    grok.require('waeup.handleReports')
    3941    label = _('Reports')
    4042
     
    5456            grok.getSite().logger.info(
    5557                '%s - report %s discarded' % (ob_class, job_id))
    56         self.entries = self._generate_entries(user_id=None)
     58        if not checkPermission('waeup.manageReports', self.context):
     59            user = get_current_principal()
     60            self.entries = self._generate_entries(user_id=user.id)
     61        else:
     62            self.entries = self._generate_entries(user_id=None)
    5763        if job_id and DOWNLOAD:
    5864            self.redirect(self._report_url(job_id))
     
    114120    grok.name('create')
    115121    grok.context(IReportsContainer)
    116     grok.require('waeup.manageReports')
     122    grok.require('waeup.handleReports')
    117123    label = _('Create report')
    118124
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/tests/test_permissions.py

    r8920 r12900  
    2626import shutil
    2727import tempfile
     28from zc.async.testing import wait_for_result
    2829from zope.app.testing.functional import HTTPCaller as http
    29 from zope.component import createObject
     30from zope.securitypolicy.interfaces import IPrincipalRoleManager
     31from zope.component import createObject, getUtility
    3032from zope.component.hooks import setSite, clearSite
    3133from zope.security.interfaces import Unauthorized
    3234from zope.testbrowser.testing import Browser
     35from waeup.kofa.interfaces import IJobManager
    3336from waeup.kofa.app import University
    3437from waeup.kofa.testing import (
    3538    FunctionalLayer, FunctionalTestCase, get_all_loggers, remove_new_loggers,
    3639    remove_logger)
     40from waeup.kofa.tests.test_async import FunctionalAsyncTestCase
     41
     42
    3743
    3844manager_pages = [
     
    6369    ]
    6470
    65 class PermissionTest(FunctionalTestCase):
     71class PermissionTest(FunctionalAsyncTestCase, FunctionalTestCase):
    6672    """Here we try to request all pages and check, whether they are
    6773    accessible.
     
    9298        dept.certificates.addCertificate(cert)
    9399        cert.addCertCourse(course)
     100        self.app = app
    94101
    95102        self.browser = Browser()
     
    110117        except Unauthorized:
    111118            return False
     119        return
     120
     121    def wait_for_report_job_completed(self, number):
     122        # helper function waiting until the current export job is completed
     123        manager = getUtility(IJobManager)
     124        job_id = self.app['reports'].running_report_jobs[number][0]
     125        job = manager.get(job_id)
     126        wait_for_result(job)
     127        return job_id
     128
     129    def stored_in_reports(self, job_id):
     130        # tell whether job_id is stored in reports's running jobs list
     131        for entry in list(self.app['reports'].running_report_jobs):
     132            if entry[0] == job_id:
     133                return True
     134        return False
     135
     136    def trigger_report_creation(self, session):
     137        self.browser.open('http://localhost/app/reports')
     138        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     139        self.browser.getLink("Create new report").click()
     140        self.browser.getControl(name="generator").value = ['student_stats']
     141        self.browser.getControl("Configure").click()
     142        self.browser.getControl(name="breakdown").value = ['depcode']
     143        self.browser.getControl(name="mode").value = ['All']
     144        self.browser.getControl(name="session").value = [session]
     145        self.browser.getControl("Create").click()
    112146        return
    113147
     
    122156            self.fail('Path %s cannot be accessed by anonymous.' % path)
    123157        return
     158
     159    def testReportsPermissions(self):
     160        # Create reports officer
     161        self.app['users'].addUser('mrofficer', 'mrofficer')
     162        self.app['users']['mrofficer'].email = 'mrofficer@foo.ng'
     163        self.app['users']['mrofficer'].title = 'Otto Report'
     164        prmglobal = IPrincipalRoleManager(self.app)
     165        prmglobal.assignRoleToPrincipal('waeup.ReportsOfficer', 'mrofficer')
     166        # Create reports manager
     167        self.app['users'].addUser('mrmanager', 'mrmanager')
     168        self.app['users']['mrmanager'].email = 'mrmanager@foo.ng'
     169        self.app['users']['mrmanager'].title = 'Manfred Report'
     170        prmglobal.assignRoleToPrincipal('waeup.ReportsManager', 'mrmanager')
     171        # The reports officer creates a report which the reports manager
     172        # can see.
     173        self.browser.open('http://localhost/app/login')
     174        self.browser.getControl(name="form.login").value = 'mrofficer'
     175        self.browser.getControl(name="form.password").value = 'mrofficer'
     176        self.browser.getControl("Login").click()
     177        self.trigger_report_creation('2004')
     178        job_id = self.wait_for_report_job_completed(0)
     179        self.browser.open('http://localhost/app/reports')
     180        self.assertTrue(
     181            'Student Statistics (depcode, 2004, All, 0)'
     182            in self.browser.contents)
     183        self.browser.open('http://localhost/app/logout')
     184        # The reports manager creates a report which the reports officer
     185        # can't see.
     186        self.browser.open('http://localhost/app/login')
     187        self.browser.getControl(name="form.login").value = 'mrmanager'
     188        self.browser.getControl(name="form.password").value = 'mrmanager'
     189        self.browser.getControl("Login").click()
     190        self.trigger_report_creation('2005')
     191        job_id = self.wait_for_report_job_completed(1)
     192        self.browser.open('http://localhost/app/reports')
     193        # Manager can see both reports.
     194        self.assertTrue(
     195            'Student Statistics (depcode, 2004, All, 0)'
     196            in self.browser.contents)
     197        self.assertTrue(
     198            'Student Statistics (depcode, 2005, All, 0)'
     199            in self.browser.contents)
     200        self.browser.open('http://localhost/app/logout')
     201        self.browser.open('http://localhost/app/login')
     202        self.browser.getControl(name="form.login").value = 'mrofficer'
     203        self.browser.getControl(name="form.password").value = 'mrofficer'
     204        self.browser.getControl("Login").click()
     205        self.browser.open('http://localhost/app/reports')
     206        # Officer can only see his report.
     207        self.assertTrue(
     208            'Student Statistics (depcode, 2004, All, 0)'
     209            in self.browser.contents)
     210        self.assertFalse(
     211            'Student Statistics (depcode, 2005, All, 0)'
     212            in self.browser.contents)
     213        return
  • main/waeup.kofa/trunk/src/waeup/kofa/browser/viewlets.py

    r12632 r12900  
    344344    """
    345345    grok.order(4)
    346     grok.require('waeup.manageReports')
     346    grok.require('waeup.handleReports')
    347347
    348348    link = u'reports'
Note: See TracChangeset for help on using the changeset viewer.