Ruby로 만든 관리자 로그인 페이지 입니다.

로그인 폼을 만들고 액션을 해결할 컨트롤러를 생성하고,  폼에서 전달받은 내용으로 관리자임을 확인하고
쿠키를 만든 후 관리자 페이지로 이동하거나, 혹은 로그인 페이지로 다시 이동하는 기능을 합니다. 

우선 폼을 위한 html파일을 만들었습니다.
rails 프로젝트를 만든 이후에 '/public/admin/logn.html'를 만듭니다.
html안에는 간단한 폼만 있습니다.


 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<table style="position:absolute; top:35%; left:35%" >
 <tr><td align="left">
 <form action="/login/admin_login" method="post" name="adminLoginForm" onsubmit="return false">
 <fieldset>
 <legend>관리자 로그인</legend>
 <div><span style="margin-right:80px;">Id</span><input id="id" name="id" size="20" type="text" value="" /></div>
 <div style="margin-top:10px"><span style="margin-right:33px;">Password</span><input id="password" name="password" size="20" type="password" value="" /></div>
 <div style="position: relative; margin-top:10px; left: 200px"><input name="commit" onclick="this.disabled=true;this.value='Please wait...';this.form.submit();" type="submit" value="Login" /></div>
 </fieldset>
 </form>
 </td></tr>
</table>
</body>
</html>



action의 값을 '/login/admin_login'으로 정했습니다.
이제 Login 컨트롤러를 만들고 admin_login 메서드/뷰 를 추가하는 작업을 진행합니다.

 class LoginController < ApplicationController
 
  def admin_login
    admin_id = params[:id]
    admin_password = params[:password]
    if admin_id == 'admin' && admin_password == '1234' then
      # make cookie
      cookies[:membership] = { :value => "admin", :expires => 5.hour.from_now }
      success_to
    else
      failed_to
    end
  end
 
  def admin_logout
    # delete cookie
    cookies.delete :membership
    failed_to
  end
 
  def success_to
    redirect_to('/admin/main')
  end
 
  def failed_to
    redirect_to('/admin/login.html')
  end
 
  private :success_to, :failed_to
 
end


성공했을 경우에는 Admin 컨트롤러를 호출하게 됩니다.
이제 Admin컨트롤러를 만들고 main 메서드/뷰를 만들겠습니다. 

 require 'admin'

class AdminController < ApplicationController
  layout 'admin_page'

  before_filter :is_admin
 
  def is_admin
    if cookies['membership'] != "admin" then
      redirect_to('/admin/login.html')
    end
  end
 
  def main

  end
 end



Admin 컨트롤러를 통해서 만나는 뷰페지이지 들은  'admin_page' 레이아웃을 기본으로 합니다.
admin_page 레이아웃을 '/app/view/layouts/admin_page.rhtml'로 생성합니다.


 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>
<body>
<div>상단 <a href="/login/admin_logout">로그아웃</a></div>
<div><%= @content_for_layout %></div>
<div>하단</div>
</body>
</html>


'<%= @content_for_layout %>'에 지정된 뷰가 들어가게 됩니다.
admin/main으로 호출했으니 '/app/view/admin/main.rhtml'이 저 자리에 들어가게 됩니다.
Admin 컨트롤러에는 'before_filter'이 포함되어 있습니다.
단어 그대로 항상 먼저 실행되게 됩니다. 이렇게 함으로 /admin/ ... 으로 시작하는 모든 페이지는 쿠키값을 통해서 admin 로그인 여부를 확인하게 됩니다.

익숙한 언어로 만든다면 눈깜짝할 사이에 해결할 수도 있겠지만 

저작자 표시 비영리
Posted by 티엘로