2011-12-07 66 views
0

我有一个网站,我创建了Rails 3.0与设计& cancan安装。此应用程序拥有超过4000人的成员可以查看的列表。数据库中很少有人是成员。rails允许非用户编辑自己的配置文件

有没有办法允许4000人访问更新他们的个人资料,而不必让他们成为用户?

我目前的想法是,我将有三个角色(管理员,成员和更新者)。更新者角色只能访问他们的个人资料,就是这样。我会用cancan限制这个。

这是做到这一点的最好方法是什么? 我可以随机生成一个密码,并通过电子邮件发出链接,在电子邮件中编辑他们的个人资料,当他们点击它时,我的程序可能会要求他们的登录详细信息,然后离开他们。我只是想,如果他们是这样做的更好的方式,而不需要产生这么多的用户。

回答

1

您的个人资料模型添加一列用随机生成的密码,然后有一个链接编辑个人资料的电子邮件该密码文件上的电子邮件地址进行编辑。

class Profile < ActiveRecord::Base 
    def generate_edit_key! 
    self.edit_key = rand(9999999999) 
    save 
    end 
end 

class ProfilesController < ApplicationController 
    def request_edit_key 
    @profile = Profile.find(params[:id]) 
    @proifle.generate_edit_key! 
    ProfileMailer.edit_profile_request(@profile).deliver 
    redirect_to @profile, :notice => 'Mail sent to profile owner' 
    end 
    def edit 
    if current_user 
     @profile = current_user.profile 
    else 
     @profile = Profile.find_by_id_and_edit_key(params[:id], params[:edit_key]) 
    end 
    end 
end 

class ProfileMailer < ActionMailer::Base 
    def edit_profile_request(profile) 
    mail(:to => profile.email, 
     :subject => 'Edit your profile', 
     :body => "<%= link_to 'Click to edit' edit_profile_url(profile, :edit_key => profile.edit_key) %> If you didn't request this, please ignore.") 
end 
+0

我不得不关闭设计认证检查编辑和更新,有没有办法解决这个问题,而不需要重写authenticate_user!?这里是我目前使用的行: before_filter:authenticate_user !,:except => [:edit,:update] – map7

+0

@ map7你可以创建一个特殊的控制器和动作来执行编辑,而不是保护那个。 – Unixmonkey

0

如果你不想让他们成为用户,那么使用令牌系统来授予他们临时访问权限来执行此操作。

link_to 'Let me update this profile', token_path(:id = @profile.id, :method => :post) 

class TokensController < ApplicationController 
    def create 
    @profile = Profile.find(params[:id]) 
    if @profile.send_token! 
     redirect_to profile_path(@profile), :notice => 'A token has been sent' 
    else 
     redirect_to profile_path(@profile), :error => 'A token could not be sent' 
    end 
    end 
end 

class Profile < ActiveRecord::Base 
    has_many :tokens 
    def send_token! 
    token = self.tokens.create() 
    TokenMailer.update_profile(token).deliver 
    end 
end 

class Token < ActiveRecord::Base 
    belongs_to :profile 
    before_save :generate_key, set_expiration 
    def generate_key 
    self.key = rand(999999999999) 
    end 
    def set_expiration 
    self.expires_at = 1.day.from_now 
    end 
    def expired? 
    expires_at > Time.now 
    end 
end 

class TokenMailer < ActionMailer::Base 
    def update_profile(token) 
    mail(:to => token.profile.email, 
     :subject => 'Edit your profile', 
     :body => "<%= link_to 'Click to edit' edit_profile_url(token.profile, :key => token.key) %>") 
    end 
end 

class ProfilesController < ApplicationController 
    def edit 
    if params[:key] 
     token = Token.find_by_key(params[:key]) 
     if token.expired? 
     redirect_to root_url, :message => 'Token Expired' 
     else 
     @profile = token.profile 
     end 
    elsif current_user 
     @profile = current_user.profiles.detect{|p| p.id == params[:id] } 
    else 
     redirect_to root_url, :message => 'Not allowed' 
    end 
    end 
end