app/soc/views/helper/access.py
author Lennard de Rijk <ljvderijk@gmail.com>
Fri, 23 Jan 2009 23:42:07 +0000
changeset 940 a40056afef83
parent 931 1131884c3c56
child 942 2250bb08f8e2
permissions -rw-r--r--
Changed the access checks to comply with state in request. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed
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
#
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
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__ = [
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    27
  '"Todd Larsen" <tlarsen@google.com>',
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
726
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
    29
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
  '"Pawel Solyga" <pawel.solyga@gmail.com>',
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
  ]
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
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
from google.appengine.api import users
315
c4f1a07ee340 Add missing blank lines between imports in access.py module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 309
diff changeset
    35
746
018efb9863dc Fix import sorting in soc.views.helper.access module and rename 'host' to 'Program Administrator'.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 729
diff changeset
    36
from django.core import urlresolvers
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
from django.utils.translation import ugettext_lazy
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
    39
from soc.logic import accounts
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
    40
from soc.logic import dicts
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
    41
from soc.logic.models.club_admin import logic as club_admin_logic
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
    42
from soc.logic.models.host import logic as host_logic
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.notification import logic as notification_logic
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
    44
from soc.logic.models.request import logic as request_logic
891
3d40190f35b6 Move getToSLink() to soc.views.helper.redirects.getToSRedirect().
Todd Larsen <tlarsen@google.com>
parents: 890
diff changeset
    45
from soc.logic.models.site import logic as site_logic
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
    46
from soc.logic.models.user import logic as user_logic
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    47
from soc.views import helper
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
    48
from soc.views import out_of_band
891
3d40190f35b6 Move getToSLink() to soc.views.helper.redirects.getToSRedirect().
Todd Larsen <tlarsen@google.com>
parents: 890
diff changeset
    49
from soc.views.helper import redirects
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    50
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
DEF_NO_USER_LOGIN_MSG_FMT = ugettext_lazy(
446
0b479d573a4c Refactoring of {site/home}_settings to make them use base.View
Sverre Rabbelier <srabbelier@gmail.com>
parents: 389
diff changeset
    53
  'Please create <a href="/user/edit">User Profile</a>'
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
  ' in order to view this page.')
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
    56
DEF_AGREE_TO_TOS_MSG_FMT = ugettext_lazy(
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
    57
  'You must agree to the <a href="%(tos_link)s">site-wide Terms of'
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
    58
  ' Service</a> in your <a href="/user/edit">User Profile</a>'
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
    59
  ' in order to view this page.')
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
    60
320
a0a86306e7f6 Some indentations fixes and ugettext_lazy() wrap up for DEF_DEV_LOGOUT_LOGIN_MSG_FMT in access.py.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 315
diff changeset
    61
DEF_DEV_LOGOUT_LOGIN_MSG_FMT = ugettext_lazy(
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
  'Please <a href="%%(sign_out)s">sign out</a>'
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
  ' and <a href="%%(sign_in)s">sign in</a>'
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    64
  ' again as %(role)s to view this page.')
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    66
DEF_PAGE_DENIED_MSG = ugettext_lazy(
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    67
  'Access to this page has been restricted')
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    68
590
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
    69
DEF_LOGOUT_MSG_FMT = ugettext_lazy(
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
    70
    'Please <a href="%(sign_out)s">sign out</a> in order to view this page')
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
    71
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
    72
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
    73
