app/soc/views/helper/access.py
author Mario Ferraro <fadinlight@gmail.com>
Sun, 15 Nov 2009 22:12:20 +0100
changeset 3093 d1be59b6b627
parent 3070 afd98e17a2b2
permissions -rw-r--r--
GMaps related JS changed to use new google namespace. Google is going to change permanently in the future the way to load its services, so better stay safe. Also this commit shows uses of the new melange.js module. Fixes Issue 634.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Access control helper.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
The functions in this module can be used to check access control
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
related requirements. When the specified required conditions are not
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
met, an exception is raised. This exception contains a views that
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
either prompts for authentication, or informs the user that they
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
do not meet the required criteria.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
"""
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    25
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    26
__authors__ = [
2957
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    27
  '"Madhusudan.C.S" <madhusudancs@gmail.com>',
3070
afd98e17a2b2 Minor (mostly stylish and whitespace) fixes in GHOP access.py module.
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 3068
diff changeset
    28
  '"Daniel Hans" <daniel.m.hans@gmail.com>',
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    29
  '"Todd Larsen" <tlarsen@google.com>',
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
726
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
    31
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
  ]
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 965
diff changeset
    36
from django.utils.translation import ugettext
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    38
from soc.logic import dicts
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
    39
from soc.logic.helper import timeline as timeline_helper
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
    40
from soc.logic.models.club_admin import logic as club_admin_logic
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
    41
from soc.logic.models.club_member import logic as club_member_logic
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1107
diff changeset
    42
from soc.logic.models.document import logic as document_logic
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
    43
from soc.logic.models.host import logic as host_logic
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    44
from soc.logic.models.mentor import logic as mentor_logic
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    45
from soc.logic.models.org_admin import logic as org_admin_logic
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
    46
from soc.logic.models.organization import logic as org_logic
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    47
from soc.logic.models.program import logic as program_logic
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
    48
from soc.logic.models.request import logic as request_logic
979
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
    49
from soc.logic.models.role import logic as role_logic
891
3d40190f35b6 Move getToSLink() to soc.views.helper.redirects.getToSRedirect().
Todd Larsen <tlarsen@google.com>
parents: 890
diff changeset
    50
from soc.logic.models.site import logic as site_logic
1445
c2e09f7d62d9 Forgotten import
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1444
diff changeset
    51
from soc.logic.models.sponsor import logic as sponsor_logic
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
    52
from soc.logic.models.student import logic as student_logic
2112
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
    53
from soc.logic.models.student_project import logic as student_project_logic
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
    54
from soc.logic.models.student_proposal import logic as student_proposal_logic
1142
da2487767ef4 Fix missing dot and wrong import sorting in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1135
diff changeset
    55
from soc.logic.models.timeline import logic as timeline_logic
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
    56
from soc.logic.models.user import logic as user_logic
2836
0181649aa7b0 Use per-request store to store access rights
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2792
diff changeset
    57
from soc.modules import callback
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    58
from soc.views.helper import redirects
543
280a1ac6bcc1 Merge soc/logic/out_of_band.py into soc/views/out_of_band.py. Merge
Todd Larsen <tlarsen@google.com>
parents: 525
diff changeset
    59
from soc.views import out_of_band
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
2957
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    61
from soc.modules.ghop.logic.models.mentor import logic as ghop_mentor_logic
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    62
from soc.modules.ghop.logic.models.organization import logic as ghop_org_logic
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    63
from soc.modules.ghop.logic.models.org_admin import logic as \
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    64
    ghop_org_admin_logic
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    65
from soc.modules.ghop.logic.models.program import logic as ghop_program_logic
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    66
from soc.modules.ghop.logic.models.student import logic as ghop_student_logic
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
    67
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
    69
DEF_NO_USER_LOGIN_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    70
    'Please create <a href="/user/create_profile">User Profile</a>'
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    71
    ' in order to view this page.')
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 965
diff changeset
    73
DEF_AGREE_TO_TOS_MSG_FMT = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    74
    'You must agree to the <a href="%(tos_link)s">site-wide Terms of'
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    75
    ' Service</a> in your <a href="/user/edit_profile">User Profile</a>'
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    76
    ' in order to view this page.')
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
    77
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 965
diff changeset
    78
DEF_DEV_LOGOUT_LOGIN_MSG_FMT = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    79
    'Please <a href="%%(sign_out)s">sign out</a>'
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    80
    ' and <a href="%%(sign_in)s">sign in</a>'
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    81
    ' again as %(role)s to view this page.')
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
    83
DEF_NEED_MEMBERSHIP_MSG_FMT = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    84
    'You need to be in the %(status)s group to %(action)s'
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    85
    ' documents in the %(prefix)s prefix.')
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
    86
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    87
DEF_NEED_ROLE_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    88
    'You do not have the required role.')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    89
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    90
DEF_NOT_YOUR_ENTITY_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    91
    'This entity does not belong to you.')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    92
1435
e9a2b1e87b1a Added support for having a format string as scope_path filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1388
diff changeset
    93
DEF_NO_ACTIVE_ENTITY_MSG = ugettext(
e9a2b1e87b1a Added support for having a format string as scope_path filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1388
diff changeset
    94
    'There is no such active entity.')
e9a2b1e87b1a Added support for having a format string as scope_path filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1388
diff changeset
    95
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
    96
DEF_NO_ACTIVE_GROUP_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    97
    'There is no such active group.')
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    98
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
    99
DEF_NO_ACTIVE_ROLE_MSG = ugettext(
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   100
    'There is no such active role.')
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   101
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
   102
DEF_ALREADY_PARTICIPATING_MSG = ugettext(
1832
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
   103
    'You cannot become a Student because you are already participating '
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
   104
    'in this program.')
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
   105
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
   106
DEF_ALREADY_STUDENT_ROLE_MSG = ugettext(
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
   107
    'You cannot become a Mentor or Organization Admin because you already are '
1832
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
   108
    'a Student in this program.')
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
   109
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   110
DEF_NO_ACTIVE_PROGRAM_MSG = ugettext(
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   111
    'There is no such active program.')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   112
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   113
DEF_NO_REQUEST_MSG = ugettext(
1870
ea7019607df2 Verbiage fix in ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1832
diff changeset
   114
    'There is no accepted request that would allow you to visit this page. '
ea7019607df2 Verbiage fix in ACL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1832
diff changeset
   115
    'Perhaps you already accepted this request?')
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   116
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   117
DEF_NO_APPLICATION_MSG = ugettext(
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   118
    'There is no application that would allow you to visit this page.')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   119
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   120
DEF_NEED_PICK_ARGS_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   121
    'The "continue" and "field" args are not both present.')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   122
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   123
DEF_REVIEW_COMPLETED_MSG = ugettext('This Application can not be reviewed '
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   124
    'anymore (it has been completed or rejected).')
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   125
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   126
DEF_REQUEST_COMPLETED_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   127
    'This request cannot be accepted (it is either completed or denied).')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   128
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   129
DEF_REQUEST_NOT_ACCEPTED_MSG = ugettext(
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   130
    'This request has not been accepted by the group (it can also be completed already).')
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   131
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   132
DEF_SCOPE_INACTIVE_MSG = ugettext(
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   133
    'The scope for this request is not active.')
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   134
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
   135
DEF_SIGN_UP_AS_STUDENT_MSG = ugettext(
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
   136
    'You need to sign up as a Student first.')
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
   137
1995
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
   138
DEF_MAX_PROPOSALS_REACHED = ugettext(
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
   139
    'You have reached the maximum number of Proposals allowed '
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
   140
    'for this program.')
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
   141
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   142
DEF_NO_LIST_ACCESS_MSG = ugettext('You do not have the required rights to '
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   143
    'list documents for this scope and prefix.')
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
   144
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 965
diff changeset
   145
DEF_PAGE_DENIED_MSG = ugettext(
1350
c822368a60b1 Add missing dots in messages in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1334
diff changeset
   146
    'Access to this page has been restricted.')
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   147
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
   148
DEF_PREFIX_NOT_IN_ARGS_MSG = ugettext(
1350
c822368a60b1 Add missing dots in messages in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1334
diff changeset
   149
    'A required GET url argument ("prefix") was not specified.')
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
   150
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   151
DEF_PAGE_INACTIVE_MSG = ugettext(
1350
c822368a60b1 Add missing dots in messages in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1334
diff changeset
   152
    'This page is inactive at this time.')
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
   153
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 965
diff changeset
   154
DEF_LOGOUT_MSG_FMT = ugettext(
1350
c822368a60b1 Add missing dots in messages in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1334
diff changeset
   155
    'Please <a href="%(sign_out)s">sign out</a> in order to view this page.')
590
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   156
979
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   157
DEF_GROUP_NOT_FOUND_MSG = ugettext(
1350
c822368a60b1 Add missing dots in messages in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1334
diff changeset
   158
    'The requested Group can not be found.')
979
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   159
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   160
DEF_NOT_ALLOWED_PROJECT_FOR_SURVEY_MSG = ugettext(
2538
c82fbf7fbad0 Style fix in access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2536
diff changeset
   161
    'You are not allowed to take this Survey for the specified Student'
c82fbf7fbad0 Style fix in access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2536
diff changeset
   162
    ' Project.')
2523
9d9d1ded30ee addressed comments to previous version
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2522
diff changeset
   163
2734
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
   164
DEF_NO_VALID_RECORD_ID = ugettext('No valid numeric record ID given.')
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
   165
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
   166
DEF_NOT_YOUR_RECORD = ugettext(
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
   167
    'This is not your Survey Record. If you feel you should have access to '
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
   168
    'this page please notify the administrators.')
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
   169
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   170
DEF_USER_ACCOUNT_INVALID_MSG_FMT = ugettext(
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   171
    'The <b><i>%(email)s</i></b> account cannot be used with this site, for'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   172
    ' one or more of the following reasons:'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   173
    '<ul>'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   174
    ' <li>the account is invalid</li>'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   175
    ' <li>the account is already attached to a User profile and cannot be'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   176
    ' used to create another one</li>'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   177
    ' <li>the account is a former account that cannot be used again</li>'
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   178
    '</ul>')
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   179
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   180
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   181
class Error(Exception):
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   182
  """Base class for all exceptions raised by this module.
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   183
  """
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   184
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   185
  pass
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   186
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   187
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   188
class InvalidArgumentError(Error):
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   189
  """Raised when an invalid argument is passed to a method.
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   190
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   191
  For example, if an argument is None, but must always be non-False.
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   192
  """
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   193
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   194
  pass
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   195
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
   196
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   197
def allowSidebar(fun):
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   198
  """Decorator that allows access if the sidebar is calling.
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   199
  """
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   200
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   201
  from functools import wraps
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   202
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   203
  @wraps(fun)
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   204
  def wrapper(self, django_args, *args, **kwargs):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   205
    """Decorator wrapper method.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   206
    """
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   207
    if django_args.get('SIDEBAR_CALLING'):
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   208
      return
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   209
    return fun(self, django_args, *args, **kwargs)
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   210
  return wrapper
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   211
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
   212
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   213
def denySidebar(fun):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   214
  """Decorator that denies access if the sidebar is calling.
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   215
  """
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   216
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   217
  from functools import wraps
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   218
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   219
  @wraps(fun)
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   220
  def wrapper(self, django_args, *args, **kwargs):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   221
    """Decorator wrapper method.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   222
    """
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   223
    if django_args.get('SIDEBAR_CALLING'):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   224
      raise out_of_band.Error("Sidebar Calling")
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   225
    return fun(self, django_args, *args, **kwargs)
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   226
  return wrapper
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   227
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   228
1073
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   229
def allowIfCheckPasses(checker_name):
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   230
  """Returns a decorator that allows access if the specified checker passes.
