app/soc/logic/out_of_band.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 09 Oct 2008 23:38:27 +0000
changeset 293 1edd01373e71
parent 111 f506a22f50db
child 303 4f1bb54ddae5
permissions -rw-r--r--
Add an access control module The module's purpose is to check access requirements, (such as being logged in), and raise an exception containing a authentication request when the requirement is not met. Patch by: Sverre Rabbelier Reviewed by: Pawel Solyga, Augie Fackler, Todd Larsen Reviewed at: http://codereviews.googleopensourceprograms.com/1601 Review id: 1601
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
111
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     2
#
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     4
#
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     8
#
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    10
#
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    15
# limitations under the License.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    16
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    17
"""Out-of-band responses to render instead of the usual HTTP response.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    18
"""
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    19
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    20
__authors__ = [
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    21
  '"Todd Larsen" <tlarsen@google.com>',
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    22
  ]
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    23
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    24
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    25
class OutOfBandResponse(Exception):
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    26
  """Base exception for out-of-band responses raised by controller logic.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    27
  """
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    28
  pass
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    29
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    30
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    31
class ErrorResponse(OutOfBandResponse):
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    32
  """Out-of-band response when controller logic needs a special error page.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    33
  """
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    34
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    35
  def __init__(self, message, **response_args):
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    36
    """Constructor used to set error message and HTTP response arguments.
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    37
  
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    38
    Args:
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    39
      message: error message to display on the error page
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    40
      **response_args: keyword arguments that are supplied directly to
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    41
        django.http.HttpResponse; the most commonly used is 'status' to
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    42
        set the HTTP status code for the response
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    43
    """
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    44
    self.message = message
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    45
    self.response_args = response_args
f506a22f50db Exception classes used by controller logic to request an "out-of-band" response
Todd Larsen <tlarsen@google.com>
parents:
diff changeset
    46
293
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    47
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    48
class AccessViolationResponse(OutOfBandResponse):
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    49
  """"Out of band response when an access requirement was not met.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    50
  """
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    51
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    52
  def __init__(self, response):
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    53
    """Constructor used to set response message \.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    54
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    55
    Args:
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    56
      response: The response that should be returned to the user.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    57
    """
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    58
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    59
    self._response = response
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    60
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    61
  def response(self):
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    62
    """Returns the response that was set in the constructor.
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    63
    """
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    64
1edd01373e71 Add an access control module
Sverre Rabbelier <srabbelier@gmail.com>
parents: 111
diff changeset
    65
    return self._response