app/soc/logic/rights.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 24 Oct 2009 16:23:31 -0700
changeset 3043 187c1709756b
parent 2956 50ce8ac13932
permissions -rw-r--r--
GSoC Views
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
2956
50ce8ac13932 Use the new rights code in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1528
diff changeset
    32
  def __init__(self, rights, prefix):
1143
b07b7d5b3e27 Fix missing dots and blank lines to soc.logic modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1136
diff changeset
    33
    """Constructs a Checker for the specified prefix.
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    34
    """
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    35
2956
50ce8ac13932 Use the new rights code in core
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1528
diff changeset
    36
    self.rights = rights[prefix]
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    37
    self.prefix = prefix
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
  def getMembership(self, status):
1136
aaf75aa8eca5 Added two forgotten comments in rights.Checker
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1133
diff changeset
    40
    """Retrieves the membership list for the specified status.
1133
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    41
    """
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    42
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    43
    if status == 'user':
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    44
      return ['user']
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
    if status == 'public':
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    47
      return ['anyone']
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    48
4bb31d9a58e0 Added a rights module
Sverre Rabbelier <srabbelier@gmail.com>
parents:
diff changeset
    49
    return self.rights[status]
1299
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
    50
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
    51
  def getMemberships(self):
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
    52
    """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
    53
    """
e209bda5addb Add a getMemberships method to logic/rights.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1143
diff changeset
    54
1318
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    55
    extra_rights = {
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    56
        'user': ['user'],
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    57
        'public': ['anyone'],
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    58
        'list': [],
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    59
        }
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    60
3f41f33a4ad2 Add custom access check for document listing
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1314
diff changeset
    61
    return dicts.merge(extra_rights, self.rights)