1016
15a2f644725f Create a decorator for allowDeveloper
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1012
diff changeset
   231
  """
15a2f644725f Create a decorator for allowDeveloper
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1012
diff changeset
   232
15a2f644725f Create a decorator for allowDeveloper
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1012
diff changeset
   233
  from functools import wraps
15a2f644725f Create a decorator for allowDeveloper
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1012
diff changeset
   234
1073
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   235
  def decorator(fun):
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   236
    """Decorator that allows access if the current user is a Developer.
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   237
    """
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   238
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   239
    @wraps(fun)
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   240
    def wrapper(self, django_args=None, *args, **kwargs):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   241
      """Decorator wrapper method.
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   242
      """
1073
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   243
      try:
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   244
        # if the check passes we allow access regardless
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   245
        return self.doCheck(checker_name, django_args, [])
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   246
      except out_of_band.Error:
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   247
        # otherwise we run the original check
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   248
        return fun(self, django_args, *args, **kwargs)
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   249
    return wrapper
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   250
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   251
  return decorator
feea88d0e1d8 Factor out the allowIfCheckPasses logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1068
diff changeset
   252
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   253
# pylint: disable-msg=C0103
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   254
allowDeveloper = allowIfCheckPasses('checkIsDeveloper') 
1016
15a2f644725f Create a decorator for allowDeveloper
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1012
diff changeset
   255
15a2f644725f Create a decorator for allowDeveloper
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1012
diff changeset
   256
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   257
class Checker(object):
590
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   258
  """
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   259
  The __setitem__() and __getitem__() methods are overloaded to DTRT
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   260
  when adding new access rights, and retrieving them, so use these
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   261
  rather then modifying rights directly if so desired.
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   262
  """
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   263
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   264
  MEMBERSHIP = {
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   265
    'anyone': 'allow',
1248
f318538394d9 Rename checkHasActiveRole to checkHasActiveRoleForScope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1239
diff changeset
   266
    'club_admin': ('checkHasActiveRoleForScope', club_admin_logic),
f318538394d9 Rename checkHasActiveRole to checkHasActiveRoleForScope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1239
diff changeset
   267
    'club_member': ('checkHasActiveRoleForScope', club_member_logic),
1444
b97cfeb423f4 Make use of the new checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1443
diff changeset
   268
    'host': ('checkHasDocumentAccess', [host_logic, 'sponsor']),
b97cfeb423f4 Make use of the new checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1443
diff changeset
   269
    'org_admin': ('checkHasDocumentAccess', [org_admin_logic, 'org']),
b97cfeb423f4 Make use of the new checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1443
diff changeset
   270
    'org_mentor': ('checkHasDocumentAccess', [mentor_logic, 'org']),
b97cfeb423f4 Make use of the new checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1443
diff changeset
   271
    'org_student': ('checkHasDocumentAccess', [student_logic, 'org']),
2957
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   272
    'ghop_org_admin': ('checkHasDocumentAccess', [ghop_org_admin_logic, 'org']),
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   273
    'ghop_org_mentor': ('checkHasDocumentAccess', [ghop_mentor_logic, 'org']),
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   274
    'ghop_org_student': ('checkHasDocumentAccess', [ghop_student_logic, 'org']),
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   275
    'user': 'checkIsUser',
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   276
    'user_self': ('checkIsUserSelf', 'scope_path'),
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   277
    }
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   278
1442
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   279
  #: the depths of various scopes to other scopes
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   280
  # the 0 entries are not used, and are for clarity purposes only
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   281
  SCOPE_DEPTH = {
1528
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1525
diff changeset
   282
      'site': None,
1442
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   283
      'sponsor': (sponsor_logic, {'sponsor': 0}),
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   284
      'program': (program_logic, {'sponsor': 1, 'program': 0}),
2957
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   285
      'ghop_program': (
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   286
          ghop_program_logic, {'sponsor': 1, 'ghop_program': 0}),
1442
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   287
      'org': (org_logic, {'sponsor': 2, 'program': 1, 'org': 0}),
2957
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   288
      'ghop_org': (
f6515daaf000 Enable document system for GHOP
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2956
diff changeset
   289
          ghop_org_logic, {'sponsor': 2, 'ghop_program': 1, 'ghop_org': 0}),
1442
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   290
      }
8eec34007e80 Specify how to walk the scope up to another scope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1441
diff changeset
   291
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   292
  def __init__(self, params):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   293
    """Adopts base.rights as rights if base is set.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   294
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   295
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   296
    base = params.get('rights') if params else None
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   297
    self.rights = base.rights if base else {}
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   298
    self.id = None
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   299
    self.user = None
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   300
1226
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   301
  def normalizeChecker(self, checker):
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   302
    """Normalizes the checker to a pre-defined format.
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   303
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   304
    The result is guaranteed to be a list of 2-tuples, the first element is a
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   305
    checker (iff there is an checker with the specified name), the second
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   306
    element is a list of arguments that should be passed to the checker when
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   307
    calling it in addition to the standard django_args.
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   308
    """
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   309
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   310
    # Be nice an repack so that it is always a list with tuples
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   311
    if isinstance(checker, tuple):
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   312
      name, arg = checker
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   313
      return (name, (arg if isinstance(arg, list) else [arg]))
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   314
    else:
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   315
      return (checker, [])
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   316
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   317
  def __setitem__(self, key, value):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   318
    """Sets a value only if no old value exists.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   319
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   320
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   321
    oldvalue = self.rights.get(key)
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   322
    self.rights[key] = oldvalue if oldvalue else value
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   323
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   324
  def __getitem__(self, key):
1226
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   325
    """Retrieves and normalizes the right checkers.
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   326
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   327
1226
a671f0d63562 Fix broken document access checks
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1223
diff changeset
   328
    return [self.normalizeChecker(i) for i in self.rights.get(key, [])]
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   329
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   330
  def key(self, checker_name):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   331
    """Returns the key for the specified checker for the current user.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   332
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   333
2836
0181649aa7b0 Use per-request store to store access rights
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2792
diff changeset
   334
    return "checker.%s.%s" % (self.id, checker_name)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   335
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   336
  def put(self, checker_name, value):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   337
    """Puts the result for the specified checker in the cache.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   338
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   339
2836
0181649aa7b0 Use per-request store to store access rights
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2792
diff changeset
   340
    cache_key = self.key(checker_name)
2850
c935cb48f757 Do not store core in Checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2836
diff changeset
   341
    callback.getCore().setRequestValue(cache_key, value)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   342
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   343
  def get(self, checker_name):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   344
    """Retrieves the result for the specified checker from cache.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   345
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   346
2836
0181649aa7b0 Use per-request store to store access rights
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2792
diff changeset
   347
    cache_key = self.key(checker_name)
2850
c935cb48f757 Do not store core in Checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2836
diff changeset
   348
    return callback.getCore().getRequestValue(cache_key)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   349
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   350
  def doCheck(self, checker_name, django_args, args):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   351
    """Runs the specified checker with the specified arguments.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   352
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   353
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   354
    checker = getattr(self, checker_name)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   355
    checker(django_args, *args)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   356
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   357
  def doCachedCheck(self, checker_name, django_args, args):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   358
    """Retrieves from cache or runs the specified checker.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   359
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   360
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   361
    cached = self.get(checker_name)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   362
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   363
    if cached is None:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   364
      try:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   365
        self.doCheck(checker_name, django_args, args)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   366
        self.put(checker_name, True)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   367
        return
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   368
      except out_of_band.Error, exception:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   369
        self.put(checker_name, exception)
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   370
        raise
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   371
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   372
    if cached is True:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   373
      return
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   374
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   375
    # re-raise the cached exception
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   376
    raise cached
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   377
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   378
  def check(self, use_cache, checker_name, django_args, args):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   379
    """Runs the checker, optionally using the cache.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   380
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   381
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   382
    if use_cache:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   383
      self.doCachedCheck(checker_name, django_args, args)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   384
    else:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   385
      self.doCheck(checker_name, django_args, args)
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   386
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   387
  def setCurrentUser(self, id, user):
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   388
    """Sets up everything for the current user.
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   389
    """
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   390
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   391
    self.id = id
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   392
    self.user = user
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   393
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   394
  def checkAccess(self, access_type, django_args):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   395
    """Runs all the defined checks for the specified type.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   396
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   397
    Args:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   398
      access_type: the type of request (such as 'list' or 'edit')
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   399
      rights: a dictionary containing access check functions
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   400
      django_args: a dictionary with django's arguments
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   401
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   402
    Rights usage:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   403
      The rights dictionary is used to check if the current user is allowed
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   404
      to view the page specified. The functions defined in this dictionary
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   405
      are always called with the provided django_args dictionary as argument. On any
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   406
      request, regardless of what type, the functions in the 'any_access' value
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   407
      are called. If the specified type is not in the rights dictionary, all
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   408
      the functions in the 'unspecified' value are called. When the specified
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   409
      type _is_ in the rights dictionary, all the functions in that access_type's
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   410
      value are called.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   411
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   412
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   413
    use_cache = django_args.get('SIDEBAR_CALLING')
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   414
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   415
    # Call each access checker
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   416
    for checker_name, args in self['any_access']:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   417
      self.check(use_cache, checker_name, django_args, args)
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   418
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   419
    if access_type not in self.rights:
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   420
      # No checks defined, so do the 'generic' checks and bail out
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   421
      for checker_name, args in self['unspecified']:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   422
        self.check(use_cache, checker_name, django_args, args)
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   423
      return
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   424
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   425
    for checker_name, args in self[access_type]:
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   426
      self.check(use_cache, checker_name, django_args, args)
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   427
1300
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   428
  def hasMembership(self, roles, django_args):
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   429
    """Checks whether the user has access to any of the specified roles.
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   430
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   431
    Makes use of self.MEMBERSHIP, which defines checkers specific to
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   432
    document access, as such this method should only be used when checking
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   433
    document access.
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   434
1300
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   435
    Args:
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   436
      roles: a list of roles to check
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   437
      django_args: the django args that should be passed to doCheck
1300
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   438
    """
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   439
1315
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   440
    try:
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   441
      # we need to check manually, as we must return True!
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   442
      self.checkIsDeveloper(django_args)
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   443
      return True
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   444
    except out_of_band.Error:
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   445
      pass
7c58f5cdd5b8 Developers have all roles
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1309
diff changeset
   446
1300
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   447
    for role in roles:
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   448
      try:
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   449
        checker_name, args = self.normalizeChecker(self.MEMBERSHIP[role])
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   450
        self.doCheck(checker_name, django_args, args)
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   451
        # the check passed, we can stop now
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   452
        return True
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   453
      except out_of_band.Error:
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   454
        continue
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   455
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   456
    return False
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   457
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   458
  @allowDeveloper
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   459
  def checkMembership(self, action, prefix, status, django_args):
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   460
    """Checks whether the user has access to the specified status.
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   461
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   462
    Args:
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   463
      action: the action that was performed (e.g., 'read')
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   464
      prefix: the prefix, determines what access set is used
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   465
      status: the access status (e.g., 'public')
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   466
      django_args: the django args to pass on to the checkers
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   467
    """
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   468
2956
50ce8ac13932 Use the new rights code in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2936
diff changeset
   469
    checker = callback.getCore().getRightsChecker(prefix)
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   470
    roles = checker.getMembership(status)
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   471
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   472
    message_fmt = DEF_NEED_MEMBERSHIP_MSG_FMT % {
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   473
        'action': action,
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   474
        'prefix': prefix,
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   475
        'status': status,
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   476
        }
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   477
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   478
    # try to see if they belong to any of the roles, if not, raise an
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   479
    # access violation for the specified action, prefix and status.
1300
a89d673771eb Factor out the hasMembership method from checkMembership
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1265
diff changeset
   480
    if not self.hasMembership(roles, django_args):
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   481
      raise out_of_band.AccessViolation(message_fmt)
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   482
1486
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   483
  def checkHasAny(self, django_args, checks):
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   484
    """Checks if any of the checks passes.
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   485
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   486
    If none of the specified checks passes, the exception that the first of the
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   487
    checks raised is reraised.
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   488
    """
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   489
2101
d6250eac3ab0 Fixed bug introduced by fixing a pylint error
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2077
diff changeset
   490
    first = None
1486
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   491
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   492
    for checker_name, args in checks:
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   493
      try:
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   494
        self.doCheck(checker_name, django_args, args)
1488
4b7cec48e26c Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1486
diff changeset
   495
        # one check passed, all is well
4b7cec48e26c Brown paper bag fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1486
diff changeset
   496
        return
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   497
      except out_of_band.Error, exception:
2717
fdde098394a7 Fixed typo in access module comment and StudentProject list description.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2710
diff changeset
   498
        # store the first exception
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   499
        first = first if first else exception
1486
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   500
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   501
    # none passed, re-raise the first exception
2116
68d7679a2af2 Add pylint: disable-msg=W0706 to soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2112
diff changeset
   502
    # pylint: disable-msg=W0706
1486
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   503
    raise first
c417a4188e73 Added a checkHasAny method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1481
diff changeset
   504
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   505
  def allow(self, django_args):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   506
    """Never raises an alternate HTTP response.  (an access no-op, basically).
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   507
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   508
    Args:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   509
      django_args: a dictionary with django's arguments
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   510
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   511
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   512
    return
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   513
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   514
  def deny(self, django_args=None):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   515
    """Always raises an alternate HTTP response.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   516
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   517
    Args:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   518
      django_args: a dictionary with django's arguments
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   519
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   520
    Raises:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   521
      always raises AccessViolationResponse if called
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   522
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   523
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   524
    context = django_args.get('context', {})
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   525
    context['title'] = 'Access denied'
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   526
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   527
    raise out_of_band.AccessViolation(DEF_PAGE_DENIED_MSG, context=context)
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   528
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   529
  def checkIsLoggedIn(self, django_args=None):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   530
    """Raises an alternate HTTP response if Google Account is not logged in.
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   531
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   532
    Args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   533
      django_args: a dictionary with django's arguments, not used
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   534
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   535
    Raises:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   536
      AccessViolationResponse:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   537
      * if no Google Account is even logged in
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   538
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   539
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   540
    if self.id:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   541
      return
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   542
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   543
    raise out_of_band.LoginRequest()
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   544
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   545
  def checkNotLoggedIn(self, django_args=None):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   546
    """Raises an alternate HTTP response if Google Account is logged in.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   547
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   548
    Args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   549
      django_args: a dictionary with django's arguments, not used
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   550
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   551
    Raises:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   552
      AccessViolationResponse:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   553
      * if a Google Account is currently logged in
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   554
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   555
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   556
    if not self.id:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   557
      return
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   558
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   559
    raise out_of_band.LoginRequest(message_fmt=DEF_LOGOUT_MSG_FMT)
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   560
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   561
  def checkIsUser(self, django_args=None):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   562
    """Raises an alternate HTTP response if Google Account has no User entity.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   563
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   564
    Args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   565
      django_args: a dictionary with django's arguments, not used
