>Can you share the content of session_help.rb?
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
# Returns true if the given user is the current user.
def current_user?(user)
user == current_user
end
def logged_in?
!current_user.nil?
end
# Redirects to stored location (or to the default).
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
def log_out
session.delete(:user_id)
@current_user = nil
session.delete(:isitadmin)
end
def admin(role)
session[:isitadmin] = role
end
def checkadmin
admin?(session[:isitadmin])
end
def admin?(rolea)
rolea == 1
end
end
enddef loginnow
role = User.where(userid: params[:session][:userid]).pluck(:roleid)
user = User.find_by(userid: params[:session][:userid])
if user && user.authenticate(params[:session][:password])
# Log the user in and redirect to the user's show page.
admin role
log_in user
if admin?(role)
flash.now[:info] = 'You are logged in as Admin and your roleid is #{role}'
redirect_to dashboard_index_path
puts "*******************************************************************************************************"
puts "The roleid is #{rolea} executed in if part"
puts "*******************************************************************************************************"
else
flash.now[:danger] = 'For some reason you are not recognized as Admin and the roleid is #{role}'
redirect_to dashboard_index_path
puts "*******************************************************************************************************"
puts "The roleid is #{role} executed in else part"
puts "*******************************************************************************************************"
end>On creation of a new user, in your database users table, do you set a flag to indicate whether or not the user is admin, let's say the >field is 'is_admin' and 1 indicates admin >and 0 indicates not admin.
def admin?(rolea)
if rolea == 1
return true
else
return false
end
endif admin?(role) == true-And I deleted the above response, my kid startled me and I hit some wrong keys that caused a posting of an incomplete response.....
Not sure what your helper method is doing
Why do you not use
if current_user.roleid == 1
to test if the logged in user is admin or not?
Why do you not use
if current_user.roleid == 1
to test if the logged in user is admin or not?
I don't think you need this
#def checkadmin
# admin?(session[:isitadmin])
# end
def admin?(rolea)
rolea == 1
end
end
endThis is my sessions_controller.rb file
def loginnow
role = User.where(userid: params[:session][:userid]).pluck(:roleid) ## This call isn't necessary
user = User.find_by(userid: params[:session][:userid]) ## you should verify validity of session[:userid], and then check validity of user
if user && user.authenticate(params[:session][:password])
# Log the user in and redirect to the user's show page.
admin user.roleid.to_s ## to_s may not be necessary
log_in user
if admin?(role)
flash.now[:info] = 'You are logged in as Admin and your roleid is #{role}' ## Remove .now, see http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-now
redirect_to dashboard_index_path ## are you displaying flash in view... Something like <% flash.each .... %>
puts "*******************************************************************************************************"
puts "The roleid is #{rolea} executed in if part"
puts "*******************************************************************************************************"