def checkAccess(access_type, request, rights, args=None, kwargs=None):
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    74
  """Runs all the defined checks for the specified type.
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    75
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    76
  Args:
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    77
    access_type: the type of request (such as 'list' or 'edit')
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    78
    request: the Django request object
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    79
    rights: a dictionary containing access check functions
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    80
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    81
  Rights usage: 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    82
    The rights dictionary is used to check if the current user is allowed 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    83
    to view the page specified. The functions defined in this dictionary 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    84
    are always called with the django request object as argument. On any 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    85
    request, regardless of what type, the functions in the 'any_access' value 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    86
    are called. If the specified type is not in the rights dictionary, all 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    87
    the functions in the 'unspecified' value are called. When the specified 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    88
    type _is_ in the rights dictionary, all the functions in that access_type's 
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    89
    value are called.
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    90
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    91
  Returns:
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    92
    True: If all the required access checks have been made successfully
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    93
    False: If a check failed, in this case self._response will contain
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
    94
      the response provided by the failed access check.
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    95
  """
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    96
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    97
  # Call each access checker
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
    98
  for check in rights['any_access']:
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
    99
    check(request, args, kwargs)
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   100
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   101
  if access_type not in rights:
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   102
    for check in rights['unspecified']:
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   103
      # No checks defined, so do the 'generic' checks and bail out
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   104
      check(request, args, kwargs)
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   105
    return
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   106
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   107
  for check in rights[access_type]:
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   108
    check(request, args, kwargs)
612
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   109
3cca81b1e5a1 Moved checkAccess and getCleanedFields into access and forms
Sverre Rabbelier <srabbelier@gmail.com>
parents: 590
diff changeset
   110
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   111
def allow(request, args, kwargs):
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   112
  """Never raises an alternate HTTP response.  (an access no-op, basically)
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   113
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   114
  Args:
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
   115
    request: a Django HTTP request
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   116
  """
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   117
  return
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   118
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   119
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   120
def deny(request, args, kwargs):
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   121
  """Always raises an alternate HTTP response.
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   122
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   123
  Args:
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
   124
    request: a Django HTTP request
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   125
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   126
  Raises:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   127
    always raises AccessViolationResponse if called
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   128
  """
927
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   129
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   130
  import soc.views.helper.responses
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   131
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   132
  if kwargs.get('SIDEBAR_CALLING', False):
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   133
    context = {}
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   134
  else:
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   135
    context = soc.views.helper.responses.getUniversalContext(request)
f790f77ba6b7 Restore context in deny() page
Sverre Rabbelier <srabbelier@gmail.com>
parents: 914
diff changeset
   136
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
   137
  context['title'] = 'Access denied'
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   138
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
   139
  raise out_of_band.AccessViolation(DEF_PAGE_DENIED_MSG, context=context)
508
2b90baceac88 Add a access.deny and access.allow method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 481
diff changeset
   140
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   141
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   142
def checkIsLoggedIn(request, args, kwargs):
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   143
  """Raises an alternate HTTP response if Google Account is not logged in.
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   144
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   145
  Args:
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
   146
    request: a Django HTTP request
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   147
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   148
  Raises:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   149
    AccessViolationResponse:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   150
    * if no Google Account is even logged in
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   151
  """
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   152
  if users.get_current_user():
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   153
    return
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   154
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
   155
  raise out_of_band.LoginRequest()
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   156
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   157
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   158
def checkNotLoggedIn(request, args, kwargs):
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   159
  """Raises an alternate HTTP response if Google Account is logged in.
590
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   160
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   161
  Args:
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   162
    request: a Django HTTP request
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   163
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   164
  Raises:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   165
    AccessViolationResponse:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   166
    * if a Google Account is currently logged in
590
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   167
  """
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   168
  if not users.get_current_user():
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   169
    return
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   170
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   171
  raise out_of_band.LoginRequest(message_fmt=DEF_LOGOUT_MSG_FMT)
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   172
37735d97b541 Created a seperate module for editSelf things
Sverre Rabbelier <srabbelier@gmail.com>
parents: 543
diff changeset
   173
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   174
def checkIsUser(request, args, kwargs):
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   175
  """Raises an alternate HTTP response if Google Account has no User entity.
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   176
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   177
  Args:
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
   178
    request: a Django HTTP request
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   179
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   180
  Raises:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   181
    AccessViolationResponse:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   182
    * if no User exists for the logged-in Google Account, or
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   183
    * if no Google Account is logged in at all
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   184
  """
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   185
  checkIsLoggedIn(request, args, kwargs)
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   186
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   187
  user = user_logic.getForFields({'account': users.get_current_user()},
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   188
                                 unique=True)
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   189
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   190
  if user:
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   191
    return
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   192
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
   193
  raise out_of_band.LoginRequest(message_fmt=DEF_NO_USER_LOGIN_MSG_FMT)
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   194
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   195
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   196
def checkAgreesToSiteToS(request, args, kwargs):
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   197
  """Raises an alternate HTTP response if User has not agreed to site-wide ToS.
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   198
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   199
  Args:
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
   200
    request: a Django HTTP request
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   201
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   202
  Raises:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   203
    AccessViolationResponse:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   204
    * if User has not agreed to the site-wide ToS, or
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   205
    * if no User exists for the logged-in Google Account, or
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   206
    * if no Google Account is logged in at all
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   207
  """
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   208
  checkIsUser(request, args, kwargs)
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   209
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   210
  user = user_logic.getForFields({'account': users.get_current_user()},
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   211
                                 unique=True)
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   212
  
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   213
  if user_logic.agreesToSiteToS(user):
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   214
    return
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   215
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   216
  # Would not reach this point of site-wide ToS did not exist, since
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   217
  # agreesToSiteToS() call above always returns True if no ToS is in effect.
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   218
  login_msg_fmt = DEF_AGREE_TO_TOS_MSG_FMT % {
891
3d40190f35b6 Move getToSLink() to soc.views.helper.redirects.getToSRedirect().
Todd Larsen <tlarsen@google.com>
parents: 890
diff changeset
   219
      'tos_link': redirects.getToSRedirect(site_logic.getSingleton())}
