1

In order do add security in devise, i need to set the "before_filter" thingy, like:

before_filter :authenticate_student!, only: [:new, :edit]

which is great... But my app need two user types... students and teachers. How do i make the controller just check if any of then is authenticate?

like:

before_filter :authenticate_any!, only: [:new, :edit]

How can i archive that?

I am using Ruby 2.2.0, and rails 4.

Techmago
  • 380
  • 4
  • 18
  • Given your description, sounds like users may have different 'roles'. Where a user could be a student or a teacher (or both?). If so, another approach to this feature is to set the role based on the relationship to the course and only have one type of user. Then you can restrict based on role instead of authentication. – Mark Swardstrom Jun 01 '16 at 18:36
  • Look at this answer: http://stackoverflow.com/a/20642860/3033467 Seems to be exactly what you want. – user3033467 Jun 01 '16 at 18:36
  • My student/teacher tables are really different. The teachers will have different roles, and need different attributes. Thats why i have then apart from the students. – Techmago Jun 01 '16 at 18:39

1 Answers1

2

Just define those methods in your application controller

class ApplicationController < ActionController::Base
  def authenticate_student!
     authenticate_user! && current_user.student?
  end

  def authenticate_any!
    authenticate_user!
  end
end

You may complete the code of how to check student?

Hieu Pham
  • 6,577
  • 2
  • 30
  • 50