rails 4设计ldap_authenticatable current_user未设置

时间:2015-09-30 11:52:53

标签: devise

我对Rails 4很新,我正在尝试使用Devise和ldap_authenticatable,我看到一些我不确定是对的。当我对我的Active Directory Devise进行身份验证时,可以正常工作,并按预期将用户存储在MySQL数据库中。但是,我似乎失去了用户参数,无法分辨哪个用户只是经过身份验证。 user_signed_in?返回false但如果我点击登录链接,我收到消息“已登录”current_user为nil且set_user失败,因为params(:id)为nil。看起来像是在这里打破了一些东西,但我不确定在设计或保持用户参数存活的情况下,规范是什么。 任何想法或有用的信息? 用户模型:

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :ldap_authenticatable, :trackable, :validatable
  before_save :get_ldap_attrs
def get_ldap_attrs
  self.firstname = Devise::LDAP::Adapter.get_ldap_param(self.email, 'givenName')
  self.lastname = Devise::LDAP::Adapter.get_ldap_param(self.email, 'sn')
  self.login = Devise::LDAP::Adapter.get_ldap_param(self.email, 'sAMAccountName')
  self.email = Devise::LDAP::Adapter.get_ldap_param(self.email,'mail').first
  self.studentid = Devise::LDAP::Adapter.get_ldap_param(self.email, 'title')
end
end
----

    ldap.yaml
    ## Authorizations
    # Uncomment out the merging for each environment that you'd like to include.
    # You can also just copy and paste the tree (do not include the "authorizations") to each
    # environment if you need something different per enviornment.
    authorizations: &AUTHORIZATIONS
      allow_unauthenticated_bind: false
      group_base: ou=groups,dc=kentshill,dc=org
      ## Requires config.ldap_check_group_membership in devise.rb be true
      # Can have multiple values, must match all to be authorized
      required_groups:
        # If only a group name is given, membership will be checked against "uniqueMember"
        #- ######################## 
        #- #######################
        # If an array is given, the first element will be the attribute to check against, the second the group name
        #- ["moreMembers", "cn=users,ou=groups,dc=test,dc=com"]
      ## Requires config.ldap_check_attributes in devise.rb to be true
      ## Can have multiple attributes and values, must match all to be authorized
      require_attribute:
        objectClass: inetOrgPerson
        authorizationRole: postsAdmin

    ## Environment

    development:
      host: address
      port: 636
      attribute: mail
      base: DN
      admin_user: fqn user with privs
      admin_password: password
      ssl: true
      # <<: *AUTHORIZATIONS

    test:
      host: localhost
      port: 3389
      attribute: cn
      base: ou=people,dc=test,dc=com
      admin_user: cn=admin,dc=test,dc=com
      admin_password: admin_password
      ssl: simple_tls
      # <<: *AUTHORIZATIONS

    production:
      host: localhost
      port: 636
      attribute: cn
      base: ou=people,dc=test,dc=com
      admin_user: cn=admin,dc=test,dc=com
      admin_password: admin_password
      ssl: start_tls
      # <<: *AUTHORIZATIONS
    ----------------
    Devise initializer
    # Use this hook to configure devise mailer, warden hooks and so forth.
    # Many of these configuration options can be set straight in your model.
    Devise.setup do |config|
      # ==> LDAP Configuration
      config.ldap_logger = true
      config.ldap_create_user = true
      config.ldap_update_password = true
      #config.ldap_config = "#{Rails.root}/config/ldap.yml"
      config.ldap_check_group_membership = false
      #config.ldap_check_group_membership_without_admin = false
      config.ldap_check_attributes = false
      config.ldap_use_admin_to_bind = true
      config.ldap_ad_group_check = false

      # The secret key used by Devise. Devise uses this key to generate
      # random tokens. Changing this key will render invalid all existing
      # confirmation, reset password and unlock tokens in the database.
      # Devise will use the `secret_key_base` on Rails 4+ applications as its `secret_key`
      # by default. You can change it below and use your own secret key.
      # config.secret_key = 'ead157a98cc1402f93c717c537225a807971f381bdb51063b22d9979b39e0db385493e0d392999152597ce52baf327d97ffc9a59371ea3258cd8f5fc6d158b75'

      # ==> Mailer Configuration
      # Configure the e-mail address which will be shown in Devise::Mailer,
      # note that it will be overwritten if you use your own mailer class
      # with default "from" parameter.
      config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

      # Configure the class responsible to send e-mails.
      # config.mailer = 'Devise::Mailer'

      # ==> ORM configuration
      # Load and configure the ORM. Supports :active_record (default) and
      # :mongoid (bson_ext recommended) by default. Other ORMs may be
      # available as additional gems.
      require 'devise/orm/active_record'
      config.ldap_auth_username_builder = Proc.new() { |attribute, login, ldap| login }
    #  config.warden do |manager|
    #    manager.default_strategies(:scope => :user).unshift :ldap_authenticatable
    #  end

      # ==> Configuration for any authentication mechanism
      # Configure which keys are used when authenticating a user. The default is
      # just :email. You can configure it to use [:username, :subdomain], so for
      # authenticating a user, both parameters are required. Remember that those
      # parameters are used only when authenticating and not when retrieving from
      # session. If you need permissions, you should implement that in a before filter.
      # You can also supply a hash where the value is a boolean determining whether
      # or not authentication should be aborted when the value is not present.
      config.authentication_keys = [:email]

      # Configure parameters from the request object used for authentication. Each entry
      # given should be a request method and it will automatically be passed to the
      # find_for_authentication method and considered in your model lookup. For instance,
      # if you set :request_keys to [:subdomain], :subdomain will be used on authentication.
      # The same considerations mentioned for authentication_keys also apply to request_keys.
      # config.request_keys = []

      # Configure which authentication keys should be case-insensitive.
      # These keys will be downcased upon creating or modifying a user and when used
      # to authenticate or find a user. Default is :email.
      config.case_insensitive_keys = [:email]
    "config/initializers/devise.rb" 280L, 13721C

0 个答案:

没有答案