888
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   220
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   221
  raise out_of_band.LoginRequest(message_fmt=login_msg_fmt)
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   222
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   223
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   224
def checkIsDeveloper(request, args, kwargs):
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   225
  """Raises an alternate HTTP response if Google Account is not a Developer.
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   226
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   227
  Args:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   228
    request: a Django HTTP request
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   229
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   230
  Raises:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   231
    AccessViolationResponse:
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   232
    * if User is not a Developer, or
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   233
    * if no User exists for the logged-in Google Account, or
a75ae24f04cb Add checkAgreesToSiteToS() (partially implemented until circular import fixed).
Todd Larsen <tlarsen@google.com>
parents: 887
diff changeset
   234
    * if no Google Account is logged in at all
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   235
  """
895
e70ffd079438 Even developers need to agree to the terms of service for Melange
Sverre Rabbelier <srabbelier@gmail.com>
parents: 892
diff changeset
   236
e70ffd079438 Even developers need to agree to the terms of service for Melange
Sverre Rabbelier <srabbelier@gmail.com>
parents: 892
diff changeset
   237
  checkAgreesToSiteToS(request, args, kwargs)
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   238
481
94834a1e6c01 Attempt to rename User.id to User.account, in preparation for making User be
Todd Larsen <tlarsen@google.com>
parents: 448
diff changeset
   239
  if accounts.isDeveloper(account=users.get_current_user()):
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   240
    return
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   241
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   242
  login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
