app/soc/views/helper/access.py
changeset 709 e71b20847eb0
parent 699 4e8eefe95748
child 713 bcd480745f44
equal deleted inserted replaced
708:89f1a8c2ea59 709:e71b20847eb0
    33 from google.appengine.api import users
    33 from google.appengine.api import users
    34 
    34 
    35 from django.utils.translation import ugettext_lazy
    35 from django.utils.translation import ugettext_lazy
    36 
    36 
    37 from soc.logic import accounts
    37 from soc.logic import accounts
       
    38 from soc.logic.models import host as host_logic
    38 from soc.logic.models import user as user_logic
    39 from soc.logic.models import user as user_logic
    39 from soc.logic.models import request as request_logic
    40 from soc.logic.models import request as request_logic
    40 from soc.views import helper
    41 from soc.views import helper
    41 from soc.views import out_of_band
    42 from soc.views import out_of_band
    42 
    43 
   113   Returns: 
   114   Returns: 
   114     a subclass of django.http.HttpResponse which contains the
   115     a subclass of django.http.HttpResponse which contains the
   115     alternate response that should be returned by the calling view.
   116     alternate response that should be returned by the calling view.
   116   """
   117   """
   117 
   118 
   118   context = helper.responses.getUniversalContext(request)
   119   context = {}
   119   context['title'] = 'Access denied'
   120   context['title'] = 'Access denied'
   120 
   121 
   121   raise out_of_band.AccessViolation(DEF_PAGE_DENIED_MSG, context=context)
   122   raise out_of_band.AccessViolation(DEF_PAGE_DENIED_MSG, context=context)
   122 
   123 
   123 
   124 
   213       'role': 'a site developer '}
   214       'role': 'a site developer '}
   214 
   215 
   215   raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
   216   raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
   216 
   217 
   217 
   218 
       
   219 def checkIsHost(request):
       
   220   """Returns an alternate HTTP response if Google Account has no Host entity
       
   221      for the specified program.
       
   222 
       
   223   Args:
       
   224     request: a Django HTTP request
       
   225 
       
   226    Raises:
       
   227      AccessViolationResponse: if the required authorization is not met
       
   228 
       
   229   Returns:
       
   230     None if Host exists for the specified program, or a subclass of
       
   231     django.http.HttpResponse which contains the alternate response
       
   232     should be returned by the calling view.
       
   233   """
       
   234 
       
   235   try:
       
   236     # if the current user is a developer we allow access
       
   237     checkIsInvited(request)
       
   238     return
       
   239   except out_of_band.Error:
       
   240     pass
       
   241 
       
   242   checkIsUser(request)
       
   243 
       
   244   user = user_logic.logic.getForFields(
       
   245       {'account': users.get_current_user()}, unique=True)
       
   246 
       
   247   host = host_logic.logic.getForFields(
       
   248       {'user': user}, unique=True)
       
   249 
       
   250   if host:
       
   251     return
       
   252 
       
   253   login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
       
   254       'role': 'a host '}
       
   255 
       
   256   raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
       
   257 
       
   258 
   218 def checkIsInvited(request):
   259 def checkIsInvited(request):
   219   """Returns an alternate HTTP response if Google Account has no Host entity
   260   """Returns an alternate HTTP response if Google Account has no Host entity
   220      for the specified program.
   261      for the specified program.
   221 
   262 
   222   Args:
   263   Args:
   229     None if Host exists for the specified program, or a subclass of
   270     None if Host exists for the specified program, or a subclass of
   230     django.http.HttpResponse which contains the alternate response
   271     django.http.HttpResponse which contains the alternate response
   231     should be returned by the calling view.
   272     should be returned by the calling view.
   232   """
   273   """
   233 
   274 
   234   checkIsUser(request)
       
   235   
       
   236   try:
   275   try:
   237     # if the current user is a developer we allow access
   276     # if the current user is a developer we allow access
   238     checkIsDeveloper(request)  
   277     checkIsDeveloper(request)
   239     return
   278     return
   240   except out_of_band.Error:
   279   except out_of_band.Error:
   241     pass
   280     pass
   242   
   281 
       
   282   checkIsUser(request)
       
   283 
   243   login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
   284   login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
   244       'role': 'a host for this program'}
   285       'role': 'a host for this program'}
   245 
   286 
   246   splitpath = request.path.split('/')
   287   splitpath = request.path.split('/')
   247 
   288