-
Notifications
You must be signed in to change notification settings - Fork 979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #16850 - Added record of password change to audit log #3970
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,9 @@ class User < ActiveRecord::Base | |
|
||
validates_lengths_from_database :except => [:firstname, :lastname, :format, :mail, :login] | ||
attr_accessor :password, :password_confirmation, :current_password | ||
attr_reader :password_changed | ||
after_save :ensure_default_role | ||
after_save :unset_password_changed | ||
before_destroy EnsureNotUsedBy.new([:direct_hosts, :hosts]), :ensure_hidden_users_are_not_deleted, :ensure_last_admin_is_not_deleted | ||
|
||
belongs_to :auth_source | ||
|
@@ -88,6 +90,7 @@ def self.name_format | |
before_validation :verify_current_password, :if => Proc.new {|user| user == User.current}, | ||
:unless => Proc.new {|user| user.password.empty?} | ||
before_validation :prepare_password, :normalize_mail | ||
before_validation :set_password_changed, :if => Proc.new { |user| user.manage_password? && user.password.present? } | ||
before_save :set_lower_login | ||
|
||
after_create :welcome_mail | ||
|
@@ -447,6 +450,19 @@ def self.try_to_auto_create_user(login, password) | |
user | ||
end | ||
|
||
def password_changed_changed? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this necessary? Doesn't AM::Dirty provide it? |
||
changed.include?('password_changed') | ||
end | ||
|
||
def set_password_changed | ||
@password_changed = true | ||
attribute_will_change!('password_changed') | ||
end | ||
|
||
def unset_password_changed | ||
@password_changed = false | ||
end | ||
|
||
private | ||
|
||
def prepare_password | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -320,6 +320,71 @@ def setup_user(operation) | |
assert_includes record.errors.keys, :admin | ||
end | ||
|
||
context "audits for password change" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Block has too many lines. [57/25] |
||
def setup_user_for_audits | ||
user = FactoryGirl.create(:user) | ||
User.find_by_id(user.id) #to clear the value of user.password | ||
end | ||
|
||
test "audit of password change should be saved only once, second time audited changes should not contain password_changed" do | ||
user = setup_user_for_audits | ||
as_admin do | ||
user.password = "newpassword" | ||
assert_valid user | ||
assert user.password_changed_changed? | ||
assert user.password_changed | ||
assert_includes user.changed, "password_changed" | ||
assert user.save | ||
assert_includes Audit.last.audited_changes, "password_changed" | ||
#testing after_save | ||
refute user.password_changed_changed? | ||
refute user.password_changed | ||
refute_includes user.changed, "password_changed" | ||
end | ||
end | ||
|
||
test "audit of password change should be saved" do | ||
user = setup_user_for_audits | ||
as_admin do | ||
user.password = "newpassword" | ||
assert_valid user | ||
assert user.password_changed_changed? | ||
assert user.password_changed | ||
assert_includes user.changed, "password_changed" | ||
assert user.save | ||
assert_includes Audit.last.audited_changes, "password_changed" | ||
end | ||
end | ||
|
||
test "audit of password change should not be saved - due to no password change" do | ||
user = setup_user_for_audits | ||
as_admin do | ||
user.firstname = "Johnny" | ||
assert_valid user | ||
refute user.password_changed_changed? | ||
refute user.password_changed | ||
refute_includes user.changed, "password_changed" | ||
assert user.save | ||
refute_includes Audit.last.audited_changes, "password_changed" | ||
end | ||
end | ||
|
||
test "audit of name change sholud contain only firstname and not password_changed" do | ||
user = setup_user_for_audits | ||
as_admin do | ||
user.firstname = "Johnny" | ||
assert_valid user | ||
assert_includes user.changed, "firstname" | ||
refute user.password_changed_changed? | ||
refute user.password_changed | ||
refute_includes user.changed, "password_changed" | ||
assert user.save | ||
assert_includes Audit.last.audited_changes, "firstname" | ||
refute_includes Audit.last.audited_changes, "password_changed" | ||
end | ||
end | ||
end | ||
|
||
test "user can save user if he does not change roles" do | ||
setup_user "edit" | ||
record = users(:two) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing test coverage? A test should run two saves in a row and check for no second audit entry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhlavac When I see your tests - it looks like you're calling at most
.valid?
and.save
, so I'm not 100% positive this is ggcovered as per the comment above.