895
e70ffd079438 Even developers need to agree to the terms of service for Melange
Sverre Rabbelier <srabbelier@gmail.com>
parents: 892
diff changeset
   566
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   567
    Raises:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   568
      AccessViolationResponse:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   569
      * if no User exists for the logged-in Google Account, or
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   570
      * if no Google Account is logged in at all
1012
73f0b61f2d9d Fold checkAgreesToSiteToS into checkIsUser
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
   571
      * if User has not agreed to the site-wide ToS, if one exists
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   572
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   573
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   574
    self.checkIsLoggedIn()
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   575
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   576
    if not self.user:
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   577
      raise out_of_band.LoginRequest(message_fmt=DEF_NO_USER_LOGIN_MSG)
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   578
1017
6ad4fdb48840 Cache access checks and disable sidebar caching
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1016
diff changeset
   579
    if user_logic.agreesToSiteToS(self.user):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   580
      return
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   581
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   582
    # Would not reach this point of site-wide ToS did not exist, since
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   583
    # agreesToSiteToS() call above always returns True if no ToS is in effect.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   584
    login_msg_fmt = DEF_AGREE_TO_TOS_MSG_FMT % {
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   585
        'tos_link': redirects.getToSRedirect(site_logic.getSingleton())}
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   586
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   587
    raise out_of_band.LoginRequest(message_fmt=login_msg_fmt)
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   588
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   589
  @allowDeveloper
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   590
  def checkIsHost(self, django_args=None):
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   591
    """Checks whether the current user has a role entity.
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   592
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   593
    Args:
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   594
      django_args: the keyword args from django, not used
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   595
    """
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   596
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   597
    if not django_args:
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   598
      django_args = {}
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   599
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   600
    return self.checkHasActiveRole(django_args, host_logic)
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   601
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   602
  @allowDeveloper
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   603
  def checkIsUserSelf(self, django_args, field_name):
1142
da2487767ef4 Fix missing dot and wrong import sorting in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1135
diff changeset
   604
    """Checks whether the specified user is the logged in user.
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   605
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   606
    Args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   607
      django_args: the keyword args from django, only field_name is used
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   608
    """
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   609
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   610
    self.checkIsUser()
1198
3318f8d00691 Added some extra isUser checks to make sure that you have to accept the ToS before you can do anything concerning requests.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
   611
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   612
    if not field_name in django_args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   613
      self.deny()
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   614
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   615
    if self.user.link_id == django_args[field_name]:
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   616
      return
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   617
1177
53c802c2a2e2 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1176
diff changeset
   618
    raise out_of_band.AccessViolation(DEF_NOT_YOUR_ENTITY_MSG)
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   619
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   620
  def checkIsUnusedAccount(self, django_args=None):
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   621
    """Raises an alternate HTTP response if Google Account has a User entity.
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   622
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   623
    Args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   624
      django_args: a dictionary with django's arguments, not used
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   625
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   626
    Raises:
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   627
      AccessViolationResponse:
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   628
      * if a User exists for the logged-in Google Account, or
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   629
      * if a User has this Gooogle Account in their formerAccounts list
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   630
    """
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   631
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   632
    self.checkIsLoggedIn()
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   633
1716
a387bb7a39d4 Fixes banned people being able to create a new user entity.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1633
diff changeset
   634
    fields = {'account': self.id}
a387bb7a39d4 Fixes banned people being able to create a new user entity.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1633
diff changeset
   635
    user_entity = user_logic.getForFields(fields, unique=True)
a387bb7a39d4 Fixes banned people being able to create a new user entity.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1633
diff changeset
   636
a387bb7a39d4 Fixes banned people being able to create a new user entity.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1633
diff changeset
   637
    if not user_entity and not user_logic.isFormerAccount(self.id):
1048
0fe0cb8f7253 Changed access.py to comply more with the style of the module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1043
diff changeset
   638
      # this account has not been used yet
0fe0cb8f7253 Changed access.py to comply more with the style of the module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1043
diff changeset
   639
      return
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   640
1048
0fe0cb8f7253 Changed access.py to comply more with the style of the module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1043
diff changeset
   641
    message_fmt = DEF_USER_ACCOUNT_INVALID_MSG_FMT % {
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   642
        'email' : self.id.email()
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   643
        }
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   644
1048
0fe0cb8f7253 Changed access.py to comply more with the style of the module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1043
diff changeset
   645
    raise out_of_band.LoginRequest(message_fmt=message_fmt)
0fe0cb8f7253 Changed access.py to comply more with the style of the module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1043
diff changeset
   646
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   647
  def checkHasUserEntity(self, django_args=None):
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   648
    """Raises an alternate HTTP response if Google Account has no User entity.
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   649
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   650
    Args:
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   651
      django_args: a dictionary with django's arguments
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   652
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   653
    Raises:
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   654
      AccessViolationResponse:
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   655
      * if no User exists for the logged-in Google Account, or
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   656
      * if no Google Account is logged in at all
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   657
    """
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   658
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   659
    self.checkIsLoggedIn()
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   660
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   661
    if self.user:
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   662
      return
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   663
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   664
    raise out_of_band.LoginRequest(message_fmt=DEF_NO_USER_LOGIN_MSG)
1043
5e15994b2033 Redone the user's profile page.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1037
diff changeset
   665
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   666
  def checkIsDeveloper(self, django_args=None):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   667
    """Raises an alternate HTTP response if Google Account is not a Developer.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   668
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   669
    Args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
   670
      django_args: a dictionary with django's arguments, not used
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   671
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   672
    Raises:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   673
      AccessViolationResponse:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   674
      * if User is not a Developer, or
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   675
      * if no User exists for the logged-in Google Account, or
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   676
      * if no Google Account is logged in at all
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   677
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   678
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   679
    self.checkIsUser()
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   680
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   681
    if user_logic.isDeveloper(account=self.id, user=self.user):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   682
      return
979
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   683
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   684
    login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   685
        'role': 'a Site Developer ',
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   686
        }
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   687
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   688
    raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   689
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   690
  @allowDeveloper
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   691
  @denySidebar
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   692
  def _checkIsActive(self, django_args, logic, fields):
1435
e9a2b1e87b1a Added support for having a format string as scope_path filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1388
diff changeset
   693
    """Raises an alternate HTTP response if the entity is not active.
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   694
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   695
    Args:
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   696
      django_args: a dictionary with django's arguments
1435
e9a2b1e87b1a Added support for having a format string as scope_path filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1388
diff changeset
   697
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   698
      fields: the name of the fields that should be copied verbatim
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   699
              from the django_args as filter
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   700
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   701
    Raises:
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   702
      AccessViolationResponse:
1438
e484f9acf999 Updated comments in access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1435
diff changeset
   703
      * if no entity is found
e484f9acf999 Updated comments in access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1435
diff changeset
   704
      * if the entity status is not active
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   705
    """
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   706
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   707
    self.checkIsUser()
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   708
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   709
    fields = dicts.filter(django_args, fields)
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   710
    fields['status'] = 'active'
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   711
1179
427d2ec42823 Rewrite getForFields to use GQL instead of the Query API
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1177
diff changeset
   712
    entity = logic.getForFields(fields, unique=True)
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   713
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   714
    if entity:
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   715
      return entity
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   716
1435
e9a2b1e87b1a Added support for having a format string as scope_path filter
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1388
diff changeset
   717
    raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_ENTITY_MSG)
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   718
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   719
  def checkGroupIsActiveForScopeAndLinkId(self, django_args, logic):
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   720
    """Checks that the specified group is active.
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   721
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   722
    Only group where both the link_id and the scope_path match the value
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   723
    of the link_id and the scope_path from the django_args are considered.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   724
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   725
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   726
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   727
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   728
    """
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   729
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   730
    fields = ['scope_path', 'link_id']
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   731
    return self._checkIsActive(django_args, logic, fields)
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   732
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   733
  def checkGroupIsActiveForLinkId(self, django_args, logic):
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   734
    """Checks that the specified group is active.
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   735
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   736
    Only group where the link_id matches the value of the link_id
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   737
    from the django_args are considered.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   738
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   739
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   740
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   741
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   742
    """
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   743
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   744
    return self._checkIsActive(django_args, logic, ['link_id'])
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   745
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   746
  def checkHasActiveRole(self, django_args, logic):
1203
38225f2ad3a6 Renamed checkHasRole to checkHasActiveRole.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1201
diff changeset
   747
    """Checks that the user has the specified active role.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   748
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   749
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   750
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   751
      logic: the logic that should be used to look up the entity
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   752
    """
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   753
1555
cba45797f4b9 Do not modify django_args directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1554
diff changeset
   754
    django_args = django_args.copy()
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   755
    django_args['user'] = self.user
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   756
    return self._checkIsActive(django_args, logic, ['user'])
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   757
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   758
  def _checkHasActiveRoleFor(self, django_args, logic, field_name):
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   759
    """Checks that the user has the specified active role.
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   760
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   761
    Only roles where the field as specified by field_name matches the
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   762
    scope_path from the django_args are considered.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   763
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   764
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   765
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   766
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   767
    """
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   768
1554
163c879d1ffe Fixed a brown paper bag bug in _checkHasActiveRoleFor
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1538
diff changeset
   769
    fields = [field_name, 'user']
1555
cba45797f4b9 Do not modify django_args directly
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1554
diff changeset
   770
    django_args = django_args.copy()
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   771
    django_args['user'] = self.user
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   772
    return self._checkIsActive(django_args, logic, fields)
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   773
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   774
  def checkHasActiveRoleForKeyFieldsAsScope(self, django_args, logic):
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   775
    """Checks that the user has the specified active role.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   776
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   777
    Args:
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   778
      django_args: a dictionary with django's arguments
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
   779
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   780
    """
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   781
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   782
    key_fields = "%(scope_path)s/%(link_id)s" % django_args
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   783
    new_args = {'scope_path': key_fields}
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   784
    return self._checkHasActiveRoleFor(new_args, logic, 'scope_path')
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   785
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   786
  def checkHasActiveRoleForScope(self, django_args, logic):
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   787
    """Checks that the user has the specified active role.
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   788
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   789
    Only roles where the scope_path matches the scope_path from the
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   790
    django_args are considered.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   791
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   792
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   793
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   794
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   795
    """
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   796
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   797
    return self._checkHasActiveRoleFor(django_args, logic, 'scope_path')
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   798
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   799
  def checkHasActiveRoleForLinkId(self, django_args, logic):
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   800
    """Checks that the user has the specified active role.
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   801
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   802
    Only roles where the link_id matches the link_id from the
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   803
    django_args are considered.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   804
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   805
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   806
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   807
      logic: the logic that should be used to look up the entity
1525
fe906cdbf0e9 Remove direct uses of checkIsActive, replace with helper methods
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1524
diff changeset
   808
    """
1557
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   809
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   810
    return self._checkHasActiveRoleFor(django_args, logic, 'link_id')
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   811
1557
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   812
  def checkHasActiveRoleForLinkIdAsScope(self, django_args, logic):
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   813
    """Checks that the user has the specified active role.
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   814
1558
5ff25327371e Fixed a comment typo
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1557
diff changeset
   815
    Only roles where the scope_path matches the link_id from the
1557
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   816
    django_args are considered.
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   817
    
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   818
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   819
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   820
      logic: the logic that should be used to look up the entity
1557
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   821
    """
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   822
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   823
    django_args = django_args.copy()
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   824
    django_args['scope_path'] = django_args['link_id']
2522
b97b3c8e5736 return found entity for activity checks
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2521
diff changeset
   825
    return self._checkHasActiveRoleFor(django_args, logic, 'scope_path')
1557
9e32e6258319 Properly fixed host access now
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1555
diff changeset
   826
1443
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   827
  def checkHasDocumentAccess(self, django_args, logic, target_scope):
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   828
    """Checks that the user has access to the specified document scope.
