upon login authentication with LDAP/AD how do I cr

2019-08-20 17:17发布

Currently I have this code for my sessions_controller:

class SessionsController < ApplicationController
  def new
  end

  def create
    username = params[:nome]
    password = params[:password]
    name     = username 

    if AuthenticateUser.new(username, password).call
      user = User.create_with(nome: name).find_or_create_by(nome: user)
      session[:user_id] = user.id
      redirect_to '/'
    else
      flash[:error] = "Erro!              \nNúmero de Empregado e/ou password incorrecto(a)"
      redirect_to '/login'
     end
  end

  def destroy
    session[:user_id] = nil
    redirect_to '/index/new'
  end
end

What I want to do is to check if the user that I'm logging in with the LDAP (as shown in my previous question) has a field in my users table and if not to automatically create one with the username and attributing it an automatically user_id that Rails does and getting a field from the LDAP and putting it in my SQLSERVER DB, the problem is when I log in with my account It just redirects me to '/' (root) without any error notices and without creating a new row on my database

I'm using SqlServer Management Studio and my users table has the following fields: id NumeroEmpregado nome created_at updated_at

I want to make NumeroEmpregado to be automatically given from the LDAP (attribute in LDAP is title). I'll worry with that later, and I want nome to be the username given in the form:

 <%= form_tag '/login' do %>
  <div class="form-group">
    <div class="text">
      Número de Empregado: <br> 
      <%= text_field_tag :nome %><br>
      Password: <br>
      <%= password_field_tag :password %><br>
    </div>
  </div>
  <%= submit_tag "Submit", class: "button" %>
<% end %>
  • How can I do this/What are the errors in my code?

2条回答
爷的心禁止访问
2楼-- · 2019-08-20 17:35

Update AuthenticaeUser to look like this:

class AuthenticateUser
  def self.call(*args)
    new(*args).call
  end

  def initialize(username, password)
    @username = "#{username}@company.com"
    @password = password
  end

  def call
    search_title_if_valid_user
  end

  private
  def search_title_if_valid_user
    ldap = Net::LDAP.new(
      host: server_ip_address,
      port: 389,
      base: "DC=corp,DC=com",   # change for your company values
      auth: { method: :simple, username: @username, password: @password }
    )

    ldap.search(attributes: ["title"]) if ldap.bind
  end
end

Then use it in your controller like this:

class SessionsController < ApplicationController
  def new
  end

  def create
    username = params[:nome]
    password = params[:password]

    title = AuthenticateUser.call(username, password)

    if title
      user = User.create_with(nome: username).find_or_create_by(NumeroEmpregado: title)
      session[:user_id] = user.id
      redirect_to '/'
    else
      flash[:error] = "Erro!              \nNúmero de Empregado e/ou password incorrecto(a)"
      redirect_to '/login'
     end
  end

  def destroy
    session[:user_id] = nil
    redirect_to '/index/new'
  end
end

This assumes that NumeroEmpregado value is stored in the title attribute within your LDAP server.

查看更多
我命由我不由天
3楼-- · 2019-08-20 17:46

I think you also want to re-write the creation of the user, because basically you're trying to create it first, and the calling find or create again. You can have it all in the same method, hence it's find_or_create_by. You probably want to make sure the user was created/exists as well (.persisted? will return true for either).

if AuthenticateUser.new(username, password).call
  user = User.find_or_create_by(nome: username)
  if user.persisted?
    session[:user_id] = user.id
    redirect_to '/'
  else
    #...
  end
else
  flash[:error] = "Erro!              \nNúmero de Empregado e/ou password incorrecto(a)"
  redirect_to '/login'
end
查看更多
登录 后发表回答