2022-11-16 16:23:46 -08:00
from django import forms
2023-05-03 21:42:37 -07:00
from django . contrib import messages
from django . core . files import File
2022-12-07 08:25:10 -08:00
from django . core . validators import RegexValidator
2022-11-16 20:12:28 -08:00
from django . db import models
from django . shortcuts import get_object_or_404 , redirect
2022-11-16 16:23:46 -08:00
from django . utils . decorators import method_decorator
2022-11-17 23:09:04 -08:00
from django . views . generic import FormView , TemplateView
2022-11-16 16:23:46 -08:00
2023-05-03 21:42:37 -07:00
from core . models import Config
2022-11-16 20:14:05 -08:00
from users . decorators import admin_required
2023-02-19 10:09:49 -08:00
from users . models import Domain , User
2022-11-16 16:23:46 -08:00
2022-12-07 08:25:10 -08:00
class DomainValidator ( RegexValidator ) :
ul = " \u00a1 - \uffff " # Unicode letters range (must not be a raw string).
# Host patterns
hostname_re = (
r " [a-z " + ul + r " 0-9](?:[a-z " + ul + r " 0-9-] { 0,61}[a-z " + ul + r " 0-9])? "
)
# Max length for domain name labels is 63 characters per RFC 1034 sec. 3.1
domain_re = r " (?: \ .(?!-)[a-z " + ul + r " 0-9-] { 1,63}(?<!-))* "
tld_re = (
r " \ . " # dot
r " (?!-) " # can't start with a dash
r " (?:[a-z " + ul + " -] { 2,63} " # domain label
r " |xn--[a-z0-9] { 1,59}) " # or punycode label
r " (?<!-) " # can't end with a dash
r " \ .? " # may have a trailing dot
)
regex = " ^ " + hostname_re + domain_re + tld_re + " $ "
message = " This does not look like a domain name "
2022-11-16 20:14:05 -08:00
@method_decorator ( admin_required , name = " dispatch " )
2022-11-17 23:09:04 -08:00
class Domains ( TemplateView ) :
2022-11-16 20:12:28 -08:00
2022-11-16 20:42:25 -08:00
template_name = " admin/domains.html "
2022-11-16 20:12:28 -08:00
def get_context_data ( self ) :
return {
" domains " : Domain . objects . filter ( local = True ) . order_by ( " domain " ) ,
" section " : " domains " ,
}
2022-11-16 20:14:05 -08:00
@method_decorator ( admin_required , name = " dispatch " )
2022-11-17 23:09:04 -08:00
class DomainCreate ( FormView ) :
2022-11-16 20:12:28 -08:00
2022-11-16 20:42:25 -08:00
template_name = " admin/domain_create.html "
2022-11-16 20:12:28 -08:00
extra_context = { " section " : " domains " }
class form_class ( forms . Form ) :
domain = forms . CharField (
help_text = " The domain displayed as part of a user ' s identity. \n Cannot be changed after the domain has been created. " ,
2022-12-07 08:25:10 -08:00
validators = [ DomainValidator ( ) ] ,
2022-11-16 20:12:28 -08:00
)
service_domain = forms . CharField (
2022-12-26 14:29:09 -08:00
help_text = " Optional - a domain that serves Takahē if it is not running on the main domain. \n Cannot be changed after the domain has been created. \n Must be unique for each display domain! " ,
2022-11-16 20:12:28 -08:00
required = False ,
2022-12-07 08:25:10 -08:00
validators = [ DomainValidator ( ) ] ,
2022-11-16 20:12:28 -08:00
)
public = forms . BooleanField (
2023-01-23 15:17:18 -08:00
help_text = " If any user on this server can create identities under this domain " ,
2022-11-16 20:12:28 -08:00
widget = forms . Select ( choices = [ ( True , " Public " ) , ( False , " Private " ) ] ) ,
required = False ,
)
2022-11-18 07:28:15 -08:00
default = forms . BooleanField (
2023-01-23 15:17:18 -08:00
help_text = " If this domain is the default option for new identities " ,
2022-12-17 16:17:59 -08:00
widget = forms . Select ( choices = [ ( False , " No " ) , ( True , " Yes " ) ] ) ,
2022-11-18 07:28:15 -08:00
required = False ,
)
2023-02-19 10:09:49 -08:00
users = forms . CharField (
label = " Permitted Users " ,
help_text = " If this domain is not public, the email addresses of the users allowed to use it. \n One email address per line. " ,
widget = forms . Textarea ,
required = False ,
)
2023-03-06 15:37:05 -08:00
notes = forms . CharField (
label = " Notes " ,
widget = forms . Textarea (
attrs = {
" rows " : 3 ,
} ,
) ,
required = False ,
)
2022-11-16 20:12:28 -08:00
def clean_domain ( self ) :
if Domain . objects . filter (
models . Q ( domain = self . cleaned_data [ " domain " ] )
| models . Q ( service_domain = self . cleaned_data [ " domain " ] )
) :
raise forms . ValidationError ( " This domain name is already in use " )
return self . cleaned_data [ " domain " ]
def clean_service_domain ( self ) :
if not self . cleaned_data [ " service_domain " ] :
return None
if Domain . objects . filter (
models . Q ( domain = self . cleaned_data [ " service_domain " ] )
| models . Q ( service_domain = self . cleaned_data [ " service_domain " ] )
) :
raise forms . ValidationError ( " This domain name is already in use " )
if self . cleaned_data . get ( " domain " ) == self . cleaned_data [ " service_domain " ] :
raise forms . ValidationError (
" You cannot have the domain and service domain be the same (did you mean to leave service domain blank?) "
)
return self . cleaned_data [ " service_domain " ]
2022-11-18 07:28:15 -08:00
def clean_default ( self ) :
value = self . cleaned_data [ " default " ]
if value and not self . cleaned_data . get ( " public " ) :
raise forms . ValidationError ( " A non-public domain cannot be the default " )
return value
2023-02-19 10:09:49 -08:00
def clean_users ( self ) :
if not self . cleaned_data [ " users " ] . strip ( ) :
return [ ]
if self . cleaned_data . get ( " public " ) :
raise forms . ValidationError (
" You cannot limit by user when the domain is public "
)
# Turn contents into an email set
user_emails = set ( )
for line in self . cleaned_data [ " users " ] . splitlines ( ) :
line = line . strip ( )
if line :
user_emails . add ( line )
# Fetch those users
users = list ( User . objects . filter ( email__in = user_emails ) )
# See if there's a set difference
missing_emails = user_emails . difference ( { user . email for user in users } )
if missing_emails :
raise forms . ValidationError (
" These emails do not have user accounts: "
+ ( " , " . join ( missing_emails ) )
)
return users
2022-11-16 20:12:28 -08:00
def form_valid ( self , form ) :
2022-11-18 07:28:15 -08:00
domain = Domain . objects . create (
2022-11-16 20:12:28 -08:00
domain = form . cleaned_data [ " domain " ] ,
service_domain = form . cleaned_data [ " service_domain " ] or None ,
2023-03-06 15:37:05 -08:00
notes = form . cleaned_data [ " notes " ] or None ,
2022-11-16 20:12:28 -08:00
public = form . cleaned_data [ " public " ] ,
2022-11-18 07:28:15 -08:00
default = form . cleaned_data [ " default " ] ,
2022-11-16 20:12:28 -08:00
local = True ,
)
2023-02-19 10:09:49 -08:00
domain . users . set ( form . cleaned_data [ " users " ] )
2022-11-18 07:28:15 -08:00
if domain . default :
Domain . objects . exclude ( pk = domain . pk ) . update ( default = False )
2022-11-16 20:12:28 -08:00
return redirect ( Domain . urls . root )
2022-11-16 20:14:05 -08:00
@method_decorator ( admin_required , name = " dispatch " )
2022-11-17 23:09:04 -08:00
class DomainEdit ( FormView ) :
2022-11-16 20:12:28 -08:00
2022-11-16 20:42:25 -08:00
template_name = " admin/domain_edit.html "
2022-11-16 20:12:28 -08:00
extra_context = { " section " : " domains " }
2022-11-18 07:28:15 -08:00
class form_class ( DomainCreate . form_class ) :
2023-05-03 21:42:37 -07:00
site_name = forms . CharField (
help_text = " Override the site name for this domain " ,
required = False ,
)
site_icon = forms . ImageField (
required = False ,
help_text = " Override the site icon for this domain " ,
)
hide_login = forms . BooleanField (
help_text = " If the login button should appear in the header for this domain " ,
widget = forms . Select ( choices = [ ( False , " Show " ) , ( True , " Hide " ) ] ) ,
initial = True ,
required = False ,
)
custom_css = forms . CharField (
help_text = " Customise the appearance of this domain \n See our <a href= ' # ' >documentation</a> for help on common changes \n <b>WARNING: Do not put untrusted code in here!</b> " ,
widget = forms . Textarea ,
required = False ,
)
single_user = forms . CharField (
label = " Single User Profile Redirect " ,
help_text = " If provided, redirect the homepage of this domain to this identity ' s profile for logged-out users \n Use the identity ' s full handle - for example, <tt>takahe@jointakahe.org</tt> " ,
required = False ,
)
2022-11-18 07:28:15 -08:00
def __init__ ( self , * args , * * kwargs ) :
super ( ) . __init__ ( * args , * * kwargs )
self . fields [ " domain " ] . disabled = True
self . fields [ " service_domain " ] . disabled = True
def clean_domain ( self ) :
return self . cleaned_data [ " domain " ]
def clean_service_domain ( self ) :
return self . cleaned_data [ " service_domain " ]
2022-11-16 20:12:28 -08:00
2023-05-03 21:42:37 -07:00
def clean_single_user ( self ) :
return self . cleaned_data [ " single_user " ] . lstrip ( " @ " )
2022-11-16 20:12:28 -08:00
def dispatch ( self , request , domain ) :
self . domain = get_object_or_404 (
Domain . objects . filter ( local = True ) , domain = domain
)
return super ( ) . dispatch ( request )
2022-11-18 07:28:15 -08:00
def get_context_data ( self , * args , * * kwargs ) :
context = super ( ) . get_context_data ( * args , * * kwargs )
2022-11-16 20:12:28 -08:00
context [ " domain " ] = self . domain
return context
def form_valid ( self , form ) :
self . domain . public = form . cleaned_data [ " public " ]
2022-11-18 07:28:15 -08:00
self . domain . default = form . cleaned_data [ " default " ]
2023-03-06 15:37:05 -08:00
self . domain . notes = form . cleaned_data [ " notes " ] or None
2022-11-16 20:12:28 -08:00
self . domain . save ( )
2023-02-19 10:09:49 -08:00
self . domain . users . set ( form . cleaned_data [ " users " ] )
2022-11-18 07:28:15 -08:00
if self . domain . default :
Domain . objects . exclude ( pk = self . domain . pk ) . update ( default = False )
2023-05-03 21:42:37 -07:00
Config . set_domain ( self . domain , " hide_login " , form . cleaned_data [ " hide_login " ] )
Config . set_domain ( self . domain , " site_name " , form . cleaned_data [ " site_name " ] )
if isinstance ( form . cleaned_data [ " site_icon " ] , File ) :
Config . set_domain ( self . domain , " site_icon " , form . cleaned_data [ " site_icon " ] )
Config . set_domain ( self . domain , " custom_css " , form . cleaned_data [ " custom_css " ] )
Config . set_domain ( self . domain , " single_user " , form . cleaned_data [ " single_user " ] )
messages . success ( self . request , f " Domain { self . domain } saved. " )
2022-11-16 20:12:28 -08:00
return redirect ( Domain . urls . root )
def get_initial ( self ) :
return {
" domain " : self . domain . domain ,
" service_domain " : self . domain . service_domain ,
2023-03-06 15:37:05 -08:00
" notes " : self . domain . notes ,
2022-11-16 20:12:28 -08:00
" public " : self . domain . public ,
2022-11-18 07:28:15 -08:00
" default " : self . domain . default ,
2023-02-19 10:09:49 -08:00
" users " : " \n " . join ( sorted ( user . email for user in self . domain . users . all ( ) ) ) ,
2023-05-03 21:42:37 -07:00
" site_name " : self . domain . config_domain . site_name ,
" site_icon " : self . domain . config_domain . site_icon ,
" hide_login " : self . domain . config_domain . hide_login ,
" custom_css " : self . domain . config_domain . custom_css ,
" single_user " : self . domain . config_domain . single_user ,
2022-11-16 20:12:28 -08:00
}
2022-11-16 20:14:05 -08:00
@method_decorator ( admin_required , name = " dispatch " )
2022-11-17 23:09:04 -08:00
class DomainDelete ( TemplateView ) :
2022-11-16 20:12:28 -08:00
2022-11-16 20:42:25 -08:00
template_name = " admin/domain_delete.html "
2022-11-16 20:12:28 -08:00
def dispatch ( self , request , domain ) :
self . domain = get_object_or_404 (
Domain . objects . filter ( public = True ) , domain = domain
)
return super ( ) . dispatch ( request )
def get_context_data ( self ) :
return {
" domain " : self . domain ,
" num_identities " : self . domain . identities . count ( ) ,
" section " : " domains " ,
}
def post ( self , request ) :
if self . domain . identities . exists ( ) :
raise ValueError ( " Tried to delete domain with identities! " )
self . domain . delete ( )
2022-11-18 07:28:15 -08:00
return redirect ( " admin_domains " )