From 86bb6c7ccfaa516f79d7e73c6d2f276da8904da8 Mon Sep 17 00:00:00 2001 From: Manager Bot Date: Sat, 26 Nov 2022 08:28:16 +0000 Subject: [PATCH] One route for domain adding. --- Web/lib/MJB/Web.pm | 3 +- Web/lib/MJB/Web/Controller/Blog.pm | 157 +++++++++++------------ Web/templates/blog/domain_hosted.html.ep | 6 +- Web/templates/blog/domain_owned.html.ep | 5 +- 4 files changed, 82 insertions(+), 89 deletions(-) diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 1e1924f..52cec12 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -214,9 +214,8 @@ sub startup ($self) { # Blog Creation $auth->get ( '/blog' )->to('Blog#index' )->name('show_blog' ); $auth->get ( '/blog/domain/hosted' )->to('Blog#domain_hosted' )->name('show_blog_domain_hosted' ); - $auth->post( '/blog/domain/hosted' )->to('Blog#do_domain_hosted' )->name('do_blog_domain_hosted' ); $auth->get ( '/blog/domain/owned' )->to('Blog#domain_owned' )->name('show_blog_domain_owned' ); - $auth->post( '/blog/domain/owned' )->to('Blog#do_domain_owned' )->name('do_blog_domain_owned' ); + $auth->post( '/blog/domain' )->to('Blog#do_domain' )->name('do_blog_domain' ); $auth->get ( '/blog/initialize' )->to('Blog#do_initialize' )->name('do_blog_initialize' ); $auth->get ( '/blog/:id/settings' )->to('Blog#settings' )->name('show_blog_settings' ); $auth->post( '/blog/:id/settings' )->to('Blog#do_settings' )->name('do_blog_settings' ); diff --git a/Web/lib/MJB/Web/Controller/Blog.pm b/Web/lib/MJB/Web/Controller/Blog.pm index daaec23..155e73f 100644 --- a/Web/lib/MJB/Web/Controller/Blog.pm +++ b/Web/lib/MJB/Web/Controller/Blog.pm @@ -26,45 +26,6 @@ sub domain_hosted ( $c ) { } -#== -# POST /blog/domain/hosted | do_blog_domain_hosted -# subdomain | A subdomain that the user would like to use -# hosted_domain_id | The id of a hosted_domain that the user wants to use -# -# This route will create a blog as a subdomain of a hosted domain. We control -# the DNS, SSL, etc. -#== -sub do_domain_hosted ( $c ) { - my $sub_domain = $c->stash->{form}->{subdomain} = $c->param('subdomain'); - my $top_domain = $c->db->hosted_domain( $c->param('hosted_domain_id') ); - - # Store the hosted domain id so we can have the right domain selected on the drop down - # for errors. - $c->stash->{form}->{hdid} = $top_domain->id; - - $sub_domain = lc($sub_domain); - - push @{$c->stash->{errors}}, "Please select a domain from the drop down menu." - unless $top_domain; - - push @{$c->stash->{errors}}, "Please enter a value for the subdomain." - unless $sub_domain; - - push @{$c->stash->{errors}}, "Subdomains must start with a letter, and may use letters, numbers, dashes and hyphens." - unless $sub_domain =~ /^[a-z]+[a-z0-9-_]*$/; - - push @{$c->stash->{errors}}, "That domain name is already being used." - unless $c->db->domains( { name => $sub_domain . '.' . $top_domain->name } )->count == 0; - - return $c->redirect_error( 'show_blog_domain_hosted' ) - if $c->stash->{errors}; - - my $domain_name = $sub_domain . '.' . $top_domain->name; - my $domain_ssl = $top_domain->letsencrypt_challenge ne 'http' ? $top_domain->name : ''; - - return $c->_initialize_blog( $domain_name, 'show_blog_domain_hosted', $domain_ssl ); -} - #== # GET /blog/domain/owned | show_blog_domain_owned # @@ -77,54 +38,88 @@ sub domain_owned ( $c ) { } #== -# POST /blog/domain/owned | do_blog_domain_owned -# domain_name | The domain name that the user would like to use +# POST /blog/domain | do_blog_domain +# domain | Host a FQDN. This will use http ssl challenge and +# | expect the user to have already configured DNS. +# hosted_subdomain | Host on a subdomain that the system knows about. +# hosted_domain_id | The id of a hosted_domain that the user wants to use +# calling_route | The route that called this (i.e. show_domain_hosted) # -# This route will create a blog with whatever domain the user supplies. They will -# need to have ensured that DNS is pointed to the correct addresses so that it works. -#== -sub do_domain_owned ( $c ) { - my $domain_name = $c->stash->{form}->{domain_name} = $c->param('domain_name'); - - $domain_name = lc($domain_name); - - push @{$c->stash->{errors}}, "Please enter a value for the domain name." - unless $domain_name; - - push @{$c->stash->{errors}}, "That domain name is already being used." - unless $c->db->domains( { name => $domain_name } )->count == 0; - - return $c->redirect_error( 'show_blog_domain_owned' ) - if $c->stash->{errors}; - - return $c->_initialize_blog( $domain_name, 'show_blog_domain_owned', '' ); -} - -#== -# Helper Function... +# domain OR ( hosted_subdomain AND hosted_domain_id ) is required, in addition +# to calling_route. +# +# This route will create a blog on a given domain. When domain is given, use that +# as a FQDN with http ssl challenge. # -# This will make the blog after domain_hosted / domain_owned figures out what -# it's making. -#== -sub _initialize_blog ( $c, $domain, $from, $ssl_domain ) { - - # Do we already have this domain name? - if ( $c->db->domain( { name => $domain } ) ) { - $c->flash( errors => [ 'That domain name is already being hosted.' ] ); - $c->redirect_to( $c->url_for( $from ) ); - return; +# When hosted_subdomain is given, prepend it to the name of the domain identified by +# hosted_domain_id, and use whatever ssl challenge method the hosted_domain is set to +# use. +# +# When an error happens, the calling_route will be returned to: +# +# show_blog_domain_hosted +# show_blog_domain_owned +#== +sub do_domain ( $c ) { + my $domain = $c->stash->{form}->{domain} = lc($c->param('domain')); + my $hosted_subdomain = $c->stash->{form}->{hosted_subdomain} = lc($c->param('hosted_subdomain')); + my $hosted_domain_id = $c->stash->{form}->{hosted_domain_id} = $c->param('hosted_domain_id'); + my $calling_route = $c->stash->{form}->{calling_route} = $c->param('calling_route'); + + if ( $calling_route eq 'show_blog_domain_owned' ) { + $c->stash->{fqdn} = $domain; + + # Domain name defined? + push @{$c->stash->{errors}}, "Please enter a value for the domain name." + unless $domain; + + # Already exists? + push @{$c->stash->{errors}}, "That domain name is already being used." + unless $c->db->domains( { name => $domain } )->count == 0; + + } elsif ( $calling_route eq 'show_blog_domain_hosted' ) { + $c->stash->{hosted_domain} = $c->db->hosted_domain( $hosted_domain_id ); + $c->stash->{form}->{hdid} = $c->stash->{hosted_domain}->id; + $c->stash->{fqdn} = $hosted_subdomain . '.' . $c->stash->{hosted_domain}->name; + $c->stash->{ssl_domain} = $c->stash->{hosted_domain}->name; + + # Valid hosted_domain selected. + push @{$c->stash->{errors}}, "Please select a domain from the drop down menu." + unless $c->stash->{hosted_domain}; + + # Sub domain was answered. + push @{$c->stash->{errors}}, "Please enter a value for the subdomain." + unless $hosted_subdomain; + + # Subdomain is valid. + push @{$c->stash->{errors}}, "Subdomains must start with a letter, and may use letters, numbers, dashes and hyphens." + unless $hosted_subdomain =~ /^[a-z]+[a-z0-9-_]*$/; + + # Already exists? + push @{$c->stash->{errors}}, "That domain name is already being used." + unless $c->db->domains( { name => $c->stash->{fqdn} } )->count == 0; + + } else { + # Shouldn't have gotten here. + push @{$c->stash->{errors}}, "If you continue to experience problems, please contact support."; + $calling_route = 'show_blog_domain_hosted'; } - # Create the Jekyll repo for the site - # This is where a job to build it on the build server should happen... + # Bail on any errors. + return $c->redirect_error( $calling_route ) + if $c->stash->{errors}; - my $jekyll = $c->jekyll($domain)->init; + # Set scalars to values from stash, and initialize Jekyll blog. + my $hosted_domain = $c->stash->{hosted_domain}; + my $fqdn = $c->stash->{fqdn}; + my $ssl_domain = $c->stash->{ssl_domain}; + my $jekyll = $c->jekyll($fqdn)->init; my $blog = try { $c->db->storage->schema->txn_do( sub { # Make the domain name record. my $domain_record = $c->stash->{person}->create_related('domains', { - name => $domain, + name => $fqdn, ( $ssl_domain ? ( ssl => $ssl_domain ) : () ), }); @@ -143,11 +138,9 @@ sub _initialize_blog ( $c, $domain, $from, $ssl_domain ) { push @{$c->stash->{errors}}, "Blog could not be created: $_"; }; - if ( $c->stash->{errors} ) { - $c->flash( errors => $c->stash->{errors} ); - $c->redirect_to( $c->url_for( $from ) ); - return; - } + # Bail on any errors. + return $c->redirect_error( $calling_route ) + if $c->stash->{errors}; # Schedule a job to deploy the website if ( ! $ssl_domain ) { diff --git a/Web/templates/blog/domain_hosted.html.ep b/Web/templates/blog/domain_hosted.html.ep index 58230c7..530d2c0 100644 --- a/Web/templates/blog/domain_hosted.html.ep +++ b/Web/templates/blog/domain_hosted.html.ep @@ -23,15 +23,15 @@ %= include '_base/status_window'; -
+ +
-
https:// - + .
https:// - +