3032
f3886d1b00a5 Changed checkCanMakeRequestToGroup to use Logic instance instead of module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3029
diff changeset
   829
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   830
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   831
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   832
      logic: the logic that should be used to look up the entity
1443
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   833
    """
3032
f3886d1b00a5 Changed checkCanMakeRequestToGroup to use Logic instance instead of module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3029
diff changeset
   834
1443
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   835
    prefix = django_args['prefix']
1528
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1525
diff changeset
   836
    if self.SCOPE_DEPTH.get(prefix):
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1525
diff changeset
   837
      scope_logic, depths = self.SCOPE_DEPTH[prefix]
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1525
diff changeset
   838
    else:
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1525
diff changeset
   839
      return self.checkHasActiveRole(django_args, logic)
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1525
diff changeset
   840
1443
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   841
    depth = depths.get(target_scope, 0)
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   842
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   843
    # nothing to do
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   844
    if not (scope_logic and depth):
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   845
      return self.checkHasActiveRoleForScope(django_args, logic)
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   846
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   847
    # we don't want to modify the original django args
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   848
    django_args = django_args.copy()
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   849
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   850
    entity = scope_logic.getFromKeyName(django_args['scope_path'])
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   851
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   852
    # cannot have access to the specified scope if it is invalid
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   853
    if not entity:
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   854
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_ENTITY_MSG)
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   855
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   856
    # walk up the scope to where we need to be
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   857
    for _ in range(depth):
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   858
      entity = entity.scope
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   859
2160
3f9dd37d98a8 Use key().id_or_name() instead of key().name()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2145
diff changeset
   860
    django_args['scope_path'] = entity.key().id_or_name()
1443
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   861
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   862
    self.checkHasActiveRoleForScope(django_args, logic)
8ce8314d1c8f Added a checkHasDocumentAccess method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1442
diff changeset
   863
1189
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   864
  def checkSeeded(self, django_args, checker_name, *args):
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   865
    """Wrapper to update the django_args with the contens of seed first.
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   866
    """
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   867
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   868
    django_args.update(django_args.get('seed', {}))
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   869
    self.doCheck(checker_name, django_args, args)
14357ec13647 More access related fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1184
diff changeset
   870
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   871
  def checkCanMakeRequestToGroup(self, django_args, group_logic):
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   872
    """Raises an alternate HTTP response if the specified group is not in an
1085
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1080
diff changeset
   873
    active status.
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   874
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   875
    Args:
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   876
      django_args: a dictionary with django's arguments
3032
f3886d1b00a5 Changed checkCanMakeRequestToGroup to use Logic instance instead of module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3029
diff changeset
   877
      group_logic: Logic instance for the group which the request is for
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   878
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   879
1198
3318f8d00691 Added some extra isUser checks to make sure that you have to accept the ToS before you can do anything concerning requests.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
   880
    self.checkIsUser(django_args)
3318f8d00691 Added some extra isUser checks to make sure that you have to accept the ToS before you can do anything concerning requests.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
   881
3032
f3886d1b00a5 Changed checkCanMakeRequestToGroup to use Logic instance instead of module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3029
diff changeset
   882
    group_entity = group_logic.getFromKeyName(django_args['scope_path'])
979
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   883
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   884
    if not group_entity:
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   885
      raise out_of_band.Error(DEF_GROUP_NOT_FOUND_MSG, status=404)
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   886
1085
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1080
diff changeset
   887
    if group_entity.status != 'active':
1198
3318f8d00691 Added some extra isUser checks to make sure that you have to accept the ToS before you can do anything concerning requests.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
   888
      # tell the user that this group is not active
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   889
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_GROUP_MSG)
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   890
979
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   891
    return
789e70941055 Added checkCanMakeRequestToGroup to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 974
diff changeset
   892
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   893
  def checkCanCreateFromRequest(self, django_args):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   894
    """Raises an alternate HTTP response if the specified request does not exist
1085
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1080
diff changeset
   895
       or if it's status is not group_accepted. Also when the group this request
0afbdd0905ef Renamed state to status where appropriate.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1080
diff changeset
   896
       is from is in an inactive or invalid status access will be denied.
3032
f3886d1b00a5 Changed checkCanMakeRequestToGroup to use Logic instance instead of module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3029
diff changeset
   897
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   898
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   899
      django_args: a dictionary with django's arguments
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   900
    """
972
43018f61b481 Remove the request and arg parameter from the checkAccess call
Sverre Rabbelier <srabbelier@gmail.com>
parents: 970
diff changeset
   901
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   902
    self.checkIsUser(django_args)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   903
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   904
    id = int(django_args['id'])
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   905
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   906
    request_entity = request_logic.getFromIDOr404(id)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   907
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   908
    if request_entity.status != 'group_accepted':
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   909
      raise out_of_band.AccessViolation(message_fmt=DEF_REQUEST_NOT_ACCEPTED_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   910
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   911
    if request_entity.group.status in ['invalid', 'inactive']:
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   912
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_GROUP_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   913
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   914
    if request_entity.user.key() != self.user.key():
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   915
      # this request does not belong to the user creating the role
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   916
      raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   917
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   918
    return
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   919
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   920
  def checkIsMyGroupAcceptedRequest(self, django_args):
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   921
    """Checks whether the user can accept the specified request.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   922
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   923
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   924
      django_args: a dictionary with django's arguments
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   925
    """
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   926
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   927
    self.checkIsUser(django_args)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   928
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   929
    id = int(django_args['id'])
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   930
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   931
    request_entity = request_logic.getFromIDOr404(id)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   932
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   933
    if request_entity.user.key() != self.user.key():
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   934
      # this is not the current user's request
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   935
      raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   936
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   937
    if request_entity.status != 'group_accepted':
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   938
      raise out_of_band.AccessViolation(message_fmt=DEF_REQUEST_NOT_ACCEPTED_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   939
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   940
    if request_entity.group.status == 'active':
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   941
      return
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   942
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   943
    raise out_of_band.AccessViolation(message_fmt=DEF_SCOPE_INACTIVE_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   944
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   945
  def checkCanProcessRequest(self, django_args, role_logics):
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   946
    """Raises an alternate HTTP response if the specified request does not exist
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   947
       or if it's status is completed or rejected. Also Raises an alternate HTTP response
1061
09c243461de8 Redone access checks concerning groups to deal with the state property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1048
diff changeset
   948
       whenever the group in the request is not active.
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   949
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   950
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   951
      django_args: a dictionary with django's arguments
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   952
      role_logics: list with Logic instances for roles who can process
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   953
          requests for the group the request is for.
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
   954
    """
948
bd956f419ad9 Add missing blank lines and remove unused checkIsInvited function from soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 943
diff changeset
   955
1198
3318f8d00691 Added some extra isUser checks to make sure that you have to accept the ToS before you can do anything concerning requests.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
   956
    self.checkIsUser(django_args)
3318f8d00691 Added some extra isUser checks to make sure that you have to accept the ToS before you can do anything concerning requests.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1192
diff changeset
   957
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   958
    id = int(django_args['id'])
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   959
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   960
    request_entity = request_logic.getFromIDOr404(id)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   961
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   962
    if request_entity.status in ['completed', 'rejected']:
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
   963
      raise out_of_band.AccessViolation(message_fmt=DEF_REQUEST_COMPLETED_MSG)
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   964
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   965
    if request_entity.group.status != 'active':
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   966
      raise out_of_band.AccessViolation(message_fmt=DEF_SCOPE_INACTIVE_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   967
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   968
    role_fields = {'user': self.user,
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   969
                   'scope': request_entity.group,
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   970
                   'status': 'active'}
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   971
    role_entity = None
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   972
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   973
    for role_logic in role_logics:
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   974
      role_entity = role_logic.getForFields(role_fields, unique=True)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   975
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   976
      if role_entity:
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   977
        break;
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   978
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   979
    if not role_entity:
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   980
      # the current user does not have the necessary role
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   981
      raise out_of_band.AccessViolation(message_fmt=DEF_NEED_ROLE_MSG)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   982
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
   983
    return
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
   984
1218
569a3fe9cb88 Cleaned up getKeyNameFromFields in Logic base.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1203
diff changeset
   985
  @allowDeveloper
1074
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
   986
  @denySidebar
3068
1e01ffe9cc61 checkIsHostForProgram fixes: logic argument has default value and is actually used to look up for a program entity,
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 3067
diff changeset
   987
  def checkIsHostForProgram(self, django_args, logic=program_logic):
1074
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
   988
    """Checks if the user is a host for the specified program.
2610
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
   989
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   990
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
   991
      django_args: a dictionary with django's arguments
3068
1e01ffe9cc61 checkIsHostForProgram fixes: logic argument has default value and is actually used to look up for a program entity,
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 3067
diff changeset
   992
      logic: the logic used to look up for program entity
1074
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
   993
    """
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
   994
3068
1e01ffe9cc61 checkIsHostForProgram fixes: logic argument has default value and is actually used to look up for a program entity,
Daniel Hans <Daniel.M.Hans@gmail.com>
parents: 3067
diff changeset
   995
    program = logic.getFromKeyFields(django_args)
1074
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
   996
1107
a878188e225c Added status to program.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1085
diff changeset
   997
    if not program or program.status == 'invalid':
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
   998
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_PROGRAM_MSG)
1074
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
   999
1107
a878188e225c Added status to program.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1085
diff changeset
  1000
    new_args = {'scope_path': program.scope_path }
1248
f318538394d9 Rename checkHasActiveRole to checkHasActiveRoleForScope
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1239
diff changeset
  1001
    self.checkHasActiveRoleForScope(new_args, host_logic)
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1002
1200
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1003
  @allowDeveloper
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1004
  @denySidebar
1250
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1005
  def checkIsHostForProgramInScope(self, django_args):
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1006
    """Checks if the user is a host for the specified program.
2610
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1007
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1008
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1009
      django_args: a dictionary with django's arguments
1250
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1010
    """
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1011
2610
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1012
    scope_path = django_args.get('scope_path')
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1013
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1014
    if not scope_path:
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1015
      raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_DENIED_MSG)
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1016
95949d4c45d9 Add scope view for GradingSurveyGroup and set access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2583
diff changeset
  1017
    program = program_logic.getFromKeyName(scope_path)
1250
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1018
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1019
    if not program or program.status == 'invalid':
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1020
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_PROGRAM_MSG)
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1021
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1022
    django_args = {'scope_path': program.scope_path}
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1023
    self.checkHasActiveRoleForScope(django_args, host_logic)
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1024
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1025
  @allowDeveloper
b06e6e014658 Fixed access checkers for orgs
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1248
diff changeset
  1026
  @denySidebar
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1027
  def checkIsActivePeriod(self, django_args, period_name, 
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1028
                          key_name_arg, program_logic):
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1029
    """Checks if the given period is active for the given program.
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1030
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1031
    Args:
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1032
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1033
      period_name: the name of the period which is checked
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1034
      key_name_arg: the entry in django_args that specifies the given program
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1035
        keyname. If none is given the key_name is constructed from django_args
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1036
        itself.
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1037
      program_logic: Program Logic instance
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1038
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1039
    Raises:
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1040
      AccessViolationResponse:
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1041
      * if no active Program is found
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1042
      * if the period is not active
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1043
    """
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1044
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1045
    if key_name_arg and key_name_arg in django_args:
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1046
      key_name = django_args[key_name_arg]
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1047
    else:
1334
5009b63c247a Fixed a bug in access.py where a non-existing variable would have been called.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1318
diff changeset
  1048
      key_name = program_logic.getKeyNameFromFields(django_args)
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1049
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1050
    program_entity = program_logic.getFromKeyName(key_name)
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1051
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1052
    if not program_entity or (
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1053
        program_entity.status in ['inactive', 'invalid']):
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1054
      raise out_of_band.AccessViolation(message_fmt=DEF_SCOPE_INACTIVE_MSG)
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1055
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1056
    if timeline_helper.isActivePeriod(program_entity.timeline, period_name):
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1057
      return
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1058
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1059
    raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_INACTIVE_MSG)
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1060
1922
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1061
  @allowDeveloper
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1062
  @denySidebar
2771
eb654b51053b checkIsAfterEvent takes logic as argument instead of hard coded program logic.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2734
diff changeset
  1063
  def checkIsAfterEvent(self, django_args, event_name, 
eb654b51053b checkIsAfterEvent takes logic as argument instead of hard coded program logic.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2734
diff changeset
  1064
                        key_name_arg, program_logic):
