Reputation: 1157
I have set up a test applications and have setup devise to take care of the authentication, additionally I have set up a component where they are sent to a create profile page after registration which works well.
The problem I have is when a logged in user goes to edit they're profile it is easy for then to change the query string and access another users data -
http://localhost:3000/profiles/1/edit
the question i have is how do I lock this down to the current user so that can only edit they're data?
Robbie
Upvotes: 0
Views: 793
Reputation: 9008
I would go for a before_filter
.
# in profiles controller
class ProfilesController < ApplicationController
before_filter :find_profile
before_filter :check_if_authorized
def find_profile
@profile = Profile.find(params[:id])
end
def check_if_authorized
render :status => 404 and return unless current_user == @profile.user
end
end
Assumptions:
User
Upvotes: 1
Reputation: 117
You can use token authentication along with session for more precise and secure authentication.
Add devise :token_authenticatable to the model User This will create an authentication token into the field authentication_token field of users table every time a user is created.
Then go for a before_filter :verify_auth_token
def verify_auth_token
if current_user.authentication_token == params[:auth_token]
return true
else
return false
end
end
Also the edit request should be http:///profiles/1/edit?auth_token=12wqaasaeaad
Upvotes: 1