746
018efb9863dc Fix import sorting in soc.views.helper.access module and rename 'host' to 'Program Administrator'.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 729
diff changeset
   243
      'role': 'a Site Developer '}
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   244
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
   245
  raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   246
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   247
def checkCanCreateFromRequest(role_name):
940
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   248
  """Raises an alternate HTTP response if the specified request does not exist
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   249
     or if it's state is not group_accepted. 
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   250
  """
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   251
  def wrapper(request, args, kwargs):
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   252
    checkAgreesToSiteToS(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   253
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   254
    user_entity = user_logic.getForCurrentAccount()
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   255
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   256
    if user_entity.link_id != kwargs['link_id']:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   257
      deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   258
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   259
    fields = {'link_id' : kwargs['link_id'],
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   260
        'scope_path' : kwargs['scope_path'],
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   261
        'role' : role_name}
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   262
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   263
    request_entity = request_logic.getFromFieldsOr404(**fields)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   264
940
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   265
    if request_entity.state != 'group_accepted':
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   266
      # TODO tell the user that this request has not been accepted yet
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   267
      deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   268
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   269
    return
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   270
  return wrapper
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   271
940
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   272
def checkIsMyGroupAcceptedRequest(request, args, kwargs):
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   273
  """Raises an alternate HTTP response if the specified request does not exist
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   274
     or if it's state is not group_accepted
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   275
  """
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   276
  checkAgreesToSiteToS(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   277
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   278
  user_entity = user_logic.getForCurrentAccount()
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   279
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   280
  if user_entity.link_id != kwargs['link_id']:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   281
    # not the current user's request
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   282
    return deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   283
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   284
  fields = {'link_id' : kwargs['link_id'],
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   285
            'scope_path' : kwargs['scope_path'],
940
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   286
            'role' : kwargs['role']}
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   287
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   288
  request_entity = request_logic.getForFields(fields, unique=True)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   289
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   290
  if not request_entity:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   291
    # TODO return 404
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   292
    return deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   293
940
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   294
  if request_entity.state != 'group_accepted':
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   295
    return deny(request, args, kwargs)
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   296
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   297
  return
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   298
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   299
def checkIsHost(request, args, kwargs):
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   300
  """Raises an alternate HTTP response if Google Account has no Host entity.
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   301
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   302
  Args:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   303
    request: a Django HTTP request
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   304
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   305
  Raises:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   306
    AccessViolationResponse:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   307
    * if User is not already a Host, or
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   308
    * if User has not agreed to the site-wide ToS, or
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   309
    * if no User exists for the logged-in Google Account, or
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   310
    * if the user is not even logged in
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   311
  """
940
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   312
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   313
  try:
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   314
    # if the current user is invited to create a host profile we allow access
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   315
    checkIsDeveloper(request, args, kwargs)
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   316
    return
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   317
  except out_of_band.Error:
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   318
    pass
a40056afef83 Changed the access checks to comply with state in request.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 931
diff changeset
   319
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   320
  checkAgreesToSiteToS(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   321
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   322
  user = user_logic.getForFields({'account': users.get_current_user()},
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   323
                                 unique=True)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   324
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   325
  fields = {'user' : user,
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   326
            'active' : True}
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   327
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   328
  host = host_logic.getForFields(fields, unique=True)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   329
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   330
  if host:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   331
    return
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   332
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   333
  login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   334
      'role': 'a Program Administrator '}
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   335
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   336
  raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   337
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   338
def checkIsHostForProgram(request, args, kwargs):
890
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   339
  """Raises an alternate HTTP response if Google Account has no Host entity
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   340
     for the specified program.
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   341
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   342
  Args:
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   343
    request: a Django HTTP request
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   344
890
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   345
  Raises:
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   346
    AccessViolationResponse:
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   347
    * if User is not already a Host for the specified program, or
890
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   348
    * if User has not agreed to the site-wide ToS, or
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   349
    * if no User exists for the logged-in Google Account, or
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   350
    * if the user is not even logged in
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   351
  """
890
b3bf833c4580 Tighten more checkIsUser() access to checkAgreesToSiteToS() instead.
Todd Larsen <tlarsen@google.com>
parents: 889
diff changeset
   352
  checkAgreesToSiteToS(request, args, kwargs)
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   353
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   354
  user = user_logic.getForFields({'account': users.get_current_user()},
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   355
                                 unique=True)
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   356
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   357
  fields = {'user' : user,
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   358
            'scope_path' : kwargs['scope_path'],
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   359
            'active' : True}
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   360
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   361
  host = host_logic.getForFields(fields, unique=True)
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   362
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   363
  if host:
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   364
    return
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   365
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   366
  login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
746
018efb9863dc Fix import sorting in soc.views.helper.access module and rename 'host' to 'Program Administrator'.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 729
diff changeset
   367
      'role': 'a Program Administrator '}
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   368
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   369
  raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   370
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   371
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   372
def checkIsClubAdminForClub(request, args, kwargs):
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   373
  """Returns an alternate HTTP response if Google Account has no Club Admin
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   374
     entity for the specified club.
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   375
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   376
  Args:
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   377
    request: a Django HTTP request
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   378
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   379
   Raises:
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   380
     AccessViolationResponse: if the required authorization is not met
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   381
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   382
  Returns:
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   383
    None if Club Admin exists for the specified club, or a subclass of
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   384
    django.http.HttpResponse which contains the alternate response
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   385
    should be returned by the calling view.
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   386
  """
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   387
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   388
  try:
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   389
    # if the current user is invited to create a host profile we allow access
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   390
    checkIsDeveloper(request, args, kwargs)
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   391
    return
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   392
  except out_of_band.Error:
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   393
    pass
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   394
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   395
  checkAgreesToSiteToS(request, args, kwargs)
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   396
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   397
  user = user_logic.getForCurrentAccount()
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   398
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   399
  fields = {'user' : user,
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   400
            'scope_path' : kwargs['link_id'],
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   401
            'state' : 'active'}
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   402
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   403
  club_admin_entity = club_admin_logic.getForFields(fields, unique=True)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   404
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   405
  if club_admin_entity:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   406
    return
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   407
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   408
  login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   409
      'role': 'a Club Admin for this Club'}
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   410
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   411
  raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   412
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   413
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   414
def checkIsInvited(request, args, kwargs):
389
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
   415
  """Returns an alternate HTTP response if Google Account has no Host entity
9b873166d7d5 Fix identions, too long lines, unused imports and some other mistakes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 361
diff changeset
   416
     for the specified program.
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   417
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   418
  Args:
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
   419
    request: a Django HTTP request
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   420
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   421
   Raises:
639
1f92bd41b914 Some docstring fixes in few modules (soc.views.helper soc.views.models.base).
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 633
diff changeset
   422
     AccessViolationResponse: if the required authorization is not met
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   423
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   424
  Returns:
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   425
    None if Host exists for the specified program, or a subclass of
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   426
    django.http.HttpResponse which contains the alternate response
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   427
    should be returned by the calling view.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   428
  """
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   429
633
899ec5d546bd Allow developers access to anything that does a checkIsInvited.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 617
diff changeset
   430
  try:
899ec5d546bd Allow developers access to anything that does a checkIsInvited.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 617
diff changeset
   431
    # if the current user is a developer we allow access
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   432
    checkIsDeveloper(request, args, kwargs)
633
899ec5d546bd Allow developers access to anything that does a checkIsInvited.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 617
diff changeset
   433
    return
899ec5d546bd Allow developers access to anything that does a checkIsInvited.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 617
diff changeset
   434
  except out_of_band.Error:
899ec5d546bd Allow developers access to anything that does a checkIsInvited.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 617
diff changeset
   435
    pass
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   436
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   437
  checkAgreesToSiteToS(request, args, kwargs)
709
e71b20847eb0 Add checkIsHost in access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 699
diff changeset
   438
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   439
  login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
746
018efb9863dc Fix import sorting in soc.views.helper.access module and rename 'host' to 'Program Administrator'.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 729
diff changeset
   440
      'role': 'a Program Administrator for this Program'}
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   441
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   442
  splitpath = request.path.split('/')
713
bcd480745f44 Thinkofix in access.checkIsInvited
Sverre Rabbelier <srabbelier@gmail.com>
parents: 709
diff changeset
   443
  splitpath = splitpath[1:] # cut off leading ''
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   444
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   445
  if len(splitpath) < 4:
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   446
    # TODO: perhaps this needs a better explanation?
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   447
    deny(request, args, kwargs)
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   448
713
bcd480745f44 Thinkofix in access.checkIsInvited
Sverre Rabbelier <srabbelier@gmail.com>
parents: 709
diff changeset
   449
  role = splitpath[0]
bcd480745f44 Thinkofix in access.checkIsInvited
Sverre Rabbelier <srabbelier@gmail.com>
parents: 709
diff changeset
   450
  group_id = splitpath[2]