1922
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1065
    """Checks if the given event has taken place for the given program.
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1066
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1067
    Args:
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1068
      django_args: a dictionary with django's arguments
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1069
      event_name: the name of the event which is checked
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1070
      key_name_arg: the entry in django_args that specifies the given program
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1071
        keyname. If none is given the key_name is constructed from django_args
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1072
        itself.
2771
eb654b51053b checkIsAfterEvent takes logic as argument instead of hard coded program logic.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2734
diff changeset
  1073
      program_logic: Program Logic instance
1922
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1074
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1075
    Raises:
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1076
      AccessViolationResponse:
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1077
      * if no active Program is found
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1078
      * if the event has not taken place yet
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1079
    """
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1080
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1081
    if key_name_arg and key_name_arg in django_args:
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1082
      key_name = django_args[key_name_arg]
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1083
    else:
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1084
      key_name = program_logic.getKeyNameFromFields(django_args)
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1085
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1086
    program_entity = program_logic.getFromKeyName(key_name)
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1087
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1088
    if not program_entity or (
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1089
        program_entity.status in ['inactive', 'invalid']):
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1090
      raise out_of_band.AccessViolation(message_fmt=DEF_SCOPE_INACTIVE_MSG)
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1091
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1092
    if timeline_helper.isAfterEvent(program_entity.timeline, event_name):
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1093
      return
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1094
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1095
    raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_INACTIVE_MSG)
800ec7253cf4 Added new access method that can check if a given event has taken place already.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1876
diff changeset
  1096
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1097
  def checkCanCreateOrgApp(self, django_args, period_name, program_logic):
1237
b5bf2aa0f3f9 Added missing comment in access.py and removed excessive whitespace.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1232
diff changeset
  1098
    """Checks to see if the program in the scope_path is accepting org apps
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1099
    
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1100
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1101
      django_args: a dictionary with django's arguments
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1102
      period_name: the name of the period which is checked
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1103
      program_logic: Program Logic instance
1237
b5bf2aa0f3f9 Added missing comment in access.py and removed excessive whitespace.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1232
diff changeset
  1104
    """
b5bf2aa0f3f9 Added missing comment in access.py and removed excessive whitespace.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1232
diff changeset
  1105
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1106
    if 'seed' in django_args:
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1107
      return self.checkIsActivePeriod(django_args['seed'],
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1108
          period_name, 'scope_path', program_logic)
1223
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1109
    else:
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1110
      return
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1111
aca77e2cc8f7 Added new access checks to deal with timeline for programs.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1218
diff changeset
  1112
  @allowDeveloper
1200
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1113
  def checkCanEditGroupApp(self, django_args, group_app_logic):
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1114
    """Checks if the group_app in args is valid to be edited by 
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1115
       the current user.
1200
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1116
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1117
    Args:
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1118
      django_args: a dictionary with django's arguments
1200
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1119
      group_app_logic: A logic instance for the Group Application
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1120
    """
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1121
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1122
    self.checkIsUser(django_args)
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1123
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1124
    fields = {
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1125
        'link_id': django_args['link_id'],
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1126
        'applicant': self.user,
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1127
        'status' : ['needs review', 'rejected']
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1128
        }
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1129
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1130
    if 'scope_path' in django_args:
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1131
      fields['scope_path'] = django_args['scope_path']
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1132
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1133
    entity = group_app_logic.getForFields(fields)
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1134
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1135
    if entity:
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1136
      return
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1137
1876
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1138
    del fields['applicant']
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1139
    fields['backup_admin'] = self.user
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1140
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1141
    entity = group_app_logic.getForFields(fields)
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1142
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1143
    if entity:
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1144
      return
a77d4cdcc052 Allow backup admins to edit org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1870
diff changeset
  1145
1200
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1146
    raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1147
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1148
  @allowSidebar
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1149
  def checkCanReviewGroupApp(self, django_args, group_app_logic):
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1150
    """Checks if the group_app in args is valid to be reviewed.
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1151
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1152
    Args:
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1153
      django_args: a dictionary with django's arguments
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1154
      group_app_logic: A logic instance for the Group Application
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1155
    """
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1156
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1157
    if 'link_id' not in django_args:
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1158
      # calling review overview, so we can't check a specified entity
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1159
      return
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1160
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1161
    fields = {
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1162
        'link_id': django_args['link_id'],
1538
b1ab0ec9c950 Changed access checks for group applications to allow reviewing of rejected applications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1529
diff changeset
  1163
        'status': ['needs review', 'accepted', 'rejected', 'ignored',
b1ab0ec9c950 Changed access checks for group applications to allow reviewing of rejected applications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1529
diff changeset
  1164
            'pre-accepted', 'pre-rejected']
1201
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1165
        }
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1166
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1167
    if 'scope_path' in django_args:
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1168
      fields['scope_path'] = django_args['scope_path']
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1169
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1170
    entity = group_app_logic.getForFields(fields)
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1171
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1172
    if entity:
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1173
      return
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1174
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1175
    raise out_of_band.AccessViolation(message_fmt=DEF_REVIEW_COMPLETED_MSG)
0a4c1af700a0 Added checkCanReviewGroupApp to acces.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1200
diff changeset
  1176
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1177
  @allowDeveloper
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1178
  def checkIsApplicationAccepted(self, django_args, app_logic):
1782
501ffd646d1e checkIsApplicationAccepted now properly checks if the application is really from the applying user.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1776
diff changeset
  1179
    """Returns an alternate HTTP response if Google Account has no accepted
501ffd646d1e checkIsApplicationAccepted now properly checks if the application is really from the applying user.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1776
diff changeset
  1180
       Group Application entity for the specified arguments.
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1181
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1182
    Args:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1183
      django_args: a dictionary with django's arguments
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1184
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1185
     Raises:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1186
       AccessViolationResponse: if the required authorization is not met
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1187
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1188
    Returns:
1782
501ffd646d1e checkIsApplicationAccepted now properly checks if the application is really from the applying user.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1776
diff changeset
  1189
      None if the Accepted Group App exists for the specified program, or a subclass
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1190
      of django.http.HttpResponse which contains the alternate response
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1191
      should be returned by the calling view.
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1192
    """
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1193
1012
73f0b61f2d9d Fold checkAgreesToSiteToS into checkIsUser
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
  1194
    self.checkIsUser(django_args)
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
  1195
1782
501ffd646d1e checkIsApplicationAccepted now properly checks if the application is really from the applying user.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1776
diff changeset
  1196
    application = app_logic.getFromKeyFieldsOr404(django_args)
2169
79bb965e723a Allow the backup_admin to fill in the org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2160
diff changeset
  1197
    applicant = application.applicant.key()
2171
83d96aadd228 Do not rely on a backup_admin being set
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2169
diff changeset
  1198
    backup_admin = application.backup_admin
83d96aadd228 Do not rely on a backup_admin being set
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2169
diff changeset
  1199
    backup_admin = backup_admin.key() if backup_admin else None
2169
79bb965e723a Allow the backup_admin to fill in the org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2160
diff changeset
  1200
    user = self.user.key()
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
  1201
1782
501ffd646d1e checkIsApplicationAccepted now properly checks if the application is really from the applying user.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1776
diff changeset
  1202
    # check if the application is accepted and the applicant is the current user
2169
79bb965e723a Allow the backup_admin to fill in the org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2160
diff changeset
  1203
    if application.status == 'accepted' and (applicant == user or
79bb965e723a Allow the backup_admin to fill in the org application
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2160
diff changeset
  1204
                                             backup_admin == user):
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
  1205
      return
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
  1206
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
  1207
    raise out_of_band.AccessViolation(message_fmt=DEF_NO_APPLICATION_MSG)
726
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
  1208
2922
6e373954bbf6 Added GHOPStudent view and access checks.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2918
diff changeset
  1209
  def checkIsNotParticipatingInProgramInScope(self, django_args, program_logic,
6e373954bbf6 Added GHOPStudent view and access checks.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2918
diff changeset
  1210
                                              student_logic, org_admin_logic,
6e373954bbf6 Added GHOPStudent view and access checks.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2918
diff changeset
  1211
                                              mentor_logic):
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1212
    """Checks if the current user has no roles for the given 
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1213
       program in django_args.
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1214
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1215
    Args:
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1216
      django_args: a dictionary with django's arguments
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1217
      program_logic: Program Logic instance
2922
6e373954bbf6 Added GHOPStudent view and access checks.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2918
diff changeset
  1218
      student_logic: Student Logic instance
6e373954bbf6 Added GHOPStudent view and access checks.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2918
diff changeset
  1219
      org_admin_logic: Org Admin Logic instance
6e373954bbf6 Added GHOPStudent view and access checks.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2918
diff changeset
  1220
      mentor_logic: Mentor Logic instance
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1221
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1222
     Raises:
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1223
       AccessViolationResponse: if the current user has a student, mentor or
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1224
                                org admin role for the given program.
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1225
    """
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1226
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1227
    if not django_args.get('scope_path'):
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1228
      raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_DENIED_MSG)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1229
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1230
    program_entity = program_logic.getFromKeyNameOr404(
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1231
        django_args['scope_path'])
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1232
    user_entity = user_logic.getForCurrentAccount()
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1233
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1234
    filter = {'user': user_entity,
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1235
              'scope': program_entity,
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1236
              'status': 'active'}
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1237
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1238
    # check if the current user is already a student for this program
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1239
    student_role = student_logic.getForFields(filter, unique=True)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1240
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1241
    if student_role:
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1242
      raise out_of_band.AccessViolation(
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1243
          message_fmt=DEF_ALREADY_PARTICIPATING_MSG)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1244
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1245
    # fill the role_list with all the mentor and org admin roles for this user
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1246
    # role_list = []
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1247
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1248
    filter = {'user': user_entity,
1832
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1249
              'program': program_entity,
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1250
              'status': 'active'}
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1251
1832
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1252
    mentor_role = mentor_logic.getForFields(filter, unique=True)
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1253
    if mentor_role:
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1254
      # the current user has a role for the given program
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1255
      raise out_of_band.AccessViolation(
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1256
            message_fmt=DEF_ALREADY_PARTICIPATING_MSG)
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1257
1832
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1258
    org_admin_role = org_admin_logic.getForFields(filter, unique=True)
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1259
    if org_admin_role:
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1260
      # the current user has a role for the given program
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1261
      raise out_of_band.AccessViolation(
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1262
            message_fmt=DEF_ALREADY_PARTICIPATING_MSG)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1263
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1264
    # no roles found, access granted
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1265
    return
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1266
3029
3c09b9396e2e Added logic paramters to two access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2957
diff changeset
  1267
  def checkIsNotStudentForProgramInScope(self, django_args, program_logic,
3c09b9396e2e Added logic paramters to two access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2957
diff changeset
  1268
                                         student_logic):
1559
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1269
    """Checks if the current user is not a student for the given
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1270
       program in django_args.
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1271
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1272
    Args:
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1273
      django_args: a dictionary with django's arguments
2781
8f6c0a5a7491 program_logic is passed as a parameter to three checkers than hard coding it.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2771
diff changeset
  1274
      program_logic: Program Logic instance
3029
3c09b9396e2e Added logic paramters to two access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2957
diff changeset
  1275
      student_logic: Student Logic instance
1559
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1276
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1277
     Raises:
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1278
       AccessViolationResponse: if the current user has a student
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1279
                                role for the given program.
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1280
    """
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1281
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1282
    if django_args.get('seed'):
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1283
      key_name = django_args['seed']['scope_path']
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1284
    else:
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1285
      key_name = django_args['scope_path']
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1286
1776
f86b0fe5f4d9 Replace getFromKeyName with getFromKeyNameOr404 where appropriate in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1716
diff changeset
  1287
    program_entity = program_logic.getFromKeyNameOr404(key_name)
1559
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1288
    user_entity = user_logic.getForCurrentAccount()
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1289
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1290
    filter = {'user': user_entity,
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1291
              'scope': program_entity,
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1292
              'status': 'active'}
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1293
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1294
    # check if the current user is already a student for this program
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1295
    student_role = student_logic.getForFields(filter, unique=True)
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1296
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1297
    if student_role:
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1298
      raise out_of_band.AccessViolation(
1832
5ded837037e7 Fixed checkIsNotParticipatingInProgramInScope to just query for the given program instead of iterating over a list.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1782
diff changeset
  1299
          message_fmt=DEF_ALREADY_STUDENT_ROLE_MSG)
1559
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1300
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1301
    return
283046e54c01 Fixed issue 205.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1558
diff changeset
  1302
