Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1b604c73
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1b604c73
编写于
5月 17, 2012
作者:
A
Aaron Patterson
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #6215 from erichmenge/fix_has_secure_password
Fix has secure password
上级
de29c308
f0213773
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
43 addition
and
15 deletion
+43
-15
activemodel/lib/active_model/secure_password.rb
activemodel/lib/active_model/secure_password.rb
+10
-5
activemodel/test/cases/secure_password_test.rb
activemodel/test/cases/secure_password_test.rb
+22
-8
activemodel/test/models/administrator.rb
activemodel/test/models/administrator.rb
+3
-0
activemodel/test/models/user.rb
activemodel/test/models/user.rb
+3
-0
activemodel/test/models/visitor.rb
activemodel/test/models/visitor.rb
+5
-2
未找到文件。
activemodel/lib/active_model/secure_password.rb
浏览文件 @
1b604c73
...
...
@@ -6,8 +6,9 @@ module ClassMethods
# Adds methods to set and authenticate against a BCrypt password.
# This mechanism requires you to have a password_digest attribute.
#
# Validations for presence of password, confirmation of password (using
# Validations for presence of password
on create
, confirmation of password (using
# a "password_confirmation" attribute) are automatically added.
# If you wish to turn off validations, pass 'validations: false' as an argument.
# You can add more validations by hand if need be.
#
# You need to add bcrypt-ruby (~> 3.0.0) to Gemfile to use has_secure_password:
...
...
@@ -31,7 +32,7 @@ module ClassMethods
# user.authenticate("mUc3m00RsqyRe") # => user
# User.find_by_name("david").try(:authenticate, "notright") # => false
# User.find_by_name("david").try(:authenticate, "mUc3m00RsqyRe") # => user
def
has_secure_password
def
has_secure_password
(
options
=
{})
# Load bcrypt-ruby only when has_secure_password is used.
# This is to avoid ActiveModel (and by extension the entire framework) being dependent on a binary library.
gem
'bcrypt-ruby'
,
'~> 3.0.0'
...
...
@@ -39,8 +40,12 @@ def has_secure_password
attr_reader
:password
if
options
.
fetch
(
:validations
,
true
)
validates_confirmation_of
:password
validates_presence_of
:password_digest
validates_presence_of
:password
,
:on
=>
:create
end
before_create
{
raise
"Password digest missing on new record"
if
password_digest
.
blank?
}
include
InstanceMethodsOnActivation
...
...
activemodel/test/cases/secure_password_test.rb
浏览文件 @
1b604c73
...
...
@@ -7,16 +7,19 @@ class SecurePasswordTest < ActiveModel::TestCase
setup
do
@user
=
User
.
new
@visitor
=
Visitor
.
new
end
test
"blank password"
do
@user
.
password
=
''
assert
!
@user
.
valid?
,
'user should be invalid'
@user
.
password
=
@visitor
.
password
=
''
assert
!
@user
.
valid?
(
:create
),
'user should be invalid'
assert
@visitor
.
valid?
(
:create
),
'visitor should be valid'
end
test
"nil password"
do
@user
.
password
=
nil
assert
!
@user
.
valid?
,
'user should be invalid'
@user
.
password
=
@visitor
.
password
=
nil
assert
!
@user
.
valid?
(
:create
),
'user should be invalid'
assert
@visitor
.
valid?
(
:create
),
'visitor should be valid'
end
test
"blank password doesn't override previous password"
do
...
...
@@ -26,15 +29,16 @@ class SecurePasswordTest < ActiveModel::TestCase
end
test
"password must be present"
do
assert
!
@user
.
valid?
assert
!
@user
.
valid?
(
:create
)
assert_equal
1
,
@user
.
errors
.
size
end
test
"
password must
match confirmation"
do
@user
.
password
=
"thiswillberight"
@user
.
password_confirmation
=
"wrong"
test
"match confirmation"
do
@user
.
password
=
@visitor
.
password
=
"thiswillberight"
@user
.
password_confirmation
=
@visitor
.
password_confirmation
=
"wrong"
assert
!
@user
.
valid?
assert
@visitor
.
valid?
@user
.
password_confirmation
=
"thiswillberight"
...
...
@@ -59,4 +63,14 @@ class SecurePasswordTest < ActiveModel::TestCase
assert
!
active_authorizer
.
include?
(
:password_digest
)
assert
active_authorizer
.
include?
(
:name
)
end
test
"User should not be created with blank digest"
do
assert_raise
RuntimeError
do
@user
.
run_callbacks
:create
end
@user
.
password
=
"supersecretpassword"
assert_nothing_raised
do
@user
.
run_callbacks
:create
end
end
end
activemodel/test/models/administrator.rb
浏览文件 @
1b604c73
class
Administrator
extend
ActiveModel
::
Callbacks
include
ActiveModel
::
Validations
include
ActiveModel
::
SecurePassword
include
ActiveModel
::
MassAssignmentSecurity
define_model_callbacks
:create
attr_accessor
:name
,
:password_digest
attr_accessible
:name
...
...
activemodel/test/models/user.rb
浏览文件 @
1b604c73
class
User
extend
ActiveModel
::
Callbacks
include
ActiveModel
::
Validations
include
ActiveModel
::
SecurePassword
define_model_callbacks
:create
has_secure_password
attr_accessor
:password_digest
,
:password_salt
...
...
activemodel/test/models/visitor.rb
浏览文件 @
1b604c73
class
Visitor
extend
ActiveModel
::
Callbacks
include
ActiveModel
::
Validations
include
ActiveModel
::
SecurePassword
include
ActiveModel
::
MassAssignmentSecurity
has_secure_password
define_model_callbacks
:create
attr_accessor
:password_digest
has_secure_password
(
validations:
false
)
attr_accessor
:password_digest
,
:password_confirmation
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录