我已经设置了一个测试应用程序并设置了负责身份验证的设置,此外我还设置了一个组件,在注册后将它们发送到创建配置文件页面,效果很好。
我遇到的问题是,当登录用户去编辑他们的个人资料时,很容易更改查询字符串并访问其他用户数据 -
http://localhost:3000/profiles/1/edit
我的问题是如何将其锁定到当前用户,以便只能编辑他们的数据?
罗比
我已经设置了一个测试应用程序并设置了负责身份验证的设置,此外我还设置了一个组件,在注册后将它们发送到创建配置文件页面,效果很好。
我遇到的问题是,当登录用户去编辑他们的个人资料时,很容易更改查询字符串并访问其他用户数据 -
http://localhost:3000/profiles/1/edit
我的问题是如何将其锁定到当前用户,以便只能编辑他们的数据?
罗比
我会去一个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
假设:
User
您可以将令牌身份验证与会话一起使用,以实现更精确和安全的身份验证。
将 devise :token_authenticable 添加到模型 User 这将在每次创建用户时在 users 表的 field authentication_token 字段中创建一个身份验证令牌。
然后去 before_filter :verify_auth_token
def verify_auth_token
if current_user.authentication_token == params[:auth_token]
return true
else
return false
end
end
编辑请求也应该是 http:///profiles/1/edit?auth_token=12wqaasaeaad