5-15 devise(git指南,部分认为重要的内容的译文)

维基百科上有how to 的很多文章。

Stackoverflow有大量问题的答案。

全的文档devise:API

 


7-20新增:

warden:https://github.com/wardencommunity/warden/wiki/overview

warden是一个devise依赖的框架。env["warden"]是一个环境对象,用于验证

见最下:


 

 

devise使用介绍:


 

一个Rails基于warden的灵活的验证解决方案

 

  • 完全MVC
  • 可以同时有多个models登入
  • 基于模块化原则: 只使用你想要的功能。 

 

由10个单独的module组成:

  • Database Authenticatable: hashes and stores a password in the database ,用于验证登陆的。
  • Omniauthable: adds OmniAuth support. ⚠️没看
  • Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
  • Recoverable: 密码忘记后的重置.
  • Registerable: 注册,也可以更新和删除账号,
  • Rememberable: manages generating and clearing a token for remembering the user from a saved cookie.
  • Trackable: tracks sign in count, timestamps and IP address.
  • Timeoutable: expires sessions that have not been active in a specified period of time.在一段时间后seesion过期注销。
  • Validatable: 提供验证邮件,密码,可选可客制化 
  • Lockable: 多次输入密码失败后🔒住, 可以通过email或者一段时间后🔓

 

安装:

 

  1. gem 'devise'
  2. rails generate devise:install   #这步骤后可以设置默认url
  3. rails generate devise [MODEL]   #一般用user或 member,生成model.
  4. rails db:migrate后从新启动rails server.
  5. 根据窗口提示:配置3条信息。

 

 


Controller filters and helpers

Devise定义了一些helper方法用在controllers和views中。

 

* 建立一个controller并进行用户验证,需要加上:

before_action :authenticate_user! 

   ⚠️, user是_your model name

 

* 确定用户是否登陆   

user_signed_in?

 

*  当前登陆用户

current_user 

 

* use_session 能够存取session。 

 

* 需要建立根路径 root  , 因为Devise需要根路径redirect to 

 


Configuring Models (有不少例子具体看操作手册。)

model中的Devise方法接受一些options来配置modules。

User model里面默认有一些Devise模块:

  devise :database_authenticatable, :registerable,

         :recoverable, :rememberable, :trackable, :validatable

 还有一些被注释掉的是可以选择的。

这些符号传给devise method能够使用Devise的功能features和假定了一系列的数据列columns,具体的list可以在migration file文件中看到。如(部分):

 

    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

 


 

Strong Parameters(高级的自定义设置)、

 

当客制化views的时候,你需要增加新的属性给forms。

有三个action可以在controller中使用:

There are just three actions in Devise that allow any set of parameters to be passed down to the model, therefore requiring sanitization.

Their names and default permitted parameters are:

在controller有三个动作注册,登入,账号更新, Devise允许你设置任何参数,然后传给model, 这称为sanitization。

  • sign_in(Devise::SessionController#create) -只有验证key可以被permit。(如like)
  • sign_up(Devise::RegistrationsController#create) - Permit 验证keys,password, password_confirmation
  • account_update( Devise::RegistrationsController#update) -Permit 验证keys,password, password_confirmation和 current_password。

如果你想要允许额外的参数(懒惰方法),可以使用before_action方法在ApplcationController:

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
  end
end

具体其他详细设置见,文档git...


 

Configuring views

后期客制化views, views打包在gem中,需要复制。

rails generate devise:views 

包括confirmations, mailer, passwords, registrations, sessions, shared, unlocks文件

因此可以选择生成部分视图:

rails generate devise:views -v registrations confirmations 

 

如果Devise有多个,User and Admin, Devise会用相同的views,但我们可以客制化views。

 

  1.  所需做的是在config/initializers/devise.rb中 config.scoped_views = true
  2.  然后就可以使用视图基于users/sessions/new和admins/sessions/new.
另外,也可以用生成器生成单独的views:

rails generate devise:views users

 

 


Configuring controllers

如果客制化视图不够,还可以客制化controller

 

第一步: rails g devise:controllers [scope] 

如果使用users作为scope,控制器创建app/controllers/users/.

另外;使用-c可以指定一个controller。(类似views -v)如:

rails generate devise:controllers users -c=sessions

总共6个控制器:confirmations_controller, omniauth_callbacks_controller, passwords_controller, registrations_controller, sessions_controller, unlocks_controller. 

 

第二步:在router.rb中使用这个controller

devise_for :users, controllers: {sessions: 'users/sessions' }

 

第三步:复制视图,从devise/sessions到users/sessions. 因为controller变了,默认的视图也需要从新定位。

 

第四步:最终,改变或扩展想要的controller actions.

 


 

 Configuring routes(详细的文档:devise_for

 

Devise也支持客制化路径。可以通过devise_for方法,它接受options如

class_name ,controllers大量自定义的配置。

 


 

Test

Devise includes some test helpers for controller and integration tests.

In order to use them, you need to include the respective module in your test cases/specs.

在spec目录内存放下面的代码,习惯是spec/support/devise.rb中,或者直接在rails_helper.rb/spec_helper.rb也行。

 

RSpec.configure do |config|
  config.include Devise::Test::ControllerHelper, type: :controller
  config.include Devise::Test::ControllerHelper, type: :view
  config.include Devise::Test::IntegrationHelper, type: :system
  config.include Devise::Test::IntegrationHelper, type: :request

  config.include Devise::Test::IntegrationHelper, type: :feature

#应该可以不写具体类型 

end
然后在控制器测试中可以用,不用自己写登陆代码了,不过控制器测试已被弃。
sign_in @user
sign_in @user, scope: :admin

用于集成测试的方法:

sign_in users(:bob)
sign_in users(:bob), scope: :admin

sign_out :user

 


Configuring multiple models

可以配置多个Devise model,用于不同的目的。比如只想要一个数据验证和计时timeout的功能的model.就可以这么设计:

# Create a migration with the required fields
create_table :admins do |t|
  t.string :email
  t.string :encrypted_password
  t.timestamps null: false
end

# Inside your Admin model
devise :database_authenticatable, :timeoutable

# Inside your routes
devise_for :admins

# Inside your protected controller
before_action :authenticate_admin!

# Inside your controllers and views
admin_signed_in?
current_admin
admin_session



wardon
warden是一个devise依赖的框架。env["warden"]是一个环境对象,用于验证

具体可看git,rubyChina上有相关帖子。
env['warden'].authenticate(:password)
env['warden'].user # the user object 






 

posted @ 2018-05-14 10:45  Mr-chen  阅读(393)  评论(0编辑  收藏  举报