3029
3c09b9396e2e Added logic paramters to two access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2957
diff changeset
  1303
  def checkIsNotStudentForProgramOfOrg(self, django_args, org_logic, student_logic):
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1304
    """Checks if the current user has no active Student role for the program
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1305
       that the organization in the scope_path is participating in.
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1306
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1307
    Args:
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1308
      django_args: a dictionary with django's arguments
3029
3c09b9396e2e Added logic paramters to two access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2957
diff changeset
  1309
      org_logic: Organization logic instance
3c09b9396e2e Added logic paramters to two access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2957
diff changeset
  1310
      student_logic: Student logic instance
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1311
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1312
     Raises:
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1313
       AccessViolationResponse: if the current user is a student for the
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1314
                                program the organization is in.
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1315
    """
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1316
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1317
    if not django_args.get('scope_path'):
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1318
      raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_DENIED_MSG)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1319
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1320
    self.checkIsUser(django_args)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1321
1776
f86b0fe5f4d9 Replace getFromKeyName with getFromKeyNameOr404 where appropriate in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1716
diff changeset
  1322
    org_entity = org_logic.getFromKeyNameOr404(django_args['scope_path'])
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1323
    user_entity = self.user
1375
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1324
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1325
    filter = {'scope': org_entity.scope,
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1326
              'user': user_entity,
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1327
              'status': 'active'}
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1328
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1329
    student_role = student_logic.getForFields(filter=filter, unique=True)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1330
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1331
    if student_role:
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1332
      raise out_of_band.AccessViolation(
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1333
          message_fmt=DEF_ALREADY_STUDENT_ROLE_MSG)
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1334
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1335
    return
edcb2add6934 Added new access methods.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1366
diff changeset
  1336
3053
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1337
  def checkIsNotStudentForProgramOfOrgInRequest(self, django_args, org_logic,
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1338
                                                student_logic):
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1339
    """Checks if the current user has no active Student role for the program
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1340
       that the organization in the request is participating in.
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1341
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1342
    Args:
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1343
      django_args: a dictionary with django's arguments
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1344
      org_logic: Organization logic instance
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1345
      student_logic: Student logic instance
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1346
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1347
     Raises:
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1348
       AccessViolationResponse: if the current user is a student for the
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1349
                                program the organization is in.
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1350
    """
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1351
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1352
    request_entity = request_logic.getFromIDOr404(int(django_args['id']))
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1353
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1354
    django_args['scope_path'] = request_entity.group.key().id_or_name()
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1355
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1356
    return self.checkIsNotStudentForProgramOfOrg(django_args, org_logic, student_logic)
1dc307cb97d0 Redone the access checks to work with the new Request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 3032
diff changeset
  1357
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1358
  @allowDeveloper
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1359
  def checkRoleAndStatusForStudentProposal(self, django_args, allowed_roles,
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1360
                                           role_status, proposal_status):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1361
    """Checks if the current user has access to the given proposal.
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1362
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1363
    Args:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1364
      django_args: a dictionary with django's arguments
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1365
      allowed_roles: list with names for the roles allowed to pass access check
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1366
      role_status: list with states allowed for the role
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1367
      proposal_status: a list with states allowed for the proposal
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1368
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1369
     Raises:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1370
       AccessViolationResponse:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1371
         - If there is no proposal found
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1372
         - If the proposal is not in one of the required states.
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1373
         - If the user does not have any ofe the required roles
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1374
    """
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1375
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1376
    self.checkIsUser(django_args)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1377
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1378
    # bail out with 404 if no proposal is found
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1379
    proposal_entity = student_proposal_logic.getFromKeyFieldsOr404(django_args)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1380
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1381
    if not proposal_entity.status in proposal_status:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1382
      # this proposal can not be accessed at the moment
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1383
      raise out_of_band.AccessViolation(
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1384
          message_fmt=DEF_NO_ACTIVE_ENTITY_MSG)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1385
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1386
    user_entity = self.user
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1387
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1388
    if 'proposer' in allowed_roles:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1389
      # check if this proposal belongs to the current user
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1390
      student_entity = proposal_entity.scope
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1391
      if (user_entity.key() == student_entity.user.key()) and (
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1392
          student_entity.status in role_status):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1393
        return
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1394
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1395
    filter = {'user': user_entity,
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1396
        'status': role_status}
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1397
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1398
    if 'host' in allowed_roles:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1399
      # check if the current user is a host for this proposal's program
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1400
      filter['scope'] =  proposal_entity.program
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1401
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1402
      if host_logic.getForFields(filter, unique=True):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1403
        return
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1404
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1405
    if 'org_admin' in allowed_roles:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1406
      # check if the current user is an admin for this proposal's org
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1407
      filter['scope'] = proposal_entity.org
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1408
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1409
      if org_admin_logic.getForFields(filter, unique=True):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1410
        return
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1411
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1412
    if 'mentor' in allowed_roles:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1413
      # check if the current user is a mentor for this proposal's org
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1414
      filter['scope'] = proposal_entity.org
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1415
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1416
      if mentor_logic.getForFields(filter, unique=True):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1417
        return
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1418
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1419
    # no roles found, access denied
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1420
    raise out_of_band.AccessViolation(
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1421
        message_fmt=DEF_NEED_ROLE_MSG)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1422
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1423
  @allowDeveloper
1995
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1424
  def checkCanStudentPropose(self, django_args, key_location, check_limit):
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1425
    """Checks if the program for this student accepts proposals.
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1426
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1427
    Args:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1428
      django_args: a dictionary with django's arguments
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1429
      key_location: the key for django_args in which the key_name
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1430
                    from the student is stored
1995
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1431
      check_limit: iff true checks if the student reached the apps_tasks_limit
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1432
                   for the given program.
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1433
    """
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1434
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1435
    self.checkIsUser(django_args)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1436
1529
0800753ebc6a Fixed a bug in student access check
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1528
diff changeset
  1437
    if django_args.get('seed'):
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1438
      key_name = django_args['seed'][key_location]
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1439
    else:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1440
      key_name = django_args[key_location]
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1441
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1442
    student_entity = student_logic.getFromKeyName(key_name)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1443
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1444
    if not student_entity or student_entity.status == 'invalid':
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1445
      raise out_of_band.AccessViolation(
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1446
        message_fmt=DEF_SIGN_UP_AS_STUDENT_MSG)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1447
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1448
    program_entity = student_entity.scope
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1449
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1450
    if not timeline_helper.isActivePeriod(program_entity.timeline,
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1451
                                          'student_signup'):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1452
      raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_INACTIVE_MSG)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1453
1995
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1454
    if check_limit:
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1455
      # count all studentproposals by the student
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1456
      fields = {'scope': student_entity}
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1457
      proposal_query = student_proposal_logic.getQueryForFields(fields)
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1458
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1459
      if proposal_query.count() >= program_entity.apps_tasks_limit:
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1460
        # too many proposals access denied
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1461
        raise out_of_band.AccessViolation(message_fmt=DEF_MAX_PROPOSALS_REACHED)
768f533d91e3 Added check to prevent students from submitting more proposals then allowed by the program settings.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1922
diff changeset
  1462
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1463
    return
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1464
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1465
  @allowDeveloper
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1466
  def checkIsStudent(self, django_args, key_location, status):
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1467
    """Checks if the current user is the given student.
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1468
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1469
    Args:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1470
      django_args: a dictionary with django's arguments
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1471
      key_location: the key for django_args in which the key_name
1466
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1472
                    from the student is stored
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1473
      status: the allowed status for the student
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1474
    """
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1475
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1476
    self.checkIsUser(django_args)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1477
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1478
    if 'seed' in django_args:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1479
      key_name = django_args['seed'][key_location]
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1480
    else:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1481
      key_name = django_args[key_location]
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1482
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1483
    student_entity = student_logic.getFromKeyName(key_name)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1484
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1485
    if not student_entity or student_entity.status not in status:
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1486
      raise out_of_band.AccessViolation(
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1487
        message_fmt=DEF_SIGN_UP_AS_STUDENT_MSG)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1488
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1489
    if student_entity.user.key() != self.user.key():
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1490
      # this is not the page for the current user
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1491
      self.deny(django_args)
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1492
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1493
    return
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1494
bfcec687b362 Added several access checks for student proposals.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1445
diff changeset
  1495
  @allowDeveloper
2112
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1496
  def checkIsMyStudentProject(self, django_args):
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1497
    """Checks whether the project belongs to the current user.
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1498
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1499
    Args:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1500
      django_args: a dictionary with django's arguments
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1501
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1502
     Raises:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1503
       AccessViolationResponse:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1504
         - If there is no project found
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1505
         - If the project does not belong to the current user
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1506
    """
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1507
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1508
    self.checkIsUser()
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1509
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1510
    project_entity = student_project_logic.getFromKeyFieldsOr404(django_args)
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1511
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1512
    if project_entity.student.user.key() != self.user.key():
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1513
      raise out_of_band.AccessViolation(
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1514
          message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1515
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1516
    return
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1517
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1518
  @allowDeveloper
2710
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1519
  def checkIsHostForStudentProject(self, django_args):
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1520
    """Checks whether the user is Host for the specified project.
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1521
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1522
    Args:
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1523
      django_args: a dictionary with django's arguments
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1524
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1525
     Raises:
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1526
       AccessViolationResponse:
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1527
         - If there is no project found
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1528
         - If the user is not a host for hte specified project
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1529
    """
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1530
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1531
    self.checkIsUser()
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1532
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1533
    project_entity = student_project_logic.getFromKeyFieldsOr404(django_args)
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1534
    program_entity = project_entity.program
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1535
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1536
    new_args = {'scope_path': program_entity.scope_path }
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1537
    self.checkHasActiveRoleForScope(new_args, host_logic)
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1538
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1539
    return
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1540
9a93624bd2d4 add checkIsHostForStudentProject checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2660
diff changeset
  1541
  @allowDeveloper
2112
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1542
  def checkStudentProjectHasStatus(self, django_args, allowed_status):
2145
ea919c2dfe41 Fixed typo in checkStudentProjectHasStatus docstring.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2116
diff changeset
  1543
    """Checks whether the Project has one of the given statuses.
2112
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1544
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1545
    Args:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1546
      django_args: a dictionary with django's arguments
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1547
      allowed_status: list with the allowed statusses for the entity
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1548
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1549
     Raises:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1550
       AccessViolationResponse:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1551
         - If there is no project found
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1552
         - If the project is not in the requested status
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1553
    """
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1554
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1555
    project_entity = student_project_logic.getFromKeyFieldsOr404(django_args)
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1556
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1557
    if not project_entity.status in allowed_status:
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1558
      raise out_of_band.AccessViolation(
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1559
          message_fmt=DEF_NO_ACTIVE_ENTITY_MSG)
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1560
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1561
    return
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1562
9a2c9354468c Added 2 new access checks to deal with StudentProjects.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2101
diff changeset
  1563
  @allowDeveloper
1180
6290c9e49848 Fixed club_app
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1179
diff changeset
  1564
  def checkIsMyEntity(self, django_args, logic,
6290c9e49848 Fixed club_app
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1179
diff changeset
  1565
                      field_name='user', user=False):
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1566
    """Checks whether the entity belongs to the user.
1481
0ccf92d073e8 Add missing comment to checkIsMyEntity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1475
diff changeset
  1567
0ccf92d073e8 Add missing comment to checkIsMyEntity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1475
diff changeset
  1568
    Args:
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1569
      django_args: a dictionary with django's arguments
1481
0ccf92d073e8 Add missing comment to checkIsMyEntity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1475
diff changeset
  1570
      logic: the logic that should be used to fetch the entity
0ccf92d073e8 Add missing comment to checkIsMyEntity
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1475
diff changeset
  1571
      field_name: the name of the field the entity uses to store it's owner
2020
df3552deba66 Clarified docstring in access.py and changed edit access checks for role views.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1995
diff changeset
  1572
      user: true iff the entity stores the user's reference, false iff keyname
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1573
    """
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
  1574
1012
73f0b61f2d9d Fold checkAgreesToSiteToS into checkIsUser
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1007
diff changeset
  1575
    self.checkIsUser(django_args)
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1576
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1577
    fields = {
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1578
        'link_id': django_args['link_id'],
2160
3f9dd37d98a8 Use key().id_or_name() instead of key().name()
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2145
diff changeset
  1579
        field_name: self.user if user else self.user.key().id_or_name()
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1580
        }
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1581
1200
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1582
    if 'scope_path' in django_args:
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1583
      fields['scope_path'] = django_args['scope_path']
e68fd70ba076 Added checkCanEditGroupApp to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1198
diff changeset
  1584
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1585
    entity = logic.getForFields(fields)
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
  1586
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1587
    if entity:
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1588
      return
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
  1589
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1590
    raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1591
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1592
  @allowDeveloper
2918
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1593
  def checkIsMyActiveRole(self, django_args, role_logic):
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1594
    """Checks whether the current user has the active role given by django_args.
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1595
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1596
    Args:
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1597
      django_args: a dictionary with django's arguments
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1598
      logic: the logic that should be used to fetch the role
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1599
    """
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1600
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1601
    self.checkIsUser(django_args)
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1602
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1603
    entity = role_logic.getFromKeyFieldsOr404(django_args)
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1604
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1605
    if entity.user.key() != self.user.key() or (
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1606
        entity.link_id != self.user.link_id):
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1607
      raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1608
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1609
    if entity.status != 'active':
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1610
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_ROLE_MSG)
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1611
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1612
    # this role belongs to the current user and is active
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1613
    return
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1614
d24b80677879 Added checkIsMyActiveRole check to the access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2850
diff changeset
  1615
  @allowDeveloper
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1616
  @denySidebar
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1617
  def checkIsAllowedToManageRole(self, django_args, logic_for_role, 
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1618
      manage_role_logic):
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1619
    """Returns an alternate HTTP response if the user is not allowed to manage
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1620
       the role given in args.
1068
8a06ebff014e Changed docstring for checkIsAllowedToManageRole.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1066
diff changeset
  1621
8a06ebff014e Changed docstring for checkIsAllowedToManageRole.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1066
diff changeset
  1622
     Args:
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1623
       django_args: a dictionary with django's arguments
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1624
       logic_for_role: determines the logic for the role in args.
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1625
       manage_role_logic: determines the logic for the role which is allowed
1068
8a06ebff014e Changed docstring for checkIsAllowedToManageRole.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1066
diff changeset
  1626
           to manage this role.
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1627
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1628
     Raises:
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1629
       AccessViolationResponse: if the required authorization is not met
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1630
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1631
    Returns:
1068
8a06ebff014e Changed docstring for checkIsAllowedToManageRole.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1066
diff changeset
  1632
      None if the given role is active and belongs to the current user.
1524
30ada09bdc6f Make django_args optional where appropriate and whitespace fixes
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1505
diff changeset
  1633
      None if the current User has an active role (from manage_role_logic)
1068
8a06ebff014e Changed docstring for checkIsAllowedToManageRole.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1066
diff changeset
  1634
           that belongs to the same scope as the role that needs to be managed
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1635
    """
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1636
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1637
    try:
