Skip to content

Commit 6ffc79d

Browse files
committed
Added extra security for blog app. Deletion of tags, articles and authors removed unless signed in. Creation of new authors removed unless via invite. Visual deletion buttons removed from views index pages.
1 parent 88ef437 commit 6ffc79d

File tree

11 files changed

+37
-10
lines changed

11 files changed

+37
-10
lines changed

app/assets/stylesheets/styles.css.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ a {
1919

2020
img {
2121
border-radius: 4px;
22+
max-width: 100%;
2223
}
2324

2425
.clear {
@@ -28,7 +29,7 @@ img {
2829
}
2930

3031
#container {
31-
width: 75%;
32+
width: auto;
3233
margin: 0 auto;
3334
background: #f0f0f0;
3435
padding: 20px 40px;

app/controllers/articles_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class ArticlesController < ApplicationController
2-
32
include ArticlesHelper
43

4+
before_action :require_login, except: [:index, :show]
5+
56
def index
67
@articles = Article.all
78
end

app/controllers/authors_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
class AuthorsController < ApplicationController
22
before_action :set_author, only: [:show, :edit, :update, :destroy]
3+
before_action :zero_authors_or_authenticated, only: [:new, :create]
4+
before_action :require_login, except: [:new, :create]
5+
6+
def zero_authors_or_authenticated
7+
unless Author.count == 0 || current_user
8+
redirect_to root_path
9+
return false
10+
end
11+
end
312

413
# GET /authors
514
# GET /authors.json

app/controllers/comments_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class CommentsController < ApplicationController
2+
before_action :require_login, except: [:create]
3+
24
def create
35
@comment = Comment.new(comment_params)
46
@comment.article_id = params[:article_id]

app/controllers/tags_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
class TagsController < ApplicationController
2+
before_action :require_login, only: [:destroy]
3+
24
def index
35
@tags = Tag.all
46
end
57

68
def show
79
@tag = Tag.find(params[:id])
810
end
11+
12+
def destroy
13+
@tag = Tag.find(params[:id])
14+
@tag.destroy
15+
flash.notice = "The '#{@tag.name}' tag has been deleted."
16+
redirect_to tags_path
17+
end
918
end

app/models/article.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Article < ApplicationRecord
22
has_many :comments
33
has_many :taggings
4-
has_many :tags, through: :taggings, dependent: :destroy
4+
has_many :tags, through: :taggings
55
has_attached_file :image, styles: {large: "1280x720>", medium: "800x450>", small: "400x225>", thumb: "100x100>"}
66
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/png', 'image/gif']
77

app/models/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Tag < ApplicationRecord
2-
has_many :taggings
2+
has_many :taggings, dependent: :destroy
33
has_many :articles, through: :taggings
44
end

app/views/articles/index.html.erb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
</li><br>
88
<% end %>
99
</ul>
10-
11-
<%= link_to 'Create a new post', new_article_path, class: "new_article" %>
10+
<% if logged_in? %>
11+
<%= link_to 'Create a new post', new_article_path, class: "new_article" %>
12+
<% end %>

app/views/articles/show.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
</p>
1313
<p><%= @article.body %></p><br>
1414

15-
<%= link_to 'Edit Article', edit_article_path(@article) %> | <%= link_to "Delete Article", article_path(@article), method: :delete, data: { confirm: 'Are you sure?' } %><br><br>
15+
<% if logged_in? %>
16+
<%= link_to 'Edit Article', edit_article_path(@article) %> | <%= link_to "Delete Article", article_path(@article), method: :delete, data: { confirm: 'Are you sure?' } %><br><br>
17+
<% end %>
18+
1619
<h3>Comments</h3>
1720
<%= render partial: 'articles/comment', collection: @article.comments %>
1821
<%= render partial: 'comments/form' %>

app/views/layouts/application.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
</head>
1111

1212
<body>
13-
<p class = 'flash'>
14-
<%= flash.notice %>
15-
</p>
13+
<p class = 'flash'><%= flash.notice %></p>
1614
<div id = "container">
1715
<div id = "content">
1816
<%= yield %>

0 commit comments

Comments
 (0)