bcd480745f44 Thinkofix in access.checkIsInvited
Sverre Rabbelier <srabbelier@gmail.com>
parents: 709
diff changeset
   451
  user_id = splitpath[3]
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   452
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   453
  user = user_logic.getForFields({'account': users.get_current_user()},
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   454
                                 unique=True)
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   455
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   456
  if user_id != user.link_id:
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   457
    # TODO: perhaps this needs a better explanation?
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   458
    deny(request, args, kwargs)
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   459
525
1dc62d570eff Created a checkIsInvited method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 508
diff changeset
   460
  properties = {
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   461
      'link_id': user_id,
525
1dc62d570eff Created a checkIsInvited method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 508
diff changeset
   462
      'role': role,
617
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   463
      'scope_path': group_id,
9cc42981d40a Now invited users can create a new Host profile
Sverre Rabbelier <srabbelier@gmail.com>
parents: 612
diff changeset
   464
      'group_accepted': True,
525
1dc62d570eff Created a checkIsInvited method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 508
diff changeset
   465
      }
1dc62d570eff Created a checkIsInvited method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 508
diff changeset
   466
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   467
  request = request_logic.getForFields(properties, unique=True)
525
1dc62d570eff Created a checkIsInvited method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 508
diff changeset
   468
1dc62d570eff Created a checkIsInvited method
Sverre Rabbelier <srabbelier@gmail.com>
parents: 508
diff changeset
   469
  if request:
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   470
    return
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   471
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
   472
  raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   473
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   474
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   475
def checkIsApplicationAccepted(app_logic):
802
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   476
  """Returns an alternate HTTP response if Google Account has no Club App
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   477
     entity for the specified Club.
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   478
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   479
  Args:
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   480
    request: a Django HTTP request
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   481
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   482
   Raises:
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   483
     AccessViolationResponse: if the required authorization is not met
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   484
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   485
  Returns:
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   486
    None if Club App  exists for the specified program, or a subclass
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   487
    of django.http.HttpResponse which contains the alternate response
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   488
    should be returned by the calling view.
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   489
  """
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   490
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   491
  def wrapper(request, args, kwargs):
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   492
    try:
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   493
      # if the current user is a developer we allow access
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   494
      checkIsDeveloper(request, args, kwargs)
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   495
      return
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   496
    except out_of_band.Error:
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   497
      pass
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   498
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   499
    checkAgreesToSiteToS(request, args, kwargs)
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   500
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   501
    user = user_logic.getForCurrentAccount()
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   502
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   503
    properties = {
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   504
        'applicant': user,
892
c3cdb581ffd2 Replaced boolean properties in soc/models/group_app with status property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 891
diff changeset
   505
        'status': 'accepted'
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   506
        }
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   507
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   508
    application = app_logic.logic.getForFields(properties, unique=True)
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   509
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   510
    if application:
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   511
      return
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   512
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   513
    # TODO(srabbelier) Make this give a proper error message
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   514
    deny(request, args, kwargs)
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   515
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   516
  return wrapper
814
25ffebd9fa8f Implement the checkIsClubAppAccepted function
Sverre Rabbelier <srabbelier@gmail.com>
parents: 802
diff changeset
   517
802
95c534d02e39 Added Club Admin model, logic, and view
Sverre Rabbelier <srabbelier@gmail.com>
parents: 796
diff changeset
   518
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   519
def checkIsMyNotification(request, args, kwargs):
726
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   520
  """Returns an alternate HTTP response if this request is for a Notification belonging
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   521
     to the current user.
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   522
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   523
  Args:
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   524
    request: a Django HTTP request
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   525
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   526
   Raises:
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   527
     AccessViolationResponse: if the required authorization is not met
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   528
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   529
  Returns:
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   530
    None if the current User is allowed to access this Notification.
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   531
  """
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   532
  
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   533
  try:
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   534
    # if the current user is a developer we allow access
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   535
    checkIsDeveloper(request, args, kwargs)
726
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   536
    return
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   537
  except out_of_band.Error:
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   538
    pass
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   539
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   540
  checkAgreesToSiteToS(request, args, kwargs)
729
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   541
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   542
  # Mine the url for params
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   543
  try:
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   544
    callback, args, kwargs = urlresolvers.resolve(request.path)
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   545
  except Exception:
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   546
    deny(request, args, kwargs)