2178
1d27b489e7e4 Fixes access check for manage view.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2171
diff changeset
  1638
      # check if it is the user's own role
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1639
      self.checkHasActiveRoleForScope(django_args, logic_for_role)
2178
1d27b489e7e4 Fixes access check for manage view.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2171
diff changeset
  1640
      self.checkIsMyEntity(django_args, logic_for_role, 'user', True)
2032
b881417db09e Added missing return to manage access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2020
diff changeset
  1641
      return
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1642
    except out_of_band.Error:
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1643
      pass
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1644
2077
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1645
    # apparently it's not the user's role so check 
fd2e83a297c7 Style fixes, add missing docstring, fix some wrong calls to super classes and removal of unused imports in soc.views.helper.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 2032
diff changeset
  1646
    # if managing this role is allowed
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1647
    fields = {
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1648
        'link_id': django_args['link_id'],
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1649
        'scope_path': django_args['scope_path'],
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1650
        }
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1651
2178
1d27b489e7e4 Fixes access check for manage view.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2171
diff changeset
  1652
    role_entity = logic_for_role.getFromKeyFieldsOr404(fields)
1d27b489e7e4 Fixes access check for manage view.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2171
diff changeset
  1653
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
  1654
    if role_entity.status != 'active':
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
  1655
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_ACTIVE_ROLE_MSG)
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
  1656
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1657
    fields = {
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  1658
        'link_id': self.user.link_id,
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1659
        'scope_path': django_args['scope_path'],
1142
da2487767ef4 Fix missing dot and wrong import sorting in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1135
diff changeset
  1660
        'status': 'active'
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1661
        }
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1662
1184
bd9c6101d41d Use .logic in checkIsAllowedToManageRole at the caller
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1180
diff changeset
  1663
    manage_entity = manage_role_logic.getForFields(fields, unique=True)
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1664
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1665
    if not manage_entity:
1239
065e5bcf90f0 Stylefixes in access.py and removal of self.deny calls
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1237
diff changeset
  1666
      raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_ENTITY_MSG)
1066
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1667
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1668
    return
b22750a2b04a Added checkIsAllowedToManageRole to access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1061
diff changeset
  1669
1265
cecb2b35f805 Added allowsidebar to checkIsDocumentReadable.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1263
diff changeset
  1670
  @allowSidebar
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1107
diff changeset
  1671
  @allowDeveloper
2514
53fabbd7c6f3 Style fixes in Access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2456
diff changeset
  1672
  def checkIsSurveyReadable(self, django_args, survey_logic,
53fabbd7c6f3 Style fixes in Access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2456
diff changeset
  1673
                            key_name_field=None):
2433
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1674
    """Checks whether a survey is readable.
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1675
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1676
    Args:
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1677
      django_args: a dictionary with django's arguments
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1678
      key_name_field: key name field
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1679
    """
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1680
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1681
    if key_name_field:
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1682
      key_name = django_args[key_name_field]
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1683
      survey = survey_logic.getFromKeyNameOr404(key_name)
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1684
    else:
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1685
      survey = survey_logic.getFromKeyFieldsOr404(django_args)
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1686
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1687
    self.checkMembership('read', survey.prefix,
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1688
                         survey.read_access, django_args)
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1689
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1690
  @denySidebar
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1691
  @allowDeveloper
2733
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1692
  def checkIsMySurveyRecord(self, django_args, survey_logic, id_field):
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1693
    """Checks if the SurveyRecord given in the GET arguments as id_field is
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1694
    from the current user.
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1695
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1696
    Args:
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1697
      django_args: a dictionary with django's arguments
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1698
      survey_logic: Survey Logic which contains the needed Record logic
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1699
      id_field: name of the field in the GET dictionary that contains the Record ID.
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1700
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1701
    Raises:
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1702
      AccesViolation if:
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1703
        - There is no valid numeric record ID present in the GET dict
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1704
        - There is no SurveyRecord with the found ID
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1705
        - The SurveyRecord has not been taken by the current user
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1706
    """
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1707
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1708
    self.checkIsUser(django_args)
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1709
    user_entity = self.user
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1710
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1711
    get_dict = django_args['GET']
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1712
    record_id = get_dict.get(id_field)
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1713
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1714
    if not record_id or not record_id.isdigit():
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1715
      raise out_of_band.AccessViolation(
2734
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1716
          message_fmt=DEF_NO_VALID_RECORD_ID)
2733
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1717
    else:
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1718
      record_id = int(record_id)
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1719
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1720
    record_logic = survey_logic.getRecordLogic()
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1721
    record_entity = record_logic.getFromIDOr404(record_id)
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1722
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1723
    if record_entity.user.key() != user_entity.key():
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1724
      raise out_of_band.AccessViolation(
2734
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1725
          message_fmt=DEF_NOT_YOUR_RECORD)
2733
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1726
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1727
  @denySidebar
054a2227249c Added and enabled access check for viewing SurveyRecords.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2732
diff changeset
  1728
  @allowDeveloper
2514
53fabbd7c6f3 Style fixes in Access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2456
diff changeset
  1729
  def checkIsSurveyWritable(self, django_args, survey_logic,
53fabbd7c6f3 Style fixes in Access module.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2456
diff changeset
  1730
                            key_name_field=None):
2433
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1731
    """Checks whether a survey is writable.
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1732
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1733
    Args:
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1734
      django_args: a dictionary with django's arguments
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1735
      key_name_field: key name field
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1736
    """
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1737
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1738
    if key_name_field:
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1739
      key_name = django_args[key_name_field]
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1740
      survey = survey_logic.getFromKeyNameOr404(key_name)
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1741
    else:
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1742
      survey = survey_logic.getFromKeyFieldsOr404(django_args)
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1743
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1744
    self.checkMembership('write', survey.prefix,
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1745
                         survey.write_access, django_args)
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1746
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1747
  @denySidebar
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1748
  @allowDeveloper
2543
4c95d717a976 Added Menu items for all three kinds of Surveys to the program menu.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2538
diff changeset
  1749
  def checkIsSurveyTakeable(self, django_args, survey_logic, check_time=True):
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1750
    """Checks if the survey specified in django_args can be taken.
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1751
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1752
    Uses survey.taking_access to map that string onto a check. Also checks for
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1753
    survey start and end.
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1754
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1755
    If the prefix is 'program', the scope of the survey is the program and
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1756
    the taking_acccess attribute means:
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1757
      mentor: user is mentor for the program
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1758
      org_admin: user is org_admin for the program
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1759
      student: user is student for the program
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1760
      user: valid user on the website
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1761
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1762
    Args:
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1763
      survey_logic: SurveyLogic instance (or subclass)
2543
4c95d717a976 Added Menu items for all three kinds of Surveys to the program menu.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2538
diff changeset
  1764
      check_time: iff True checks if the current date is between the survey
4c95d717a976 Added Menu items for all three kinds of Surveys to the program menu.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2538
diff changeset
  1765
        start and end date.
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1766
    """
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1767
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1768
    if django_args['prefix'] != 'program':
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1769
      # TODO: update when generic surveys are allowed
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1770
      return self.deny(django_args)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1771
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1772
    # get the survey from django_args
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1773
    survey = survey_logic.getFromKeyFieldsOr404(django_args)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1774
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1775
    # check if the survey can be taken now
2543
4c95d717a976 Added Menu items for all three kinds of Surveys to the program menu.
James Levy <jamesalexanderlevy@gmail.com>
parents: 2538
diff changeset
  1776
    if check_time and not timeline_helper.isActivePeriod(survey, 'survey'):
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1777
      raise out_of_band.AccessViolation(message_fmt=DEF_PAGE_INACTIVE_MSG)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1778
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1779
    # retrieve the role that is allowed to take this survey
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1780
    role = survey.taking_access
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1781
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1782
    if role == 'user':
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1783
      # check if the current user is registered
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1784
      return self.checkIsUser(django_args)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1785
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1786
    django_args = django_args.copy()
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1787
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1788
    # get the survey scope
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1789
    survey_scope = survey_logic.getScope(survey)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1790
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1791
    if role == 'mentor':
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1792
      # check if the current user is a mentor for the program in survey.scope
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1793
      django_args['program'] = survey_scope
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1794
      # program is the 'program' attribute for mentors and org_admins
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1795
      return self._checkHasActiveRoleFor(django_args, mentor_logic, 'program')
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1796
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1797
    if role == 'org_admin':
2792
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1798
      # check if the current user is an org admin for the program
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1799
      django_args['program'] = survey_scope
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1800
      # program is the 'program' attribute for mentors and org_admins
2660
e2654d53a643 Another batch of style fixes for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2610
diff changeset
  1801
      return self._checkHasActiveRoleFor(django_args, org_admin_logic,
e2654d53a643 Another batch of style fixes for Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2610
diff changeset
  1802
                                         'program')
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1803
2792
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1804
    if role == 'org':
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1805
      # check if the current user is an org admin or mentor for the program
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1806
      django_args['program'] = survey_scope
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1807
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1808
      try:
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1809
        # program is the 'program' attribute for mentors and org_admins
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1810
        return self._checkHasActiveRoleFor(django_args, org_admin_logic,
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1811
                                          'program')
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1812
      except:
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1813
        # the current user is no org admin
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1814
        pass
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1815
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1816
      # try to check if the current user is a mentor instead
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1817
      return self._checkHasActiveRoleFor(django_args, mentor_logic, 'program')
14a62fcf4e02 Added org taking access to Surveys.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2781
diff changeset
  1818
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1819
    if role == 'student':
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1820
      # check if the current user is a student for the program in survey.scope
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1821
      django_args['scope'] = survey_scope
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1822
      # program is the 'scope' attribute for students
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1823
      return self.checkHasActiveRoleForScope(django_args, student_logic)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1824
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1825
    # unknown role
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1826
    self.deny(django_args)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1827
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1828
  @denySidebar
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1829
  @allowDeveloper
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1830
  def checkIsAllowedToTakeProjectSurveyAs(self, django_args, survey_logic,
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1831
                                          role_name, project_key_location):
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1832
    """Checks whether a ProjectSurvey can be taken by the current User.
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1833
2732
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1834
    role_name argument determines wether the current user is taking the survey
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1835
    as a student or mentor specified by the project in GET dict.
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1836
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1837
    If the survey is taken as a mentor, org admins for the Organization in
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1838
    which the project resides will also have access.
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1839
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1840
    However if the project entry is not present in the dictionary this access
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1841
    check passes.
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1842
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1843
    Args:
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1844
      django_args: a dictionary with django's arguments
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1845
      survey_logic: instance of ProjectSurveyLogic (or subclass)
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1846
      role_name: String containing either "student" or "mentor"
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1847
      project_key_location: String containing the key entry in the GET dict
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1848
        where the key for the project can be located.
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1849
    """
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1850
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1851
    if not role_name in ['mentor', 'student']:
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1852
      raise InvalidArgumentError('role_name is not mentor or student')
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1853
2732
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1854
    # check if the current user is signed up
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1855
    self.checkIsUser(django_args)
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1856
    user_entity = self.user
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1857
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1858
    # get the project keyname from the GET dictionary
