ip_range.py 1.61 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - CAS authentication

    Jasig CAS (see http://www.jasig.org/cas) authentication module.

    @copyright: 2012 MoinMoin:RichardLiao
    @license: GNU GPL, see COPYING for details.
"""

import urlparse
from netaddr import IPNetwork, IPAddress

from MoinMoin.auth import BaseAuth
15
from MoinMoin.Page import Page
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
from MoinMoin import user
from anonymous_user import AnonymousAuth

class IpRange(AnonymousAuth):
    name = 'IpRange'

    def __init__(self, auth_username="Connexion", local_nets=[], actions=[], actions_msg={}):
        AnonymousAuth.__init__(self, auth_username=auth_username)
        self.local_nets = local_nets;
        self.actions = actions
        self.actions_msg=actions_msg

    def can_view(self, request):
        try:
            for net in self.local_nets:
                if IPAddress(request.remote_addr) in IPNetwork(net):
                    return True
        except:
            pass
        return False

    def request(self, request, user_obj, **kw):
        user_obj, cont = AnonymousAuth.request(self, request, user_obj, **kw)
39
        if  not user_obj or not user_obj.valid:
40 41 42 43 44 45 46 47 48
            # Are we trying to do a protected action (eg newaccount)
            action = request.args.get("action", "")
            if action in self.actions:
                if action in self.actions_msg.keys():
                    request.theme.add_msg(self.actions_msg[action])
                p = urlparse.urlparse(request.url)
                url = urlparse.urlunparse(('https', p.netloc, p.path, "", "", ""))
                request.http_redirect(url)

49
        return user_obj, True
50 51