Source

HackersEdge / he_one / middleware.py

Full commit
from django.core.cache import cache
from he_one.models import UserProfile, Host, NameSystem, FilePermission, Task
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.contrib.auth.models import User
from django.shortcuts import redirect
from django.template.defaultfilters import slugify
import datetime

class ProfileMiddleware(object):
    def process_request(self, req):
        if not req.user.is_authenticated():
            if req.path[0:8] == '/he_one/' or req.path == '/Tutorial':
                return HttpResponseRedirect('/accounts/login/?next=%s' % req.path)
            return None
        profile = cache.get('profile-%s' % req.user.pk)
        if profile == None:
            try:
                profile = UserProfile.objects.get(user=req.user)
            except UserProfile.DoesNotExist:
                profile = UserProfile(user=req.user, tutorial=0, exp=0, cash=0)
                profile.save()
            cache.set('profile-%s' % req.user.pk, profile)
        if req.path[0:8] != '/he_one/' and req.path != '/Tutorial':
            return None
        if profile.tutorial == 1:
            nobody = User.objects.get(username='nobody')
            try:
                host = Host.objects.filter(owner=nobody)[0]
                host.owner = req.user
                host.save()
                messages.info(req, "You have been assigned a host!")
                gwdns = NameSystem.objects.get(host=host.gateway)
                NameSystem.objects.create(host=host, hostname=req.user.username, domain=gwdns.domain, slug=slugify("%s-%s" % (req.user.username, gwdns.domain)))
                FilePermission.objects.filter(host=host).update(creator=req.user)
                profile.tutorial += 1
                profile.home_host = host
                profile.save()
                return redirect(host)
            except IndexError:
                messages.info(req, "No hosts are available at this time.")
        if profile.tutorial < 1:
            if req.path != '/Tutorial':
                messages.info(req, "Welcome to Hacker's Edge %s!" % req.user.username)
                return HttpResponseRedirect('/Tutorial')
            profile.tutorial += 1
            profile.save()

class TaskMiddleware(object):
    def process_request(self, req):
        if not req.user.is_authenticated():
            return None
        for task in Task.objects.filter(owner=req.user, end_time__lt=datetime.datetime.now()):
            if task.content_type.model == 'filepermission':
                pm = task.target
                pcfile = pm.pcfile
                if task.host == task.target_host:
                    pm.deleted = True
                    pm.save()
                    task.target_host.logs.create(by_who=req.user, message="Deleted file %s" % pcfile, logged_at=task.end_time)
                    messages.success(req, "%s has been deleted." % pcfile)
                else:
                    if task.is_download:
                        FilePermission.objects.create(host=task.host, pcfile=pcfile, creator=pm.creator, create_date=pm.create_date, patch=pm.patch)
                        task.target_host.logs.create(by_who=req.user, message="Downloaded %s to %s" % (pcfile, task.host), logged_at=task.end_time)
                        messages.info(req, "%s has been downloaded." % pcfile)
                        if task.is_delete:
                            pm.deleted = True
                            pm.save()
                            task.target_host.logs.create(by_who=req.user, message="Deleted file %s" % (pcfile), logged_at=task.end_time)
                    else:
                        FilePermission.objects.create(host=task.target_host, pcfile=pcfile, creator=pm.creator, create_date=pm.create_date, patch=pm.patch)
                        task.target_host.logs.create(by_who=req.user, message="Uploaded %s from %s" % (pcfile, task.host), logged_at=task.end_time)
                        messages.info(req, "%s has been uploaded." % pcfile)
                        if task.is_delete:
                            pm.deleted = True
                            pm.save()
                            task.host.logs.create(by_who=req.user, message="Deleted file %s" % (pcfile), logged_at=task.end_time)
            elif task.content_type.model == 'log':
                pclog = task.target
                pclog.deleted = True
                pclog.save()
                messages.success(req, "Log has been deleted from %s" % task.target_host)
            task.delete()

class ToolsMiddleware(object):
    def process_request(self, req):
        if not req.user.is_authenticated():
            return None
        profile = cache.get('profile-%s' % req.user.pk)
        if profile == None:
            return None
        user_tools = cache.get('usertools-%s' % req.user.pk)
        if user_tools == None:
            user_tools = []
            host = profile.home_host
            if host == None:
                return None
            for pcfile in host.files.filter(file_type='PRG', filepermission__deleted=False):
                user_tools.append(pcfile.filename)
        print user_tools
        cache.set('usertools-%s' % req.user.pk, user_tools)
        req.user_tools = user_tools