2734
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1859
    get_dict = django_args['GET']
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1860
    key_name = get_dict.get(project_key_location)
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1861
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1862
    if not key_name:
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1863
      # no key name present so no need to deny access
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1864
      return
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1865
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1866
    # retrieve the Student Project for the key
2732
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1867
    project_entity = student_project_logic.getFromKeyNameOr404(key_name)
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  1868
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1869
    # check if a survey can be conducted about this project
2732
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1870
    if project_entity.status != 'accepted':
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1871
      raise out_of_band.AccessViolation(
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1872
          message_fmt=DEF_NOT_ALLOWED_PROJECT_FOR_SURVEY_MSG)
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1873
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1874
    # get the correct role depending on the role_name
2732
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1875
    if role_name == 'student':
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1876
      role_entity = project_entity.student
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1877
    elif role_name == 'mentor':
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1878
      role_entity = project_entity.mentor
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1879
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1880
    # check if the role matches the current user
2734
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1881
    if role_entity.user.key() != user_entity.key() and (
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1882
        role_entity.status == 'active'):
2732
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1883
      if role_name == 'student':
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1884
        raise out_of_band.AccessViolation(
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1885
            message_fmt=DEF_NOT_ALLOWED_PROJECT_FOR_SURVEY_MSG)
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1886
      elif role_name == 'mentor':
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1887
        # check if the current user is an Org Admin for this Student Project
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1888
        fields = {'user': user_entity,
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1889
                  'scope': project_entity.scope,
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1890
                  'status': 'active'}
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1891
        admin_entity = org_admin_logic.getForFields(fields, unique=True)
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1892
        if not admin_entity:
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1893
          # this user is no Org Admin or Mentor for this project
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1894
          raise out_of_band.AccessViolation(
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1895
              message_fmt=DEF_NOT_ALLOWED_PROJECT_FOR_SURVEY_MSG)
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1896
    elif role_entity.status != 'active':
16ba61efc108 Enabled Org Admins to take Surveys on behalf of their Mentors.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2717
diff changeset
  1897
      # this role is not active
2536
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1898
      raise out_of_band.AccessViolation(message_fmt=DEF_NEED_ROLE_MSG)
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1899
9f1b7aba026f Added access checks for taking a Project(Grading)Survey.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2534
diff changeset
  1900
    return
2433
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1901
2734
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1902
  @denySidebar
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1903
  @allowDeveloper
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1904
  def checkIsAllowedToViewProjectSurveyRecordAs(
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1905
      self, django_args, survey_logic, role_name, record_key_location):
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1906
    """Checks wether the current user is allowed to view the record given in
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1907
    the GET data by the record_key_location.
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1908
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1909
    Args:
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1910
      django_args: a dictionary with django's arguments
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1911
      survey_logic: Survey Logic instance that belongs to the SurveyRecord
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1912
        type in question
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1913
      role_name: string containing either "student" or "mentor". Determines
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1914
        which of the roles the within the project the current user should have
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1915
        to view the evaluation results.
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1916
      record_key_location: string containing the name of the GET param which
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1917
        contains the id for the SurveyRecord to retrieve
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1918
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1919
    Raises:
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1920
      AccessViolation if:
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1921
        - No valid numeric Record ID is given in the POST data.
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1922
        - No Record could be retrieved for the given Record ID.
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1923
        - The current user has not taken the survey, is not the Student/Mentor
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1924
          (depending on the role_name) and is not an Org Admin for the project
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1925
          to which the SurveyRecord belongs.
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1926
    """
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1927
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1928
    if not role_name in ['mentor', 'student']:
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1929
      raise InvalidArgumentError('role_name is not mentor or student')
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1930
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1931
    self.checkIsUser(django_args)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1932
    user_entity = self.user
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1933
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1934
    get_dict = django_args['GET']
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1935
    record_id = get_dict.get(record_key_location)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1936
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1937
    if not record_id or not record_id.isdigit():
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1938
      raise out_of_band.AccessViolation(
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1939
          message_fmt=DEF_NO_VALID_RECORD_ID)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1940
    else:
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1941
      record_id = int(record_id)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1942
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1943
    record_logic = survey_logic.getRecordLogic()
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1944
    record_entity = record_logic.getFromIDOr404(record_id)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1945
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1946
    if record_entity.user.key() == user_entity.key():
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1947
      # this record belongs to the current user
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1948
      return
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1949
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1950
    if role_name == 'student':
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1951
      role_entity = record_entity.project.student
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1952
    elif role_name == 'mentor':
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1953
      role_entity = record_entity.project.mentor
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1954
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1955
    if role_entity.user.key() == user_entity.key() and (
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1956
        role_entity.status == 'active'):
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1957
      # this user has the role required
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1958
      return
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1959
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1960
    fields = {'user': user_entity,
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1961
              'scope': record_entity.org,
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1962
              'status': 'active'}
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1963
    admin_entity = org_admin_logic.getForFields(fields, unique=True)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1964
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1965
    if admin_entity:
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1966
      # this user is org admin for the retrieved record's project
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1967
      return
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1968
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1969
    # The current user is no Org Admin, has not taken the Survey and is not
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1970
    # the one responsible for taking this survey.
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1971
    raise out_of_band.AccessViolation(message_fmt=DEF_NOT_YOUR_RECORD)
f35f6f05c8c4 Added and enabled new access check for viewing ProjectSurvey records.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2733
diff changeset
  1972
2433
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1973
  @allowSidebar
0ba41e115dbc Added Access checks for Surveys.
Daniel Diniz <ajaksu@gmail.com>
parents: 2380
diff changeset
  1974
  @allowDeveloper
1496
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  1975
  def checkIsDocumentReadable(self, django_args, key_name_field=None):
2380
f23025455742 Clarified docstring for document read/write access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2361
diff changeset
  1976
    """Checks whether a document is readable by the current user.
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
  1977
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1978
    Args:
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1979
      django_args: a dictionary with django's arguments
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1980
      key_name_field: key name field
1007
3b66772d21a5 Major refactor of the access module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 999
diff changeset
  1981
    """
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
  1982
1496
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  1983
    if key_name_field:
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  1984
      key_name = django_args[key_name_field]
1776
f86b0fe5f4d9 Replace getFromKeyName with getFromKeyNameOr404 where appropriate in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1716
diff changeset
  1985
      document = document_logic.getFromKeyNameOr404(key_name)
1496
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  1986
    else:
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  1987
      document = document_logic.getFromKeyFieldsOr404(django_args)
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1988
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1989
    self.checkMembership('read', document.prefix,
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1990
                         document.read_access, django_args)
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1991
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1992
  @denySidebar
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1993
  @allowDeveloper
1496
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  1994
  def checkIsDocumentWritable(self, django_args, key_name_field=None):
2380
f23025455742 Clarified docstring for document read/write access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2361
diff changeset
  1995
    """Checks whether a document is writable by the current user.
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1996
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1997
    Args:
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  1998
      django_args: a dictionary with django's arguments
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  1999
      key_name_field: key name field
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2000
    """
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2001
1496
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  2002
    if key_name_field:
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  2003
      key_name = django_args[key_name_field]
1776
f86b0fe5f4d9 Replace getFromKeyName with getFromKeyNameOr404 where appropriate in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1716
diff changeset
  2004
      document = document_logic.getFromKeyNameOr404(key_name)
1496
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  2005
    else:
da531df1d92e Allow the document lookup to be done by key name
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1488
diff changeset
  2006
      document = document_logic.getFromKeyFieldsOr404(django_args)
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2007
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2008
    self.checkMembership('write', document.prefix,
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2009
                         document.write_access, django_args)
1074
94bc2a9ae103 Properly check if a program is active
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1073
diff changeset
  2010
2583
3a8275049403 Added denySidebar to several access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2543
diff changeset
  2011
  @denySidebar
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2012
  @allowDeveloper
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2013
  def checkDocumentList(self, django_args):
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2014
    """Checks whether the user is allowed to list documents.
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2015
    
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2016
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2017
      django_args: a dictionary with django's arguments
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2018
    """
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2019
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2020
    filter = django_args['filter']
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2021
    prefix = filter['prefix']
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2022
2956
50ce8ac13932 Use the new rights code in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2936
diff changeset
  2023
    checker = callback.getCore().getRightsChecker(prefix)
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2024
    roles = checker.getMembership('list')
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2025
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2026
    if not self.hasMembership(roles, filter):
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2027
      raise out_of_band.AccessViolation(message_fmt=DEF_NO_LIST_ACCESS_MSG)
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2028
2583
3a8275049403 Added denySidebar to several access checks.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2543
diff changeset
  2029
  @denySidebar
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1315
diff changeset
  2030
  @allowDeveloper
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2031
  def checkDocumentPick(self, django_args):
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2032
    """Checks whether the user has access to the specified pick url.
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2033
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2034
    Will update the 'read_access' field of django_args['GET'].
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2035
    
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2036
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2037
      django_args: a dictionary with django's arguments
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2038
    """
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2039
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2040
    get_args = django_args['GET']
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2041
    # make mutable in order to inject the proper read_access filter
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2042
    mutable = get_args._mutable
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2043
    get_args._mutable = True
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2044
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2045
    if 'prefix' not in get_args:
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2046
      raise out_of_band.AccessViolation(message_fmt=DEF_PREFIX_NOT_IN_ARGS_MSG)
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2047
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2048
    prefix = get_args['prefix']
1577
b0bb5c8b5f78 Fix bug in checkDocumentPick of soc.views.helper.access module, which cause KeyError exceptions in document picker.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1559
diff changeset
  2049
    django_args['prefix'] = prefix
1606
6f7e5a566d9a Fix the KeyError: 'scope_path' error in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1577
diff changeset
  2050
    django_args['scope_path'] = get_args['scope_path']
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2051
2956
50ce8ac13932 Use the new rights code in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2936
diff changeset
  2052
    checker = callback.getCore().getRightsChecker(prefix)
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2053
    memberships = checker.getMemberships()
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2054
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2055
    roles = []
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2056
    for key, value in memberships.iteritems():
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2057
      if self.hasMembership(value, django_args):
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2058
        roles.append(key)
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2059
1309
ba51a0cd311d Fix a bug if you have no roles at all
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
  2060
    if not roles:
ba51a0cd311d Fix a bug if you have no roles at all
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
  2061
      roles = ['deny']
ba51a0cd311d Fix a bug if you have no roles at all
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
  2062
1305
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2063
    get_args.setlist('read_access', roles)
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2064
    get_args._mutable = mutable
9567bb475d6d Do access checks on the pick url for documents
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1300
diff changeset
  2065
2936
75f9c945ed8a Added program_logic parameter to checkCanEditTimeline access check and corresponding changes in timeline views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2922
diff changeset
  2066
  def checkCanEditTimeline(self, django_args, program_logic):
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2067
    """Checks whether this program's timeline may be edited.
2534
c880489123fc Fixed basic Survey access check.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2523
diff changeset
  2068
1633
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2069
    Args:
6b65707f4248 Remove unused imports, style and too long lines fixes, remove unused variables, rename too short variable names, fix docstrings in soc.views.helper.access module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1606
diff changeset
  2070
      django_args: a dictionary with django's arguments
2936
75f9c945ed8a Added program_logic parameter to checkCanEditTimeline access check and corresponding changes in timeline views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2922
diff changeset
  2071
      program_logic: Program Logic instance
1135
24d695060863 Hook up the ACL system for documents.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1122
diff changeset
  2072
    """
1107
a878188e225c Added status to program.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1085
diff changeset
  2073
2936
75f9c945ed8a Added program_logic parameter to checkCanEditTimeline access check and corresponding changes in timeline views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2922
diff changeset
  2074
    time_line_keyname = program_logic.timeline_logic.getKeyNameFromFields(
75f9c945ed8a Added program_logic parameter to checkCanEditTimeline access check and corresponding changes in timeline views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2922
diff changeset
  2075
        django_args)
75f9c945ed8a Added program_logic parameter to checkCanEditTimeline access check and corresponding changes in timeline views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2922
diff changeset
  2076
    timeline_entity = program_logic.timeline_logic.getFromKeyNameOr404(
75f9c945ed8a Added program_logic parameter to checkCanEditTimeline access check and corresponding changes in timeline views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 2922
diff changeset
  2077
        time_line_keyname)
1107
a878188e225c Added status to program.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1085
diff changeset
  2078
1475
22b63ab59b27 Make timeline a Linkable
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1466
diff changeset
  2079
    fields = program_logic.getKeyFieldsFromFields(django_args)
1163
d8c50be19232 Cleaned up access.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1142
diff changeset
  2080
    self.checkIsHostForProgram(fields)