Sha256: 03a05cf2d1e8fce3c5d71d0f966394412ff18b8d3e858234178e0bfb6ab52552

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

module DeviseRemoteUser

  #
  # The Manager class is responsible for connecting the appliation's User 
  # class with remote user information in the request environment.
  #
  class Manager

    attr_reader :env
    
    def initialize(env)
      @env = env
    end

    def find_or_create_user
      user = find_user
      if !user && DeviseRemoteUser.auto_create
        user = create_user
      end
      update_user(user) if user && DeviseRemoteUser.auto_update
      user
    end

    def find_user
      User.where(user_criterion).first
    end

    def create_user
      random_password = SecureRandom.hex(16)
      attrs = user_criterion.merge({password: random_password, password_confirmation: random_password})
      User.create(attrs)
    end

    def update_user(user)
      user.update_attributes(remote_user_attributes)
    end

    protected
    
    def remote_user_attributes
      DeviseRemoteUser.attribute_map.inject({}) { |h, (k, v)| h[k] = env[v] if env.has_key?(v); h }
    end

    def user_criterion
      {auth_key => remote_user_id}
    end

    def remote_user_id
      env[DeviseRemoteUser.env_key]
    end

    def auth_key
      DeviseRemoteUser.auth_key || Devise.authentication_keys.first
    end
    
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
devise-remote-user-0.3.0 lib/devise_remote_user/manager.rb
devise-remote-user-0.2.0 lib/devise_remote_user/manager.rb