729
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   547
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   548
  properties = dicts.filter(kwargs, ['link_id', 'scope_path'])
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   549
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   550
  notification = notification_logic.getForFields(properties, unique=True)
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   551
  user = user_logic.getForCurrentAccount()
729
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   552
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   553
  # We need to check to see if the key's are equal since the User
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   554
  # objects are different and the default __eq__ method does not check
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   555
  # if the keys are equal (which is what we want).
726
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   556
  if user.key() == notification.scope.key():
ba3d399ec9be Added Notifications.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 720
diff changeset
   557
    return None
729
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   558
7fe218e3d359 Make checkIsMyInvitation use Django to parse the URL
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   559
  # TODO(ljvderijk) Make this give a proper error message
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   560
  deny(request, args, kwargs)
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   561
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   562
882
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   563
def checkIsMyApplication(app_logic):
796
126a1ef235ec Fixed a typo in the comment in helper/access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 791
diff changeset
   564
  """Returns an alternate HTTP response if this request is for a Application belonging
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   565
     to the current user.
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   566
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   567
  Args:
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   568
    request: a Django HTTP request
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   569
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   570
   Raises:
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   571
     AccessViolationResponse: if the required authorization is not met
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   572
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   573
  Returns:
796
126a1ef235ec Fixed a typo in the comment in helper/access.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 791
diff changeset
   574
    None if the current User is allowed to access this Application.
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   575
  """
882
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   576
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   577
  def wrapper(request, args, kwargs):
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   578
    try:
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   579
      # if the current user is a developer we allow access
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   580
      checkIsDeveloper(request, args, kwargs)
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   581
      return
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   582
    except out_of_band.Error:
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   583
      pass
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   584
889
5f3136a5eb4c Make checkAgreesToSiteToS() the default access check for many other checks.
Todd Larsen <tlarsen@google.com>
parents: 888
diff changeset
   585
    checkAgreesToSiteToS(request, args, kwargs)
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   586
882
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   587
    properties = dicts.filter(kwargs, ['link_id'])
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   588
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   589
    application = app_logic.logic.getForFields(properties, unique=True)
884
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   590
    
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   591
    if not application:
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   592
      deny(request, args, kwargs)
ded4850776c8 Changed checkIsClubAppAccepted into a more generic checkIsApplicationAccepted.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 882
diff changeset
   593
    
