Source

HackersEdge / he_one / admin_views.py

Full commit
Kevin Veroneau 8d6c3a4 





























































from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from he_one.models import Host, PCFile, FilePermission, NameSystem, Mission
import datetime
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect, get_object_or_404
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

@login_required
def bulk_create_hosts(req, network, low_range, high_range):
    if not req.user.is_superuser:
        raise PermissionDenied
    owner = User.objects.get(username='nobody')
    gw = Host.objects.get(ip="%s.1" % network)
    krnl = PCFile.objects.get(filename='KERNEL', file_type='SYS')
    for i in range(int(low_range), int(high_range)):
        ip = "%s.%s" % (network, i)
        host = Host.objects.create(ip=ip, gateway=gw, owner=owner)
        FilePermission.objects.create(pcfile=krnl, host=host, creator=owner, create_date=datetime.datetime.now(), is_hidden=True, patch=0)
    return HttpResponse("Job completed.")

@login_required
def admin_host(req, ip):
    if not req.user.is_superuser:
        raise PermissionDenied
    host = get_object_or_404(Host, ip=ip)
    return redirect('admin:he_one_host_change', str(host.pk))

class AdminMixin(object):
    def get(self, request, *args, **kwargs):
        if not request.user.is_superuser:
            raise PermissionDenied
        return super(AdminMixin, self).get(request, *args, **kwargs)

class ResolveDNS(AdminMixin, DetailView):
    model = NameSystem
    def get_object(self, queryset=None):
        return self.get_queryset().get(hostname=self.kwargs['hostname'], domain=self.kwargs['domain'])

class ShowDomain(AdminMixin, ListView):
    model = NameSystem
    def get_context_data(self, **kwargs):
        context = super(ShowDomain, self).get_context_data(**kwargs)
        context.update({'domain':self.kwargs['domain']})
        return context
    def get_queryset(self):
        return super(ShowDomain, self).get_queryset().filter(domain=self.kwargs['domain']).select_related()

class UserInfo(AdminMixin, DetailView):
    model = User
    template_name = "he_one/user_info.html"
    def get_object(self, queryset=None):
        return self.get_queryset().get(username=self.kwargs['username'])

class MissionList(AdminMixin, ListView):
    model = Mission

class MissionDetail(AdminMixin, DetailView):
    model = Mission
    def get_queryset(self):
        return super(MissionDetail, self).get_queryset().select_related()