app/soc/logic/rights.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 12 Sep 2009 14:42:37 +0200
changeset 2906 6fb53ed7aff4
parent 1528 abbdf42ab322
child 2956 50ce8ac13932
permissions -rw-r--r--
Add docstring to middleware hooks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     2
#
1307
091a21cf3627 Update the copyright notice for 2009.
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1299
diff changeset
     3
# Copyright 2009 the Melange authors.
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     4
#
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     8
#
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    10
#
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    16
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    17
"""Module with rights related methods.
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    18
"""
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    19
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    20
__authors__ = [
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    21
  '"Sverre Rabbelier" <sverre@rabbelier.nl>',
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    22
  ]
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    23
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    24
1299
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
    25
from soc.logic import dicts
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
    26
1353
14a024c71415 Add missing blank line in soc.logic.rights module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1318
diff changeset
    27
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    28
class Checker(object):
1136
aaf75aa8eca5 Added two forgotten comments in rights.Checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1133
diff changeset
    29
  """Checker class that maps from prefix and status to membership.
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    30
  """
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    31
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    32
  SITE_MEMBERSHIP = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    33
      'admin': [],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
      'restricted': ['host'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
      'member': ['user'],
1528
abbdf42ab322 Allow to do 'checkHasActiveRole' checks for document access
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1353
diff changeset
    36
      'list': ['host'],
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    38
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    39
  CLUB_MEMBERSHIP = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    40
      'admin': ['host', 'club_admin'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
      'restricted': ['host', 'club_admin'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
      'member': ['host', 'club_admin', 'club_member'],
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    43
      'list': ['host', 'club_admin', 'club_member'],
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    45
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    46
  SPONSOR_MEMBERSHIP = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
      'admin': ['host'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
      'restricted': ['host'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
      'member': ['host'],
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    50
      'list': ['host'],
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    51
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    52
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    53
  PROGRAM_MEMBERSHIP = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    54
      'admin': ['host'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    55
      'restricted': ['host', 'org_admin'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    56
      'member': ['host', 'org_admin', 'org_mentor', 'org_student'],
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    57
      'list': ['host', 'org_admin', 'org_mentor'],
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    58
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    59
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    60
  ORGANIZATION_MEMBERSHIP = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    61
      'admin': ['host', 'org_admin'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    62
      'restricted': ['host', 'org_admin', 'org_mentor'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    63
      'member': ['host', 'org_admin', 'org_mentor', 'org_student'],
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    64
      'list': ['host', 'org_admin', 'org_mentor'],
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    65
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    66
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    67
  USER_MEMBERSHIP = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    68
      'admin': ['user_self'],
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    69
      'restricted': ['user_self'], # ,'friends'
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    70
      'member': ['user'],
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    71
      'list': ['user_self'],
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    72
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    73
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    74
  RIGHTS = {
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    75
      'site': SITE_MEMBERSHIP,
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    76
      'club': CLUB_MEMBERSHIP,
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    77
      'sponsor': SPONSOR_MEMBERSHIP,
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    78
      'program': PROGRAM_MEMBERSHIP,
1314
b832e02d70c1 Organization prefix was renamed to 'org'
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
    79
      'org': ORGANIZATION_MEMBERSHIP,
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    80
      'user': USER_MEMBERSHIP,
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    81
      }
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    82
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    83
  def __init__(self, prefix):
1143
b07b7d5b3e27 Fix missing dots and blank lines to soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1136
diff changeset
    84
    """Constructs a Checker for the specified prefix.
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    85
    """
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    86
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    87
    self.prefix = prefix
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    88
    self.rights = self.RIGHTS[prefix]
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    89
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    90
  def getMembership(self, status):
1136
aaf75aa8eca5 Added two forgotten comments in rights.Checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1133
diff changeset
    91
    """Retrieves the membership list for the specified status.
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    92
    """
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    93
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    94
    if status == 'user':
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    95
      return ['user']
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    96
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    97
    if status == 'public':
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    98
      return ['anyone']
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    99
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
   100
    return self.rights[status]
1299
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
   101
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
   102
  def getMemberships(self):
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
   103
    """Returns all memberships for the configured prefix.
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
   104
    """
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
   105
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   106
    extra_rights = {
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   107
        'user': ['user'],
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   108
        'public': ['anyone'],
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   109
        'list': [],
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   110
        }
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   111
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
   112
    return dicts.merge(extra_rights, self.rights)