Passed
Push — master ( aa7b09...d79aa2 )
by Ramon
07:26 queued 03:13
created

bika.lims.browser.senaitefrontpage.FrontPageView.set_versions()   A

Complexity

Conditions 4

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
cc 4
nop 1
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-2019 by it's authors.
19
# Some rights reserved, see README and LICENSE.
20
21
from Products.CMFCore.utils import getToolByName
22
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
23
24
from plone import api as ploneapi
25
from bika.lims.browser import BrowserView
26
from bika.lims.interfaces import IFrontPageAdapter
27
from zope.component import getAdapters
28
29
30
class FrontPageView(BrowserView):
31
    """SENAITE default Front Page
32
    """
33
    template = ViewPageTemplateFile("templates/senaite-frontpage.pt")
34
35
    def __call__(self):
36
        self.icon = self.portal_url + "/++resource++bika.lims.images/chevron_big.png"
37
        bika_setup = getToolByName(self.context, "bika_setup")
38
        login_url = '{}/{}'.format(self.portal_url, 'login')
39
        landingpage = bika_setup.getLandingPage()
40
41
        # Anonymous Users get either redirected to the std. bika-frontpage or
42
        # to the custom landing page, which is set in bika_setup. If no landing
43
        # page setup, then redirect to login page.
44
        if self.is_anonymous_user():
45
            # Redirect to the selected Landing Page
46
            if landingpage:
47
                return self.request.response.redirect(landingpage.absolute_url())
48
            # Redirect to login page
49
            return self.request.response.redirect(login_url)
50
51
        # Authenticated Users get either the Dashboard, the std. login page
52
        # or the custom landing page. Furthermore, they can switch between the
53
        # Dashboard and the landing page.
54
        # Add-ons can have an adapter for front-page-url as well.
55
        for name, adapter in getAdapters((self.context,), IFrontPageAdapter):
56
            redirect_to = adapter.get_front_page_url()
57
            if redirect_to:
58
                return self.request.response.redirect(self.portal_url + redirect_to)
59
60
        # First precedence: Request parameter `redirect_to`
61
        redirect_to = self.request.form.get("redirect_to", None)
62
        if redirect_to == "dashboard":
63
            return self.request.response.redirect(self.portal_url + "/bika-dashboard")
64
        if redirect_to == "frontpage":
65
            if landingpage:
66
                return self.request.response.redirect(landingpage.absolute_url())
67
            return self.template()
68
69
        # Second precedence: Dashboard enabled
70
        if self.is_dashboard_enabled():
71
            roles = self.get_user_roles()
72
            allowed = ['Manager', 'LabManager', 'LabClerk']
73
            if set(roles).intersection(allowed):
74
                return self.request.response.redirect(
75
                    self.portal_url + "/bika-dashboard")
76
            if 'Sampler' in roles or 'SampleCoordinator' in roles:
77
                return self.request.response.redirect(self.portal_url + "/samples?samples_review_state=to_be_sampled")
78
79
        # Third precedence: Custom Landing Page
80
        if landingpage:
81
            return self.request.response.redirect(landingpage.absolute_url())
82
83
        # Last precedence: Front Page
84
        return self.template()
85
86
    def is_dashboard_enabled(self):
87
        """Checks if the dashboard is enabled
88
        """
89
        bika_setup = getToolByName(self.context, "bika_setup")
90
        return bika_setup.getDashboardByDefault()
91
92
    def is_anonymous_user(self):
93
        """Checks if the current user is anonymous
94
        """
95
        return ploneapi.user.is_anonymous()
96
97
    def get_user_roles(self):
98
        """Returns a list of roles for the current user
99
        """
100
        if self.is_anonymous_user():
101
            return []
102
        current_user = ploneapi.user.get_current()
103
        return ploneapi.user.get_roles(user=current_user)
104