Skip to content

Commit

Permalink
fix(BaseEntity): Revert calling setters when hydrating entities
Browse files Browse the repository at this point in the history
Calling setters becomes problematic with actions like hashing
passwords.  The hashed password retrieved from the database
would be double hashed and no one could log in.  If you need
to do some sort of manipulation of values from the database,
use the `postLoad` lifecycle method.
  • Loading branch information
elpete committed Dec 11, 2019
1 parent 392120e commit 670fadb
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
10 changes: 2 additions & 8 deletions models/BaseEntity.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,8 @@ component accessors="true" {
}

arguments.attrs.each( function( key, value ) {
if ( variables.keyExists( "set" & retrieveAliasForColumn( arguments.key ) ) ) {
if ( ! isNullValue( arguments.key, arguments.value ) ) {
invoke( this, "set" & retrieveAliasForColumn( arguments.key ), { 1 = arguments.value } );
}
} else {
variables._data[ retrieveColumnForAlias( key ) ] = isNull( value ) ? javacast( "null", "" ) : value;
variables[ retrieveAliasForColumn( key ) ] = isNull( value ) ? javacast( "null", "" ) : value;
}
variables._data[ retrieveColumnForAlias( key ) ] = isNull( value ) ? javacast( "null", "" ) : value;
variables[ retrieveAliasForColumn( key ) ] = isNull( value ) ? javacast( "null", "" ) : value;
} );

return this;
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/integration/BaseEntity/AttributeSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
expect( user.getUsername() ).toBe( "new_username" );
} );

it( "sets attributes using the `setColumnName` magic methods during object creation", function() {
it( "does not set attributes using the `setColumnName` magic methods during object creation", function() {
var referral = getInstance( "Referral" ).findOrFail( 1 );
expect( referral.getType() ).toBeWithCase( "EXTERNAL", "type should be EXTERNAL in all caps thanks to a `setType` method on the `Referral` entity. Instead got [#referral.getType()#]." );
expect( referral.getType() ).toBeWithCase( "external", "type should be `external` in lowercase because the `setType` method on the `Referral` entity should not be called during creation. Instead got [#referral.getType()#]." );
} );

it( "can retrieve the original attributes of a loaded entity", function() {
Expand Down
6 changes: 3 additions & 3 deletions tests/specs/integration/BaseEntity/UpdateAllSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
} );

postA.refresh();
// postB.refresh();
postB.refresh();

// expect( postA.getBody() ).toBe( "The new body" );
// expect( postB.getBody() ).toBe( "The new body" );
expect( postA.getBody() ).toBe( "The new body" );
expect( postB.getBody() ).toBe( "The new body" );
} );
} );
}
Expand Down

0 comments on commit 670fadb

Please sign in to comment.