Skip to content

Commit fb22b06

Browse files
committed
Changes from new blog tests
1 parent 1db96a2 commit fb22b06

File tree

1 file changed

+56
-27
lines changed
  • Web/lib/BlogDB/Web/Controller

1 file changed

+56
-27
lines changed

Web/lib/BlogDB/Web/Controller/Blog.pm

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -221,38 +221,67 @@ sub post_edit_new_blog ($c) {
221221

222222
my $blog_id = $c->stash->{blog_id} = $c->param('id');
223223
my $blog = $c->stash->{blog} = $c->db->resultset('PendingBlog')->find( $blog_id );
224+
my $person = $c->stash->{person};
224225

226+
# We can continue if:
227+
# 1: We have an edit_token, and it matches the blog->edit_token.
228+
# 2: We are a logged in user and we are the blog->submitter.
229+
# 3: We are a logged in user and we have can_manage_blogs
230+
my $passes_tests = 0;
225231

226-
# TODO: This section should be guarded by checking that the user
227-
# has a UUID that allows editing, or is a logged in user with
228-
# such permissions as allows editing it.
229-
$c->stash->{form_title} = $c->param("title");
230-
$c->stash->{form_url} = $c->param("url");
231-
$c->stash->{form_rss_url} = $c->param("rss_url");
232-
$c->stash->{form_tagline} = $c->param("tagline");
233-
$c->stash->{form_about} = $c->param("about");
234-
$c->stash->{form_adult} = $c->param("is_adult") ? 1 : 0;
235-
236-
$blog->title ( $c->stash->{form_title} );
237-
$blog->url ( $c->stash->{form_url} );
238-
$blog->rss_url ( $c->stash->{form_rss_url} );
239-
$blog->tagline ( $c->stash->{form_tagline} );
240-
$blog->about ( $c->stash->{form_about} );
241-
$blog->is_adult( $c->stash->{form_adult} );
242-
243-
$blog->update;
244-
245-
# Get Posts from RSS Feed.
246-
$c->minion->enqueue( populate_blog_entries => [ $blog->id, 'pending' ]);
232+
if ( $person && $person->setting('can_manage_blogs') ) {
233+
$passes_tests = 1;
234+
push @{$c->stash->{authorization}}, 'setting:can_manage_blogs';
235+
}
236+
if ( $blog->submitter_id && $blog->submitter_id == $person->id ) {
237+
push @{$c->stash->{authorization}}, 'submitter';
238+
$passes_tests = 1;
239+
}
240+
if ( $blog->edit_token && $c->session->{edit_token} ) {
241+
if ( $blog->edit_token eq $c->session->{edit_token} ) {
242+
push @{$c->stash->{authorization}}, 'token';
243+
$passes_tests = 1;
244+
}
245+
}
247246

248-
# Remove all tags, then add the tags we have set.
249-
$blog->search_related('pending_blog_tag_maps')->delete;
250-
foreach my $tag_id ( @{$c->every_param('tags')}) {
251-
$blog->create_related('pending_blog_tag_maps', {
252-
tag_id => $tag_id,
253-
});
247+
# Throw out any users who don't meet the conditions set out
248+
# above.
249+
if ( not $passes_tests ) {
250+
push @{$c->stash->{errors}}, 'Not Authorized.';
251+
$c->redirect_to( $c->url_for( 'homepage' ) );
252+
return 0;
254253
}
255254

255+
# Do the update in a transaction.
256+
$c->db->storage->schema->txn_do( sub {
257+
$c->stash->{form_title} = $c->param("title");
258+
$c->stash->{form_url} = $c->param("url");
259+
$c->stash->{form_rss_url} = $c->param("rss_url");
260+
$c->stash->{form_tagline} = $c->param("tagline");
261+
$c->stash->{form_about} = $c->param("about");
262+
$c->stash->{form_adult} = $c->param("is_adult") ? 1 : 0;
263+
264+
$blog->title ( $c->stash->{form_title} );
265+
$blog->url ( $c->stash->{form_url} );
266+
$blog->rss_url ( $c->stash->{form_rss_url} );
267+
$blog->tagline ( $c->stash->{form_tagline} );
268+
$blog->about ( $c->stash->{form_about} );
269+
$blog->is_adult( $c->stash->{form_adult} );
270+
271+
$blog->update;
272+
273+
# Get Posts from RSS Feed.
274+
$c->minion->enqueue( populate_blog_entries => [ $blog->id, 'pending' ]);
275+
276+
# Remove all tags, then add the tags we have set.
277+
$blog->search_related('pending_blog_tag_maps')->delete;
278+
foreach my $tag_id ( @{$c->every_param('tags')}) {
279+
$blog->create_related('pending_blog_tag_maps', {
280+
tag_id => $tag_id,
281+
});
282+
}
283+
});
284+
256285
# Send the user back to the standard GET path.
257286
$c->redirect_to( $c->url_for( 'edit_new_blog', id => $blog->id ) );
258287
}

0 commit comments

Comments
 (0)