I have two kinds of users: Workers and Admins.
I have a table of workers and a table of admins.
I want to force the user to sign in before he can enter to the application pages.
so I added אם my ApplicationController the next line:
`before_filter :authenticate_user!`
now, I wrote a function, is called: is_worker that check if the user is found in the workers table. if so, redirect him to the page of the workers. if the user isn’t found, redirect him to the admin page.
I want that workers will not able to enter the admin pages, and the admins will not able to enter the workers pages.
so I think I should add: before_filter :is_worker to: workerscontroller and adminscontroller.
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
def is_worker
@email = current_user.email
tag = Worker.where(:email => @email)
if tag.nil?
redirect_to '/admins'
else
redirect_to '/workers'
end
end
end
please correct me if I am wrong.
It’s not very efficient. Assuming you’re using STI, create an
is_worker?method like this:Then in your controllers you can add a method like this:
I’d make it a private method. Then you can call this method after the user successfully logs in.
Even then you’ll want to protect your application at the controller level so that a worker can’t just go to the
/adminsURL and see admin data.