File tree Expand file tree Collapse file tree 6 files changed +32
-0
lines changed
Expand file tree Collapse file tree 6 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change @@ -41,8 +41,12 @@ account.password_hash #=> "$2a$12$k/Ub1I2iomi84RacqY89Hu4.M0vK7klRnRtzorDyvOkVI.
4141account.password_hash # => #<Account::PasswordHash...> (record from `account_password_hashes` table)
4242account.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+
4447account.password = nil # clears password hash
4548account.password_hash # => nil
49+ account.password? # => false
4650```
4751
4852Note 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.
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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"
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"
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
Original file line number Diff line number Diff line change 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"
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"
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
You can’t perform that action at this time.
0 commit comments