Skip to content

Commit d6eea52

Browse files
committed
Add #password? method
It's shorter and nicer way than `#password_hash` to check whether a password is set.
1 parent 82cbcc3 commit d6eea52

File tree

6 files changed

+32
-0
lines changed

6 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.0 (2024-12-16)
2+
3+
* Add `password?` model method that returns whether a password is set (@janko)
4+
15
## 0.3.0 (2024-10-12)
26

37
* Add support for OTP Unlock feature (@janko)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ account.password_hash #=> "$2a$12$k/Ub1I2iomi84RacqY89Hu4.M0vK7klRnRtzorDyvOkVI.
4141
account.password_hash #=> #<Account::PasswordHash...> (record from `account_password_hashes` table)
4242
account.password_hash.password_hash #=> "$2a$12$k/Ub1..." (inaccessible when using database authentication functions)
4343

44+
# whether a password is set
45+
account.password? #=> true
46+
4447
account.password = nil # clears password hash
4548
account.password_hash #=> nil
49+
account.password? #=> false
4650
```
4751

4852
Note that the password attribute doesn't come with validations, making it unsuitable for forms. It was primarily intended to allow easily creating accounts in development console and in tests.

lib/rodauth/model/active_record.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ def define_methods(model)
3232
end
3333
end
3434
end
35+
36+
define_method(:password?) do
37+
if rodauth.account_password_hash_column
38+
!!public_send(rodauth.account_password_hash_column)
39+
else
40+
!!password_hash
41+
end
42+
end
3543
end
3644

3745
def define_associations(model)

lib/rodauth/model/sequel.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def define_methods(model)
4242
self.password_hash_attributes = attributes
4343
end
4444
end
45+
46+
define_method(:password?) do
47+
if rodauth.account_password_hash_column
48+
!!public_send(rodauth.account_password_hash_column)
49+
else
50+
!!password_hash
51+
end
52+
end
4553
end
4654

4755
def define_associations(model)

test/active_record_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
account.password = "secret"
3232
assert_equal "secret", account.password
33+
assert_equal true, account.password?
3334

3435
refute_nil account.password_hash
3536
assert_operator BCrypt::Password.new(account.password_hash), :==, "secret"
@@ -42,13 +43,15 @@
4243

4344
account.password = nil
4445
assert_nil account.password_hash
46+
assert_equal false, account.password?
4547
end
4648

4749
it "defines password attribute with a table" do
4850
account = build_account { account_password_hash_column nil }
4951

5052
account.password = "secret"
5153
assert_equal "secret", account.password
54+
assert_equal true, account.password?
5255

5356
assert_instance_of Account::PasswordHash, account.password_hash
5457
assert_operator BCrypt::Password.new(account.password_hash.password_hash), :==, "secret"
@@ -75,6 +78,7 @@
7578

7679
account.reload
7780
assert_nil account.password_hash
81+
assert_equal false, account.password?
7882
end
7983

8084
it "doesn't select password hash column when using database authentication functions" do

test/sequel_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
account.password = "secret"
2323
assert_equal "secret", account.password
24+
assert_equal true, account.password?
2425

2526
refute_nil account.password_hash
2627
assert_operator BCrypt::Password.new(account.password_hash), :==, "secret"
@@ -33,13 +34,15 @@
3334

3435
account.password = nil
3536
assert_nil account.password_hash
37+
assert_equal false, account.password?
3638
end
3739

3840
it "defines password attribute with a table" do
3941
account = build_account { account_password_hash_column nil }
4042

4143
account.password = "secret"
4244
assert_equal "secret", account.password
45+
assert_equal true, account.password?
4346

4447
assert_instance_of Account::PasswordHash, account.password_hash
4548
assert_operator BCrypt::Password.new(account.password_hash.password_hash), :==, "secret"
@@ -66,6 +69,7 @@
6669

6770
account.reload
6871
assert_nil account.password_hash
72+
assert_equal false, account.password?
6973
end
7074

7175
it "doesn't select password hash column when using database authentication functions" do

0 commit comments

Comments
 (0)