Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check diary entry edit/update ability using CanCanCan #5012

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/abilities/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ def initialize(user)
can [:new, :show, :create, :destroy], :oauth2_authorization
can [:edit, :update, :destroy], :account
can [:show], :dashboard
can [:new, :create, :edit, :update, :subscribe, :unsubscribe], DiaryEntry
can [:new, :create, :subscribe, :unsubscribe], DiaryEntry
can :update, DiaryEntry, :user => user
can [:create], DiaryComment
can [:make_friend, :remove_friend], Friendship
can [:new, :create, :reply, :show, :inbox, :outbox, :muted, :mark, :unmute, :destroy], Message
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/diary_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def update
@title = t "diary_entries.edit.title"
@diary_entry = DiaryEntry.find(params[:id])

if current_user != @diary_entry.user ||
if cannot?(:update, @diary_entry) ||
(params[:diary_entry] && @diary_entry.update(entry_params))
redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
else
Expand Down
2 changes: 1 addition & 1 deletion app/views/diary_entries/_diary_entry.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</li>
<% end %>

<% if current_user && current_user == diary_entry.user %>
<% if can?(:edit, diary_entry) %>
<li><%= link_to t(".edit_link"), edit_diary_entry_path(diary_entry.user, diary_entry) %></li>
<% end %>

Expand Down
23 changes: 23 additions & 0 deletions test/controllers/diary_entries_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,29 @@ def test_edit_i18n
assert_select "span[class=translation_missing]", false, "Missing translation in edit diary entry"
end

def test_update
user = create(:user)
other_user = create(:user)
diary_entry = create(:diary_entry, :language_code => "en", :user => user, :title => "Original Title")

put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" })
assert_response :forbidden
diary_entry.reload
assert_equal "Original Title", diary_entry.title

session_for(other_user)
put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" })
assert_redirected_to diary_entry_path(user, diary_entry)
diary_entry.reload
assert_equal "Original Title", diary_entry.title

session_for(user)
put diary_entry_path(user, diary_entry, :diary_entry => { :title => "Updated Title" })
assert_redirected_to diary_entry_path(user, diary_entry)
diary_entry.reload
assert_equal "Updated Title", diary_entry.title
end

def test_index_all
diary_entry = create(:diary_entry)
geo_entry = create(:diary_entry, :latitude => 51.50763, :longitude => -0.10781)
Expand Down