ruby on rails - facebook2如何保存新寄存器



我使用的是facebooker2 (Rails 2.3)。和RESTFUlL身份验证)的facebook连接功能。在我的控制器中,我使用这个动作:

  def create_facebook_user
    if current_facebook_user
      @user = User.find_by_fb_user_id(current_facebook_user.id.to_i)
    end
    if @user.blank?
      @facebook_user = current_facebook_user.fetch
      @user = User.new :login => @facebook_user.email, :email => @facebook_user.email, :name => @facebook_user.name
      @user.fb_user_id = @facebook_user.id.to_i
      @user.state = "active"
      if @user.save(:validate=> false)
        @user.profile = Profile.create(:benefactor_id => nil, :benefactor_invites => Setting.find_by_identifier("benefactor_invites").value.to_i)
        redirect_to :controller => "profiles", :action => "show", :id => @user.profile.id
      else
        render "new"
      end
    elsif @user.fb_user_id.nil?
      @user.update_attribute :fb_user_id, current_facebook_user.id
      redirect_to :controller => "dashboard", :url => "index"
    else
      redirect_to :controller => "dashboard", :url => "index"
    end

我的问题是然后分配状态给用户。当用户使用保存方法@user时。Save (:validate => false)不会"跳过"验证器。我还修改了RESTFul身份验证模块"ByPassword"的方法password_required,但保存方法返回false。

我张贴我的代码

Controller
    class UsersController < ApplicationController
      skip_before_filter :verify_authenticity_token, :only => :create
      before_filter :find_user,
                    :only => [:profile,
                              :destroy,
                              :edit_password, :update_password,
                              :edit_email, :update_email]
      layout 'application'
      def create_facebook_user
        if current_facebook_user
          @user = User.find_by_fb_user_id(current_facebook_user.id.to_i)
        end
        if @user.blank?
          @facebook_user = current_facebook_user.fetch
          @user = User.new :login => @facebook_user.email, :email => @facebook_user.email, :name => @facebook_user.name
          @user.fb_user_id = @facebook_user.id.to_i
          @user.state = "active"
          if @user.save(:validate=> false)
            @user.profile = Profile.create(:benefactor_id => nil, :benefactor_invites => Setting.find_by_identifier("benefactor_invites").value.to_i)
            redirect_to :controller => "profiles", :action => "show", :id => @user.profile.id
          else
            render "new"
          end
        elsif @user.fb_user_id.nil?
          @user.update_attribute :fb_user_id, current_facebook_user.id
          redirect_to :controller => "dashboard", :url => "index"
        else
          redirect_to :controller => "dashboard", :url => "index"
        end
      end
end

View (important Fragment)

不要通过Facebook Connect来获取信息<%= fb_login_and_redirect("/users/create_facebook_user") %><%#= fb_login_and_redirect('/users/link_user_accounts',:perms => 'email,user_birthday') %>& lt; % # = fb_login_button("窗口。Location = '/users/link_user_accounts'") %>

用户模型
require 'digest/sha1'
class User < ActiveRecord::Base
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken
  include Authorization::AasmRoles
...
end

ByPassword模块
module Authentication
  module ByPassword
    # Stuff directives into including module
    def self.included(recipient)
      recipient.extend(ModelClassMethods)
      recipient.class_eval do
        include ModelInstanceMethods
        # Virtual attribute for the unencrypted password
        attr_accessor :password
        validates_presence_of :password, :message => :"user.password.blank", :if => :password_required?
        validates_presence_of :password_confirmation, :message => :"user.password_confirmation.blank", :if => :password_required?
        validates_confirmation_of :password, :message => :"user.password.confirmation", :if => :password_required?
        validates_length_of :password, :within => 5..40, :message => :"user.password.too_short", :if => :password_required?
        before_save :encrypt_password
      end
    end
    # #included directives
    #
    # Class Methods
    #
    module ModelClassMethods
      # This provides a modest increased defense against a dictionary attack if
      # your db were ever compromised, but will invalidate existing passwords.
      # See the README and the file config/initializers/site_keys.rb
      #
      # It may not be obvious, but if you set REST_AUTH_SITE_KEY to nil and
      # REST_AUTH_DIGEST_STRETCHES to 1 you'll have backwards compatibility with
      # older versions of restful-authentication.
      def password_digest(password, salt)
        digest = REST_AUTH_SITE_KEY
        REST_AUTH_DIGEST_STRETCHES.times do
          digest = secure_digest(digest, salt, password, REST_AUTH_SITE_KEY)
        end
        digest
      end
    end # class methods
    #
    # Instance Methods
    #
    module ModelInstanceMethods
      # Encrypts the password with the user salt
      def encrypt(password)
        self.class.password_digest(password, salt)
      end
      def authenticated?(password)
        crypted_password == encrypt(password)
      end
      # before filter 
      def encrypt_password
        return if password.blank?
        self.salt = self.class.make_token if new_record?
        self.crypted_password = encrypt(password)
      end
      def password_required?
        if fb_user_id.blank?
          crypted_password.blank? || !password.blank?
        else
          return false
        end
      end
      def has_fb_user_id?
        fb_user_id.nil?
      end
    end # instance methods
  end
end

首先我使用ActiveRecord的save_with_validations(false)方法,然后我使用普通保存方法

相关内容

  • 没有找到相关文章

最新更新