2016-05-29 11 views
1

私は最近、レールの学習を始め、ユーザー(管理者)、学生、インストラクターの3つの役割を持つアプリケーションを開発しなければなりませんでした。私は以下のコードを自分で書いた。問題は、最新のログインと同様に、アクティブなすべてのユーザーの役割を果たすということです。たとえば、管理者がログインした後、生徒がログインした後、管理者がページを更新すると、学生になります。この問題を解決するために私のコードで何を変更する必要がありますか?このように実装することができない場合は、この問題をどのように修正するべきかを教えてください。ありがとう。それはあなたがIDを保存するための鍵と同じセッションsession[:user_id]を共有しているように私には思える私のapplication_controller.rbコードrails 4 - ロールベースの承認をゼロから

def require_login 
    unless session[:user_id] or config.my_config 
     flash[:notice] = "You are not Logged In" 
     redirect_to :root 
     return false 
    else 
    return true 
    end 

end 

答えて

0

def login 
if session[:user_id]!=nil 
    redirect_to(:action => 'index') 
end 
end 
def attempt_login 
if params[:username].present? && params[:password].present? 
    found_user = User.where(:username => params[:username]).first 
    if found_user 
     authorized_user = found_user.authenticate(params[:password]) 
     if authorized_user 
     flash[:notice] = "Welcome! You are LoggedIn" 
     session[:user_id] = authorized_user.id 
     redirect_to(:action => 'index') 
     return 
     end 
    end 
    found_student = Student.where(:username => params[:username]).first 
     if found_student 
     student_id = Student.authenticate(params[:username],params[:password]) 
     if student_id 
      flash[:notice] = "Welcome! You are LoggedIn" 
      session[:user_id] = student_id 
      redirect_to(:action => 'student_index') 
      return 
     end 
     end 
    found_instructor = Instructor.where(:username => params[:username]).first 
     if found_instructor 
     instructor_id = Instructor.authenticate(params[:username],params[:password]) 
     if instructor_id 
      flash[:notice] = "Welcome! You are LoggedIn" 
      session[:user_id] = instructor_id 
      redirect_to(:action => 'instructor_index') 
      return 
     end 
     end 
    flash[:notice] = "Invalid username/Password combination." 
    redirect_to(:action => 'login') 
end 
end 


def logout 
flash[:notice]="Logged out" 
session[:user_id] = nil 
redirect_to(:action => "login") 
end 

とここにある:ここ

は私users_controllerコードです User, Studentおよび Instructorである。どのように3種類のログインしたユーザーを区別しますか?

それは、Student訪問instructor_indexパスでログインしている場合、その後、学生のIDは、同じIDでInstructorをロードするために使用されるだろう、とインストラクターのためのビューがレンダリングされている可能性があります。私はそうでないことを示すコードは見当たりません。

私はsession[:user_id]session[:user_type]の両方を保存するようなシステムを想定していたので、ログインした人物を正確に識別することができます。

関連する問題