分享內容(
上述的第四行,是最簡單的帳號政策,可以在網頁上直接註冊新的帳號,該帳號密碼即可使用;若再加上參數 --include-activation ,則會為該帳號產生一驗證碼,透過 ActionMailer 寄到註冊時所提供的email地址,從email中點選其連結才生效該帳號;再加上 --stateful 這參數,可讓帳號有不同的狀態:像是有註冊但未啟用(:pending)、不能登入但可用來做mailing list之類的用途(:pasive)、暫時停用(:suspended)、帳號雖刪除但id仍留著,而其登入名稱及email可再被使用(:deleted)、啟用(:active)等。(參閱http://www.vaporbase.com/postings/stateful_authentication)
如果又需要把使用者區分成不同的角色或群組,而允許做所指定的動作的話,就需要參閱Restful Authentication with all the bells and whistles這篇文章指到最新版的文章來實作。
貼上幾行就可運作
可以用現成的這兩個註冊、登入畫面:
app/views/sessions/new.html.erb 即是登入帳號密碼的 form template
app/views/users/new.html.erb 是註冊新帳號密碼 email 的 form template
這樣 http://sitename/signup 就可註冊,…以此類推。
以「第三日」為例,若要在articles上加上此認證機制,可先在 app/views/layouts/articles.html.erb 加上
可讓 http://sitename/articles/* 的每頁上方有登出入的狀態。
如果上述的第四行「未登入」,也可取代為連結到預設的 登入 網頁或者直接畫出 登入的 form :
當執行的時候會出現 undefined method `logged_in?' for #<ActionView::Base:0x7f1a1d38c230> 這樣的錯誤訊息,所以要把 include AuthenticatedSystem 這一行字加到controller裡面,如果加在 app/controllers/application.rb 裡,則 controller 中就會把認證的function都讀進來,若不要所有的 controller 需要做認證動作的話,就只要加在該controller之中。
上述只是讓 controller 認得怎麼做認證及相關的變數,而要讓action經過認證才可被執行的話,就要在該controller上加上上述的10或12或14行,對於要未登入而要做需授權的動作,就會跳到登入帳號密碼的畫面。簡單的多人登入登出的動作就可運作了。
只有看上述的程式介紹,而沒看到 screenshot ,真的就覺得不夠實在,這兩天剛有新機器上線,再整理一下門面就放上來供參考。
參考資料
http://railscasts.com/episodes/67
http://www.railsforum.com/viewtopic.php?id=14216
http://railsforum.com/viewtopic.php?pid=74245#p74245
http://www.bllll.com/thread-36539-1-1.html
7人
restful_authentication的簡介及安裝
要實作一個可新增帳號、登入及登出的授權機制,真的蠻麻煩;而這個restful_authentication都先把必要而共通需要的每個動作都作掉了,這篇的screencast http://railscasts.com/episodes/67,實在是簡潔而有效的教學,照著指令操作就完成了登出登入的基本功能,
cd test1 #安裝 restful_authentication 的 plugin ./script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/ # 產生 user 的 model及 table 的欄位定義,並做出 users, sessions 的controller及views script/generate authenticated user sessions # 做到資料庫的table rake db:migrate
上述的第四行,是最簡單的帳號政策,可以在網頁上直接註冊新的帳號,該帳號密碼即可使用;若再加上參數 --include-activation ,則會為該帳號產生一驗證碼,透過 ActionMailer 寄到註冊時所提供的email地址,從email中點選其連結才生效該帳號;再加上 --stateful 這參數,可讓帳號有不同的狀態:像是有註冊但未啟用(:pending)、不能登入但可用來做mailing list之類的用途(:pasive)、暫時停用(:suspended)、帳號雖刪除但id仍留著,而其登入名稱及email可再被使用(:deleted)、啟用(:active)等。(參閱http://www.vaporbase.com/postings/stateful_authentication)
如果又需要把使用者區分成不同的角色或群組,而允許做所指定的動作的話,就需要參閱Restful Authentication with all the bells and whistles這篇文章指到最新版的文章來實作。
貼上幾行就可運作
可以用現成的這兩個註冊、登入畫面:
app/views/sessions/new.html.erb 即是登入帳號密碼的 form template
app/views/users/new.html.erb 是註冊新帳號密碼 email 的 form template
#在 config/routes.rb 裡加入即可用 # 額外加這一行,可以讓第三日的 blog 的 controller 的 index 畫面當首面,可不用多打路徑就出現 map.root :controller => "blog", :action => "index" # 以下做出註冊、登入、登出的路徑 map.signup '/signup', :controller => 'users', :action => 'new' map.login '/login', :controller => 'sessions', :action => 'new' map.logout '/logout', :controller => 'sessions', :action => 'destroy'
這樣 http://sitename/signup 就可註冊,…以此類推。
以「第三日」為例,若要在articles上加上此認證機制,可先在 app/views/layouts/articles.html.erb 加上
# 在 <body> 之下加入這幾行 <% if logged_in? %> 您是:<%= current_user.login -%> || <%= link_to '登出', logout_url -%> <% else -%> 未登入 <% end -%> <p style="color: green"><%= flash[:notice] %></p>
可讓 http://sitename/articles/* 的每頁上方有登出入的狀態。
如果上述的第四行「未登入」,也可取代為連結到預設的 登入 網頁或者直接畫出 登入的 form :
# 以 連結 來取代 <%= link_to '到登入畫面', login_url -%> # 或者以 form 來取代 <% form_tag session_path do -%> <label for="login">帳號</label><%= text_field_tag 'login',nil,:size => 10 %> <label for="password">密碼</label><%= password_field_tag 'password',nil ,:size => 10 %> <%= submit_tag "登入", :name => nil %> <% end %>
當執行的時候會出現 undefined method `logged_in?' for #<ActionView::Base:0x7f1a1d38c230> 這樣的錯誤訊息,所以要把 include AuthenticatedSystem 這一行字加到controller裡面,如果加在 app/controllers/application.rb 裡,則 controller 中就會把認證的function都讀進來,若不要所有的 controller 需要做認證動作的話,就只要加在該controller之中。
#在 app/controllers/application.rb 加入 class ApplicationController < ActionController::Base include AuthenticatedSystem ... end # 或只在 app/controllers/articles_controller.rb 裡 class ArticlesController < ApplicationController include AuthenticatedSystem # 所有動作都需先登入 before_filter :login_required # 或 列舉出需要登入的action before_filter :login_required, :only => [ :new, :create, :edit, :update, :destroy ] # 或 除了這兩個action外,其他動作都需登入才能執行 before_filter :login_required, :except => [ :index, :show ] ... end
上述只是讓 controller 認得怎麼做認證及相關的變數,而要讓action經過認證才可被執行的話,就要在該controller上加上上述的10或12或14行,對於要未登入而要做需授權的動作,就會跳到登入帳號密碼的畫面。簡單的多人登入登出的動作就可運作了。
只有看上述的程式介紹,而沒看到 screenshot ,真的就覺得不夠實在,這兩天剛有新機器上線,再整理一下門面就放上來供參考。
參考資料
http://railscasts.com/episodes/67
http://www.railsforum.com/viewtopic.php?id=14216
http://railsforum.com/viewtopic.php?pid=74245#p74245
http://www.bllll.com/thread-36539-1-1.html
▼ ADVERTISEMENT ▼
-
‧
-
‧
-
‧
相關問答
- 請問如何成為一位稱職的JAVA程式設計師
- API 應用程式介面(Application Programming Interface)
- 程式設計師是否須具備資安人員的相關知識?
- 什麼是網頁應用程式源碼檢測
- 成為programmer的確切行動
- 如何成一位超強的網頁程式設計師,需要加強那些技巧?
- 考 Sun SCJP SCJD認證有何不同?
- 程式之美-微軟技術面試心得
- NCLP 證照
- Ultracompare Professional
- 剛入行的Programmer薪水多少?
- [Ruby]Ruby要怎麼樣才能使用windows命令列裡的指令呢?
- 在Ruby上,可以用近似 jQuery語法來解析DOM的好工具 -Hpricot
- 如何開發出安全的ASP網頁?
- solar - Framework for PHP 5
- .NET Framework的安裝順序?
- Zend Framework 1.5 正式推出
- Zend Framework將整合dojo做為ajax開發的工具
- 學 .net 好還是學php好呢?
- Microsoft Office 專家認證MOS









