app/soc/views/helper/access.py
changeset 1074 94bc2a9ae103
parent 1073 feea88d0e1d8
child 1080 d533408811ba
equal deleted inserted replaced
1073:feea88d0e1d8 1074:94bc2a9ae103
    44 from soc.logic.models.notification import logic as notification_logic
    44 from soc.logic.models.notification import logic as notification_logic
    45 from soc.logic.models.request import logic as request_logic
    45 from soc.logic.models.request import logic as request_logic
    46 from soc.logic.models.role import logic as role_logic
    46 from soc.logic.models.role import logic as role_logic
    47 from soc.logic.models.site import logic as site_logic
    47 from soc.logic.models.site import logic as site_logic
    48 from soc.logic.models.user import logic as user_logic
    48 from soc.logic.models.user import logic as user_logic
       
    49 from soc.logic.models.program import logic as program_logic
    49 from soc.views import helper
    50 from soc.views import helper
    50 from soc.views import out_of_band
    51 from soc.views import out_of_band
    51 from soc.views.helper import redirects
    52 from soc.views.helper import redirects
    52 
    53 
    53 
    54 
   565       * if the user is not even logged in
   566       * if the user is not even logged in
   566     """
   567     """
   567 
   568 
   568     self.checkIsUser(django_args)
   569     self.checkIsUser(django_args)
   569 
   570 
   570     user = user_logic.getForCurrentAccount()
   571     scope_path = None
   571 
   572 
   572     if django_args.get('scope_path'):
   573     if 'scope_path' in django_args:
   573       scope_path = django_args['scope_path']
   574       scope_path = django_args['scope_path']
   574     else:
   575     if 'link_id' in django_args:
   575       scope_path = django_args['link_id']
   576       scope_path = django_args['link_id']
   576 
   577 
   577     fields = {'user': user,
   578     fields = {'user': self.user,
   578               'scope_path': scope_path,
       
   579               'state': 'active'}
   579               'state': 'active'}
   580 
   580 
   581     host = host_logic.getForFields(fields, unique=True)
   581     if scope_path:
   582 
   582       fields['scope_path'] = scope_path
   583     self.checkIsUser(django_args)
       
   584 
       
   585     user = user_logic.getForCurrentAccount()
       
   586 
       
   587     fields = {'user': user,
       
   588               'state': 'active'}
       
   589 
   583 
   590     host = host_logic.getForFields(fields, unique=True)
   584     host = host_logic.getForFields(fields, unique=True)
   591 
   585 
   592     if host:
   586     if host:
   593       return
   587       return
   594 
   588 
   595     login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
   589     login_message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
   596         'role': 'a Program Administrator '}
   590         'role': 'a Program Administrator '}
   597 
   591 
   598     raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
   592     raise out_of_band.LoginRequest(message_fmt=login_message_fmt)
       
   593 
       
   594   @denySidebar
       
   595   @allowDeveloper
       
   596   def checkIsHostForProgram(self, django_args):
       
   597     """Checks if the user is a host for the specified program.
       
   598     """
       
   599 
       
   600     key_fields = program_logic.getKeyFieldsFromDict(django_args)
       
   601     program = program_logic.getFromFields(**key_fields)
       
   602 
       
   603     if not program:
       
   604       self.deny(django_args)
       
   605 
       
   606     new_args = { 'scope_path': program.scope_path }
       
   607     self.checkIsHost(new_args)
   599 
   608 
   600   @allowDeveloper
   609   @allowDeveloper
   601   def checkIsHostForSponsor(self, django_args):
   610   def checkIsHostForSponsor(self, django_args):
   602     """Raises an alternate HTTP response if Google Account has no Host entity
   611     """Raises an alternate HTTP response if Google Account has no Host entity
   603        for the specified Sponsor.
   612        for the specified Sponsor.
   878     """
   887     """
   879 
   888 
   880     # TODO(srabbelier): A proper check needs to be done to see if the document
   889     # TODO(srabbelier): A proper check needs to be done to see if the document
   881     # is public or not, probably involving analysing it's scope or such.
   890     # is public or not, probably involving analysing it's scope or such.
   882     self.allow(django_args)
   891     self.allow(django_args)
       
   892 
       
   893   @allowIfCheckPasses('checkIsHost')
       
   894   def checkIsProgramActive(self, django_args):
       
   895     """Checks whether a program is active
       
   896     """
       
   897 
       
   898     if 'entity' in django_args:
       
   899       program = django_args['entity']
       
   900     else:
       
   901       key_fields = program_logic.getKeyFieldsFromDict(django_args)
       
   902       program = program_logic.getFromFields(**key_fields)
       
   903 
       
   904     if not program:
       
   905       self.deny(django_args)
       
   906 
       
   907     if program.is_enabled:
       
   908       return
       
   909 
       
   910     context = django_args.get('context', {})
       
   911     context['title'] = 'Access denied'
       
   912 
       
   913     message_fmt = DEF_DEV_LOGOUT_LOGIN_MSG_FMT % {
       
   914         'role': ugettext('a Program Administrator')}
       
   915 
       
   916     raise out_of_band.AccessViolation(DEF_DEV_LOGOUT_LOGIN_MSG_FMT,
       
   917                                       context=context)