Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit 4fc0ac8

Browse files
committed
Merge pull request #3 from pixelhandler/protected-attributes
Protected attributes
2 parents ec64f9b + 7a93cfd commit 4fc0ac8

6 files changed

Lines changed: 62 additions & 10 deletions

File tree

app/controllers/api/auth_controller.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Api::AuthController < ApplicationController
55
def authenticate
66
user = User.find_by_credentials(params[:username], params[:password])
77
if user
8+
@current_user = user
89
render json: { auth_token: user.generate_auth_token }
910
else
1011
render json: { error: 'Invalid username or password' }, status: :unauthorized
@@ -13,17 +14,18 @@ def authenticate
1314

1415
def authenticate_commenter
1516
commenter = Commenter.find_by_email(params[:email])
16-
unless commenter
17+
unless commenter.present?
1718
commenter = Commenter.create!(name: params[:username], email: params[:email])
1819
end
19-
if commenter
20+
if commenter.present?
21+
@current_user = commenter
22+
render json: { auth_token: commenter.generate_auth_token }
2023
render json: { auth_token: commenter.generate_auth_token, commenter_id: commenter.id }
2124
else
2225
render json: { error: 'Invalid name or email' }, status: :unauthorized
2326
end
24-
rescue => e
25-
byebug
26-
render json: { error: 'Could not create or authenticate user'}, status: :unauthorized
27+
rescue Exception => e
28+
render json: { error: e.message }, status: :unauthorized
2729
end
2830

2931
private

app/controllers/api/v1/comments_controller.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
class Api::V1::CommentsController < ApiControllerController
22
skip_before_action :set_current_user, :authenticate_request, only: [:index, :show, :show_association, :get_related_resources]
3+
before_action :set_current_user
4+
5+
def current_user
6+
@current_user
7+
end
38

49
private
510

11+
def context
12+
self
13+
end
14+
615
def set_current_user
716
if decoded_auth_token
8-
@current_user ||= Commenter.find(decoded_auth_token[:commenter_id])
9-
unless @current_user
10-
@current_user ||= User.find(decoded_auth_token[:user_id])
17+
if decoded_auth_token.has_key? :commenter_id
18+
@current_user = Commenter.find(decoded_auth_token[:commenter_id])
19+
elsif decoded_auth_token.has_key? :user_id
20+
@current_user = User.find(decoded_auth_token[:user_id])
1121
end
22+
nil
1223
end
1324
end
1425

app/controllers/api_controller_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class ApiControllerController < JSONAPI::ResourceController
1010
render json: { error: 'Auth token is expired' }, status: 419 # unofficial timeout status code
1111
end
1212

13+
def current_user
14+
@current_user
15+
end
16+
1317
private
1418

1519
# Based on the user_id inside the token payload, find the user.

app/models/comment.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ class Comment < ActiveRecord::Base
33
belongs_to :post
44

55
validates :body, :length => { :minimum => 2 }
6+
7+
scope :approved, -> { where(approved: true) }
68
end

app/models/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Post < ActiveRecord::Base
22
belongs_to :author
3-
has_many :comments
3+
has_many :comments, -> { where(approved: true) }
44

55
validates :slug, uniqueness: true
66

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
11
require 'jsonapi/resource'
22

33
class Api::V1::CommentResource < JSONAPI::Resource
4-
attributes :id, :body, :approved, :created_at
4+
attributes :id, :body, :created_at
5+
attribute :approved
56
has_one :commenter
67
has_one :post
8+
9+
def approved
10+
if user_is_authorized or comment_owned_by_commenter?(@model)
11+
return @model.approved
12+
end
13+
end
14+
15+
def fetchable_fields
16+
user_is_known ? super : super - [:approved]
17+
end
18+
19+
private
20+
21+
def current_user
22+
context.respond_to?(:current_user) ? context.current_user : nil
23+
end
24+
25+
def user_is_commenter
26+
current_user.present? and current_user.is_a? Commenter
27+
end
28+
29+
def user_is_authorized
30+
current_user.present? and current_user.is_a? User
31+
end
32+
33+
def user_is_known
34+
user_is_commenter or user_is_authorized
35+
end
36+
37+
def comment_owned_by_commenter?(model)
38+
user_is_commenter and model.commenter.id == current_user.id
39+
end
740
end

0 commit comments

Comments
 (0)