示例#1
0
文件: exc.py 项目: blaflamme/ptah
    def update(self):
        request = self.request

        context = getattr(request, 'context', None)
        if context is None:
            context = getattr(request, 'root', None)

        if context is None:
            root_factory = config.registry.queryUtility(
                IRootFactory, default=DefaultRootFactory)
            context = root_factory(request)
            request.root = context

        self.__parent__ = context

        user = authService.get_userid()
        if user is None:
            loginurl = PTAH_CONFIG.login
            if loginurl and not loginurl.startswith(('http://', 'https://')):
                loginurl = request.application_url + loginurl
            elif not loginurl:
                loginurl = request.application_url + '/login.html'

            location = '%s?%s'%(
                loginurl, urllib.urlencode({'came_from': request.url}))
            if isinstance(location, unicode):
                location = location.encode('utf-8')

            request.response.status = HTTPFound.code
            request.response.headers['location'] = location
            return

        self.request.response.status = HTTPForbidden.code
示例#2
0
文件: security.py 项目: mcdonc/ptah
def checkPermission(permission, context, request=None, throw=False):
    """ Check `permission` withing `context`.

    :param permission: Permission
    :type permission: (Permission or sting)
    :param context: Context object
    :param throw: Throw HTTPForbidden exception.
    """

    if not permission or permission == NO_PERMISSION_REQUIRED:
        return True
    if permission == NOT_ALLOWED:
        if throw:
            raise HTTPForbidden()
        return False

    userid = authService.get_userid()
    if userid == SUPERUSER_URI:
        return True

    global AUTHZ
    try:
        AUTHZ
    except:
        AUTHZ = config.registry.getUtility(IAuthorizationPolicy)

    principals = [Everyone.id]

    if userid is not None:
        principals.extend((Authenticated.id, userid))

        roles = LocalRoles(userid, context=context)
        if roles:
            principals.extend(roles)

    res = AUTHZ.permits(context, principals, permission)

    if isinstance(res, ACLDenied):
        if throw:
            raise HTTPForbidden(res)

        return False
    return True
示例#3
0
文件: exc.py 项目: blaflamme/ptah
 def update(self):
     self.root = getattr(self.request, 'root', None)
     self.user = authService.get_current_principal()
     self.isanon = self.user is None
     self.ptahmanager = get_access_manager()(authService.get_userid())