Passed
Pull Request — 2.x (#1977)
by
unknown
05:04
created

checkUserAccess()   A

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 22
rs 9.9
c 0
b 0
f 0
cc 3
nop 3
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of SENAITE.CORE.
4
#
5
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free Software
7
# Foundation, version 2.
8
#
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12
# details.
13
#
14
# You should have received a copy of the GNU General Public License along with
15
# this program; if not, write to the Free Software Foundation, Inc., 51
16
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
#
18
# Copyright 2018-2021 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from bika.lims import bikaMessageFactory as _
22
from bika.lims.interfaces import IWorksheetLayouts
23
24
from zope.component import getUtilitiesFor
25
from Products.CMFCore.utils import getToolByName
26
from Products.Archetypes.public import DisplayList
27
28
29
def checkUserAccess(worksheet, request, redirect=True):
30
    """ Checks if the current user has granted access to the worksheet.
31
        If the user is an analyst without LabManager, LabClerk and
32
        RegulatoryInspector roles and the option 'Allow analysts
33
        only to access to the Worksheets on which they are assigned' is
34
        ticked and the above condition is true, it will redirect to
35
        the main Worksheets view.
36
        Returns False if the user has no access, otherwise returns True
37
    """
38
    # Deny access to foreign analysts
39
    allowed = worksheet.checkUserAccess()
40
    if allowed == False and redirect == True:
41
        msg =  _('You do not have sufficient privileges to view '
42
                 'the worksheet ${worksheet_title}.',
43
                 mapping={"worksheet_title": worksheet.Title()})
44
        worksheet.plone_utils.addPortalMessage(msg, 'warning')
45
        # Redirect to WS list
46
        portal = getToolByName(worksheet, 'portal_url').getPortalObject()
47
        destination_url = portal.absolute_url() + "/worksheets"
48
        request.response.redirect(destination_url)
49
50
    return allowed
51
52
def checkUserManage(worksheet, request, redirect=True):
53
    """ Checks if the current user has granted access to the worksheet
54
        and if has also privileges for managing it. If the user has no
55
        granted access and redirect's value is True, redirects to
56
        /manage_results view. Otherwise, does nothing
57
    """
58
    allowed = worksheet.checkUserManage()
59
    if allowed == False and redirect == True:
60
        # Redirect to /manage_results view
61
        destination_url = worksheet.absolute_url() + "/manage_results"
62
        request.response.redirect(destination_url)
63
64
def showRejectionMessage(worksheet):
65
    """ Adds a portalMessage if
66
        a) the worksheet has been rejected and replaced by another or
67
        b) if the worksheet is the replacement of a rejected worksheet.
68
        Otherwise, does nothing.
69
    """
70
    if hasattr(worksheet, 'replaced_by'):
71
        uc = getToolByName(worksheet, 'uid_catalog')
72
        uid = getattr(worksheet, 'replaced_by')
73
        _ws = uc(UID=uid)[0].getObject()
74
        msg = _("This worksheet has been rejected.  The replacement worksheet is ${ws_id}",
75
                mapping={'ws_id':_ws.getId()})
76
        worksheet.plone_utils.addPortalMessage(msg)
77
    if hasattr(worksheet, 'replaces_rejected_worksheet'):
78
        uc = getToolByName(worksheet, 'uid_catalog')
79
        uid = getattr(worksheet, 'replaces_rejected_worksheet')
80
        _ws = uc(UID=uid)[0].getObject()
81
        msg = _("This worksheet has been created to replace the rejected "
82
                "worksheet at ${ws_id}",
83
                mapping={'ws_id':_ws.getId()})
84
        worksheet.plone_utils.addPortalMessage(msg)
85
86
87
def getWorksheetLayouts():
88
    """ Getting additional layouts for Worksheet
89
    """
90
    layouts = ()
91
    for name, layout_utility in getUtilitiesFor(IWorksheetLayouts):
92
        layouts += layout_utility.getLayoutsList()
93
94
    return DisplayList(layouts)
95