2018-06-30 01:25:46 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2018-03-17 17:20:31 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
|
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Maël Kervella
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Defines the views of the API
|
2018-03-17 17:20:31 +00:00
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
All views inherits the `rest_framework.views.APIview` to respect the
|
|
|
|
REST API requirements such as dealing with HTTP status code, format of
|
|
|
|
the response (JSON or other), the CSRF exempting, ...
|
2018-03-17 17:20:31 +00:00
|
|
|
"""
|
2018-03-17 17:50:03 +00:00
|
|
|
|
2018-04-21 19:48:33 +00:00
|
|
|
import datetime
|
2018-03-17 17:50:03 +00:00
|
|
|
|
2018-04-21 19:48:33 +00:00
|
|
|
from django.conf import settings
|
2018-08-31 11:26:54 +00:00
|
|
|
from django.db.models import Q
|
2018-09-24 17:14:46 +00:00
|
|
|
from rest_framework import viewsets, generics, views
|
2018-04-21 19:48:33 +00:00
|
|
|
from rest_framework.authtoken.models import Token
|
2018-09-24 17:14:46 +00:00
|
|
|
from rest_framework.authtoken.views import ObtainAuthToken
|
2018-04-19 22:00:49 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2018-04-24 15:57:55 +00:00
|
|
|
import cotisations.models as cotisations
|
|
|
|
import machines.models as machines
|
|
|
|
import preferences.models as preferences
|
|
|
|
import topologie.models as topologie
|
|
|
|
import users.models as users
|
2018-06-10 15:12:42 +00:00
|
|
|
from re2o.utils import all_active_interfaces, all_has_access
|
2018-04-24 15:57:55 +00:00
|
|
|
from . import serializers
|
2018-06-10 15:12:42 +00:00
|
|
|
from .pagination import PageSizedPagination
|
2018-06-13 22:39:37 +00:00
|
|
|
from .permissions import ACLPermission
|
2018-03-17 17:57:11 +00:00
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
# SERVICE REGEN
|
2018-05-26 21:08:03 +00:00
|
|
|
|
2018-06-10 13:41:31 +00:00
|
|
|
class ServiceRegenViewSet(viewsets.ModelViewSet):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Exposes list and details of the services to regen
|
|
|
|
"""
|
2018-05-26 21:08:03 +00:00
|
|
|
serializer_class = serializers.ServiceRegenSerializer
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = machines.Service_link.objects.select_related(
|
|
|
|
'server__domain'
|
|
|
|
).select_related(
|
|
|
|
'service'
|
|
|
|
)
|
|
|
|
if 'hostname' in self.request.GET:
|
|
|
|
hostname = self.request.GET['hostname']
|
|
|
|
queryset = queryset.filter(server__domain__name__iexact=hostname)
|
|
|
|
return queryset
|
|
|
|
|
2018-07-02 09:00:32 +00:00
|
|
|
# Config des switches
|
|
|
|
|
|
|
|
class SwitchPortView(generics.ListAPIView):
|
2018-11-15 14:47:15 +00:00
|
|
|
"""Output each port of a switch, to be serialized with
|
|
|
|
additionnal informations (profiles etc)
|
2018-07-02 09:00:32 +00:00
|
|
|
"""
|
2018-07-08 21:39:15 +00:00
|
|
|
queryset = topologie.Switch.objects.all().select_related("switchbay").select_related("model__constructor").prefetch_related("ports__custom_profile__vlan_tagged").prefetch_related("ports__custom_profile__vlan_untagged").prefetch_related("ports__machine_interface__domain__extension").prefetch_related("ports__room")
|
|
|
|
|
2018-07-02 09:00:32 +00:00
|
|
|
serializer_class = serializers.SwitchPortSerializer
|
|
|
|
|
2018-07-03 16:34:10 +00:00
|
|
|
# Rappel fin adhésion
|
|
|
|
|
|
|
|
class ReminderView(generics.ListAPIView):
|
2018-11-15 14:47:15 +00:00
|
|
|
"""Output for users to remind an end of their subscription.
|
2018-07-03 16:34:10 +00:00
|
|
|
"""
|
|
|
|
queryset = preferences.Reminder.objects.all()
|
|
|
|
serializer_class = serializers.ReminderSerializer
|
|
|
|
|
2018-05-26 21:08:03 +00:00
|
|
|
|
2018-07-08 02:08:01 +00:00
|
|
|
class RoleView(generics.ListAPIView):
|
2018-11-15 14:47:15 +00:00
|
|
|
"""Output of roles for each server
|
2018-07-08 02:08:01 +00:00
|
|
|
"""
|
|
|
|
queryset = machines.Role.objects.all().prefetch_related('servers')
|
|
|
|
serializer_class = serializers.RoleSerializer
|
|
|
|
|
|
|
|
|
2018-07-30 15:00:41 +00:00
|
|
|
# LOCAL EMAILS
|
2018-07-15 20:31:42 +00:00
|
|
|
|
|
|
|
|
2018-07-30 15:00:41 +00:00
|
|
|
class LocalEmailUsersView(generics.ListAPIView):
|
|
|
|
"""Exposes all the aliases of the users that activated the internal address
|
2018-07-15 20:31:42 +00:00
|
|
|
"""
|
2018-07-30 15:00:41 +00:00
|
|
|
serializer_class = serializers.LocalEmailUsersSerializer
|
2018-07-15 20:31:42 +00:00
|
|
|
|
2018-07-30 15:00:41 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
if preferences.OptionalUser.get_cached_value(
|
2018-09-24 17:14:46 +00:00
|
|
|
'local_email_accounts_enabled'):
|
2018-07-30 15:00:41 +00:00
|
|
|
return (users.User.objects
|
|
|
|
.filter(local_email_enabled=True))
|
|
|
|
else:
|
|
|
|
return users.User.objects.none()
|
2018-07-15 20:31:42 +00:00
|
|
|
|
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
# DHCP
|
2018-05-24 23:07:23 +00:00
|
|
|
|
2018-07-30 15:00:41 +00:00
|
|
|
|
2018-05-24 23:07:23 +00:00
|
|
|
class HostMacIpView(generics.ListAPIView):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Exposes the associations between hostname, mac address and IPv4 in
|
|
|
|
order to build the DHCP lease files.
|
|
|
|
"""
|
2018-05-24 23:07:23 +00:00
|
|
|
serializer_class = serializers.HostMacIpSerializer
|
|
|
|
|
2019-01-10 22:15:36 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return all_active_interfaces()
|
|
|
|
|
2018-05-24 23:07:23 +00:00
|
|
|
|
2018-09-24 17:14:46 +00:00
|
|
|
# Firewall
|
2018-07-13 07:56:15 +00:00
|
|
|
|
|
|
|
class SubnetPortsOpenView(generics.ListAPIView):
|
|
|
|
queryset = machines.IpType.objects.all()
|
|
|
|
serializer_class = serializers.SubnetPortsOpenSerializer
|
|
|
|
|
2018-09-24 17:14:46 +00:00
|
|
|
|
2018-08-05 12:36:32 +00:00
|
|
|
class InterfacePortsOpenView(generics.ListAPIView):
|
|
|
|
queryset = machines.Interface.objects.filter(port_lists__isnull=False).distinct()
|
|
|
|
serializer_class = serializers.InterfacePortsOpenSerializer
|
|
|
|
|
2018-09-24 17:14:46 +00:00
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
# DNS
|
2018-06-10 00:47:25 +00:00
|
|
|
|
2018-07-30 15:00:41 +00:00
|
|
|
|
2018-06-10 13:41:31 +00:00
|
|
|
class DNSZonesView(generics.ListAPIView):
|
2018-08-04 08:53:34 +00:00
|
|
|
"""Exposes the detailed information about each extension (hostnames,
|
2018-06-17 01:06:58 +00:00
|
|
|
IPs, DNS records, etc.) in order to build the DNS zone files.
|
|
|
|
"""
|
2018-06-23 21:19:11 +00:00
|
|
|
queryset = (machines.Extension.objects
|
|
|
|
.prefetch_related('soa')
|
|
|
|
.prefetch_related('ns_set').prefetch_related('ns_set__ns')
|
|
|
|
.prefetch_related('origin')
|
|
|
|
.prefetch_related('mx_set').prefetch_related('mx_set__name')
|
|
|
|
.prefetch_related('txt_set')
|
|
|
|
.prefetch_related('srv_set').prefetch_related('srv_set__target')
|
|
|
|
.all())
|
2018-06-10 00:47:25 +00:00
|
|
|
serializer_class = serializers.DNSZonesSerializer
|
|
|
|
|
2018-09-24 17:14:46 +00:00
|
|
|
|
2018-07-14 08:59:17 +00:00
|
|
|
class DNSReverseZonesView(generics.ListAPIView):
|
2019-01-10 22:15:36 +00:00
|
|
|
"""Exposes the detailed information about each extension (hostnames,
|
2018-07-14 08:59:17 +00:00
|
|
|
IPs, DNS records, etc.) in order to build the DNS zone files.
|
|
|
|
"""
|
|
|
|
queryset = (machines.IpType.objects.all())
|
|
|
|
serializer_class = serializers.DNSReverseZonesSerializer
|
|
|
|
|
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
# MAILING
|
2018-06-10 15:12:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StandardMailingView(views.APIView):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Exposes list and details of standard mailings (name and members) in
|
|
|
|
order to building the corresponding mailing lists.
|
|
|
|
"""
|
2018-06-10 15:12:42 +00:00
|
|
|
pagination_class = PageSizedPagination
|
2018-09-24 17:14:46 +00:00
|
|
|
permission_classes = (ACLPermission,)
|
|
|
|
perms_map = {'GET': [users.User.can_view_all]}
|
2018-06-10 15:12:42 +00:00
|
|
|
|
|
|
|
def get(self, request, format=None):
|
2018-06-13 22:39:37 +00:00
|
|
|
adherents_data = serializers.MailingMemberSerializer(all_has_access(), many=True).data
|
2018-06-10 15:12:42 +00:00
|
|
|
data = [{'name': 'adherents', 'members': adherents_data}]
|
|
|
|
paginator = self.pagination_class()
|
|
|
|
paginator.paginate_queryset(data, request)
|
|
|
|
return paginator.get_paginated_response(data)
|
|
|
|
|
|
|
|
|
|
|
|
class ClubMailingView(generics.ListAPIView):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Exposes list and details of club mailings (name, members and admins) in
|
|
|
|
order to build the corresponding mailing lists.
|
|
|
|
"""
|
2018-06-10 15:12:42 +00:00
|
|
|
queryset = users.Club.objects.all()
|
|
|
|
serializer_class = serializers.MailingSerializer
|
|
|
|
|
|
|
|
|
2018-06-17 01:06:58 +00:00
|
|
|
# TOKEN AUTHENTICATION
|
|
|
|
|
|
|
|
|
2018-04-21 19:48:33 +00:00
|
|
|
class ObtainExpiringAuthToken(ObtainAuthToken):
|
2018-06-17 01:06:58 +00:00
|
|
|
"""Exposes a view to obtain a authentication token.
|
|
|
|
|
|
|
|
This view as the same behavior as the
|
|
|
|
`rest_framework.auth_token.views.ObtainAuthToken` view except that the
|
|
|
|
expiration time is send along with the token as an addtional information.
|
|
|
|
"""
|
2018-09-24 17:14:46 +00:00
|
|
|
|
2018-04-21 19:48:33 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
user = serializer.validated_data['user']
|
|
|
|
token, created = Token.objects.get_or_create(user=user)
|
|
|
|
|
|
|
|
token_duration = datetime.timedelta(
|
|
|
|
seconds=settings.API_TOKEN_DURATION
|
|
|
|
)
|
|
|
|
utc_now = datetime.datetime.now(datetime.timezone.utc)
|
|
|
|
if not created and token.created < utc_now - token_duration:
|
|
|
|
token.delete()
|
|
|
|
token = Token.objects.create(user=user)
|
|
|
|
token.created = datetime.datetime.utcnow()
|
|
|
|
token.save()
|
|
|
|
|
|
|
|
return Response({
|
|
|
|
'token': token.key,
|
2018-05-24 17:56:00 +00:00
|
|
|
'expiration': token.created + token_duration
|
2018-04-21 19:48:33 +00:00
|
|
|
})
|
2019-01-29 15:33:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EMailAddressViewSet(viewsets.ReadOnlyModelViewSet):
|
|
|
|
"""Exposes list and details of `users.models.EMailAddress` objects.
|
|
|
|
"""
|
|
|
|
serializer_class = serializers.EMailAddressSerializer
|
|
|
|
queryset = users.EMailAddress.objects.none()
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
if preferences.OptionalUser.get_cached_value(
|
|
|
|
'local_email_accounts_enabled'):
|
|
|
|
return (users.EMailAddress.objects
|
|
|
|
.filter(user__local_email_enabled=True))
|
|
|
|
else:
|
|
|
|
return users.EMailAddress.objects.none()
|