在上一篇的介绍Devise【视频,阅读】中,我们已经可以在我们的Rails应用中搭建和使用devise的基本功能了。现在,我们从上一篇介绍留下来的问题开始,来阐述如何定制devise。
在上一篇的介绍里,我们已经可以在rails项目里通过devise很容易的添加注册,登录和退出的功能:
[img]http://dl.iteye.com/upload/attachment/253632/acd403e3-d3b6-3774-81ff-ce56a3eee457.png[/img]
[size=x-large]访问限制[/size]
接下来,我们将实现限制访问。在我们这个应用中(译者注:我们开发的是一个简单的项目管理系统),就是对于创建,修改和删除项目进行权限控制,只有注册并且登录的用户才能有权操作。
我们通过在中ProjectsController添加一个before_filter,在其中调用devise提供的检查是否授权的方法authenticate_user!。这个方法会检查Project下的所有方法,如果,用户没有登录并且试图调用该方法的时候,就会跳转到登录页面。理论上讲,没有登录的用户也应该可以访问index和show方法,也就是说,我们还应该添加一个:except选项来保证用户可以正常方法上面两个方法:
[code="ruby"]
class ProjectsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index]
def index
#rest of class
$ rails generate devise_views
create app/views/devise
create app/views/devise/confirmations/new.html.erb
create app/views/devise/mailer/confirmation_instructions.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
create app/views/devise/passwords/edit.html.erb
create app/views/devise/passwords/new.html.erb
create app/views/devise/registrations/edit.html.erb
create app/views/devise/registrations/new.html.erb
create app/views/devise/sessions/new.html.erb
create app/views/devise/shared/_links.erb
create app/views/devise/unlocks/new.html.erb
#/app/views/devise/sessions/new.html.erb
Sign in
<%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<% if devise_mapping.rememberable? -%>
<%= f.check_box :remember_me %> <%= f.label :remember_me %>
<% end -%>
<%= f.submit "Sign in" %>
<% end %>
<%= render :partial => "devise/shared/links" %>
#/app/views/devise/sessions/new.html.erb
<% title "Sign In" %>
<%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
- <%= f.label :email %> <%= f.text_field :email %>
- <%= f.label :password %> <%= f.password_field :password %>
<% if devise_mapping.rememberable? -%>
- <%= f.check_box :remember_me %> <%= f.label :remember_me %>
<% end %>
- <%= f.submit "Sign in" %>
<% end %>
<%= render :partial => "devise/shared/links" %>
#/config/locales/devise.en.yml
en:
errors:
messages:
not_found: "not found"
already_confirmed: "was already confirmed"
not_locked: "was not locked"
devise:
failure:
unauthenticated: 'You need to sign in or sign up before continuing.'
unconfirmed: 'You have to confirm your account before continuing.'
locked: 'Your account is locked.'
invalid: 'OH NOES! ERROR IN TEH EMAIL!'
invalid_token: 'Invalid authentication token.'
timeout: 'Your session expired, please sign in again to continue.'
inactive: 'Your account was not activated yet.'
sessions:
signed_in: 'Signed in successfully.'
signed_out: 'Signed out successfully.'
#rest of file omitted.
#/config/initalizers/devise.rb
# ==> Configuration for :validatable
# Range for password length
# config.password_length = 6..20
#/app/models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
# :confirmable,
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
end
#/config/routes.rb
ProjectManage::Application.routes.draw do |map|
devise_for :users
resources :projects
root :to => 'projects#index'
end
#/config/routes.rb
ProjectManage::Application.routes.draw do |map|
devise_for :users, :path_names => { :sign_up => "register" }
resources :projects
root :to => 'projects#index'
end
$ rails generate migration add_username_to_users username:string
$ rake db:migrate
$ rails c
Loading development environment (Rails 3.0.0.beta2)
ruby-1.8.7-p249 > User.first.update_attribute(:username, "eifion")
=> true
#/config/initializers/devise.rb
config.authentication_keys = [ :username ]
#/app/views/devise/sessions/new.html.erb
<% title "Sign In" %>
<%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
- <%= f.label :username %> <%= f.text_field :username %>
- <%= f.label :password %> <%= f.password_field :password %>
<% if devise_mapping.rememberable? -%>
- <%= f.check_box :remember_me %> <%= f.label :remember_me %>
<% end %>
- <%= f.submit "Sign in" %>
<% end %>
<%= render :partial => "devise/shared/links" %>