django-extauth / extauth / permissions.py

'''
Copyright 2009 H. Lee Moffitt Cancer Center and Research Institute, Inc. 
All rights reserved.

@author: Michael Hall <mhall119@gmail.com>
'''
from extauth import session

ALL_USERS = '*'

def has_perm(perm, user=None):
    if perm == ALL_USERS:
        return True
    
    if user is None:
        user = session.get_current_user()

    if user is None:
        return False
    else:
        return user.has_perm(perm)

def require_perm(perm, user=None):
    if user is None:
        user = session.get_current_user()

    if has_perm(perm, user):
        return True
    else:
        if user is None or not user.is_authenticated():
            raise LoginRequiredException(user, perm, None)
        else:
            raise InsufficientPermsException(user, perm, None)
        
def get_add_permission(model_class):
    return '%s.add_%s' % (model_class._meta.app_label, model_class._meta.object_name.lower())

def get_read_permission(model_class):
    return '%s.read_%s' % (model_class._meta.app_label, model_class._meta.object_name.lower())

def get_change_permission(model_class):
    return '%s.change_%s' % (model_class._meta.app_label, model_class._meta.object_name.lower())

def get_delete_permission(model_class):
    return '%s.delete_%s' % (model_class._meta.app_label, model_class._meta.object_name.lower())

def get_modify_permission(model_class, field_name):
    return '%s.modify_%s__%s' % (model_class._meta.app_label, model_class._meta.object_name.lower(), field_name)

def get_view_permission(model_class, field_name):
    return '%s.view_%s__%s' % (model_class._meta.app_label, model_class._meta.object_name.lower(), field_name)

class PermissionException(Exception):
    def __init__(self, user, node, perm, msg):
        self.user = user
        self.node = node
        self.msg = msg
        
    def __str__(self):
        return repr(self.msg)

class InsufficientPermsException(PermissionException):
    
    def __init__(self, user, perm, node):
        self.user = user
        self.perm = perm
        self.node = node
        self.msg = "User %s does not have permission to access this page: %s" % (user, perm)

class LoginRequiredException(PermissionException):
    def __init__(self, user, perm, node):
        self.user = user
        self.perm = perm
        self.node = node
        self.msg = "User must be logged in to access this page"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.