887
b8c1a6bc913e Take advantage of new "from ... import ... as ..." rules in the style guide.
Todd Larsen <tlarsen@google.com>
parents: 884
diff changeset
   594
    user = user_logic.getForCurrentAccount()
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   595
882
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   596
    # We need to check to see if the key's are equal since the User
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   597
    # objects are different and the default __eq__ method does not check
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   598
    # if the keys are equal (which is what we want).
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   599
    if user.key() == application.applicant.key():
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   600
      return None
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   601
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   602
    # TODO(srabbelier) Make this give a proper error message
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   603
    deny(request, args, kwargs)
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   604
882
267e31f1a0b6 Added club_app model and logic.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 872
diff changeset
   605
  return wrapper
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   606
914
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   607
def checkIsMyActiveRole(role_logic):
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   608
  """Returns an alternate HTTP response if there is no active role found for
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   609
     the current user using the given role_logic.
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   610
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   611
   Raises:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   612
     AccessViolationResponse: if the required authorization is not met
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   613
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   614
  Returns:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   615
    None if the current User has no active role for the given role_logic.
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   616
  """
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   617
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   618
  def wrapper(request, args, kwargs):
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   619
    try:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   620
      # if the current user is a developer we allow access
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   621
      checkIsDeveloper(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   622
      return
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   623
    except out_of_band.Error:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   624
      pass
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   625
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   626
    user = user_logic.getForCurrentAccount()
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   627
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   628
    if not user or user.link_id != kwargs['link_id']:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   629
      # not my role
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   630
      deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   631
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   632
    fields = {'link_id' : kwargs['link_id'],
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   633
              'scope_path' : kwargs['scope_path']
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   634
              }
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   635
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   636
    role_entity = role_logic.logic.getForFields(fields, unique=True)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   637
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   638
    if not role_entity:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   639
      # no role found
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   640
      deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   641
      
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   642
    if role_entity.state == 'active':
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   643
      # this role exist and is active
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   644
      return
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   645
    else:
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   646
      # this role is not active
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   647
      deny(request, args, kwargs)
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   648
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   649
  return wrapper
6ec8dd2a73b3 Added various access methods in preperation for the new request system.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 895
diff changeset
   650
791
30da180c4bca Added the club_app view, logic and model
Sverre Rabbelier <srabbelier@gmail.com>
parents: 746
diff changeset
   651
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   652
def checkCanInvite(request, args, kwargs):
746
018efb9863dc Fix import sorting in soc.views.helper.access module and rename 'host' to 'Program Administrator'.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 729
diff changeset
   653
  """Checks to see if the current user can create an invite.
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   654
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   655
  Note that if the current url is not in the default 'request' form
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   656
  this method either deny()s or performs the wrong access check.
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   657
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   658
  Args:
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   659
    request: a Django HTTP request
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   660
  """
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   661
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   662
  try:
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   663
    # if the current user is a developer we allow access
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   664
    checkIsDeveloper(request, args, kwargs)
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   665
    return
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   666
  except out_of_band.Error:
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   667
    pass
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   668
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   669
  # Mine the url for params
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   670
  try:
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   671
    callback, args, kwargs = urlresolvers.resolve(request.path)
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   672
  except Exception:
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   673
    deny(request, args, kwargs)
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   674
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   675
  # Construct a new url by reshufling the kwargs
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   676
  order = ['role', 'access_type', 'scope_path', 'link_id']
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   677
  url_params = dicts.unzip(kwargs, order)
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   678
  url = '/'.join([''] + list(url_params))
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   679
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   680
  # Mine the reshufled url
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   681
  try:
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   682
    callback, args, kwargs = urlresolvers.resolve(url)
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   683
  except Exception:
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   684
    deny(request, args, kwargs)
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   685
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   686
  # Get the everything we need for the access check
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   687
  params = callback.im_self.getParams()
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   688
  access_type = kwargs['access_type']
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   689
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   690
  # Perform the access check
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   691
  checkAccess(access_type, request, rights=params['rights'])
720
9eb2522dfa83 Make it possible to invite another Host as Host
Sverre Rabbelier <srabbelier@gmail.com>
parents: 714
diff changeset
   692
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   693
931
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   694
def checkHasPickGetArgs(request, arg, kwargs):
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   695
  """Raises an alternate HTTP response if the request misses get args
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   696
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   697
  Args:
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   698
    request: a Django HTTP request
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   699
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   700
  Raises:
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   701
    AccessViolationResponse:
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   702
    * if continue is not in request.GET
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   703
    * if field is not in request.GET
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   704
  """
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   705
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   706
  get_args = request.GET
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   707
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   708
  if 'continue' in get_args and 'field' in get_args:
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   709
    return
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   710
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   711
  #TODO(SRabbelier) inform user that return_url and field are required
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   712
  deny(request, arg, kwargs)
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   713
1131884c3c56 Add a simple access check for a picker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 927
diff changeset
   714
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   715
def checkIsDocumentPublic(request, args, kwargs):
714
3e2ce3d8057a Add missing dots in docstrings, proper sorting of imports and small docstring typo fixes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 713
diff changeset
   716
  """Checks whether a document is public.
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   717
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   718
  Args:
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   719
    request: a Django HTTP request
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   720
  """
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   721
714
3e2ce3d8057a Add missing dots in docstrings, proper sorting of imports and small docstring typo fixes.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 713
diff changeset
   722
  # TODO(srabbelier): A proper check needs to be done to see if the document
699
4e8eefe95748 Add and use a placeholder for checkIsDocumentPublic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 639
diff changeset
   723
  # is public or not, probably involving analysing it's scope or such.
872
70e0b6d8ff73 Prepare access to receive args and kwargs as argument
Sverre Rabbelier <srabbelier@gmail.com>
parents: 814
diff changeset
   724
  allow(request, args, kwargs)