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?
Update
AuthenticaeUser
to look like this:Then use it in your controller like this:
This assumes that
NumeroEmpregado
value is stored in thetitle
attribute within your LDAP server.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).