getting and setting ENCODE / DECODE 'Column' function to AJax Sourced data
getting and setting ENCODE / DECODE 'Column' function to AJax Sourced data
I have an ajax sourced datatable with the use of the Editor plugin. The problem is i have a 'Password' field which needs to be encoded (with key value) upon new entry and decoded when editing the column. I have looked but can't find how to do this using the code.
This discussion has been closed.
Replies
Assuming you are using the Editor PHP libraries (?) you would use the getFormatter and setFormatter methods of the Field instance for the password field: http://editor.datatables.net/docs/current/php/class-DataTables.Editor.Field.html .
These will effectively allow you to "transcode" between values, using whatever logic operation you wish.
Regards,
Allan
[code]
public static function decode_password( $val, $data, $opts ) {
if ( $val ) {
return DECODE( $val, 'secretkey' );
}
return '';
}
public static function encode_password( $val, $data, $opts ) {
if ( $val ) {
return ENCODE( $val, 'secretkey' );
}
return '';
}
[/code]
Am i right in saying returning the value here automatically inputs into the SQL query?
It would be remiss of me to not note that you'll be transmitting unencrypted passwords over http(s). I'm sure you'll be well aware of this and the security implications of using encrypted passwords rather than hashed, but I have to point it out all the same :-)
Regards,
Allan
I'm afraid there isn't even a quick work around that I can see at the moment. You will probably need to write some custom queries in PHP.
Allan
Allan
I have been looking at the server_processing code on Datatables, it seems alright for the front end but in terms of the Add / Edit functionality, how can i incorporate this with Editor? Also how easy would it be to change the ajax source code to utilise my own sql queries once a record has been updated / inserted?
Please ignore my previous comment. I have fixed part of my issue as you will see bwlow.
Editor Lib -> Format.php
[code]
public static function decode_password( $val, $data, $opts )
{
if ( $val )
{
$conn = mysqli_connect("", "", "", "");
if (mysqli_connect_errno())
{
return "Failed to connect to Database" . mysqli_connect_error();
}
$sql = "SELECT DECODE(password,'".$opts."') AS 'passDecode' FROM table WHERE ID = ? LIMIT 1";
if ($result = mysqli_query($conn,$sql))
{
$obj = mysqli_fetch_object($result);
return $obj->passDecode;
mysqli_free_result($result);
}
mysqli_close($conn);
}
return '';
}
public static function encode_password( $val, $data, $opts )
{
if ( $val )
{
$conn = mysqli_connect("", "", "", "");
if (mysqli_connect_errno())
{
return "Failed to connect to Database" . mysqli_connect_error();
}
$sql = "SELECT ENCODE('".$val."','".$opts."') AS 'passEncode'";
if ($result = mysqli_query($conn,$sql))
{
$obj = mysqli_fetch_object($result);
return $obj->passEncode;
mysqli_free_result($result);
}
mysqli_close($conn);
}
return '';
}
[/code]
Editor -> ajax script
[code]
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'table', 'ID' )
->fields(
Field::inst( 'username' ),
Field::inst( 'password' )
->getFormatter( 'Format::decode_password', 'key' )
->setFormatter( 'Format::encode_password', 'key' )
)
->process( $_POST )
->json();
[/code]
It's not pretty but seems to be working, Could you tell me how i would pass the ID of the record from the ajax script to the Format.php page, i need to get the ID field in-order to decode my password.
> Could you tell me how i would pass the ID of the record from the ajax script to the Format.php page, i need to get the ID field in-order to decode my password.
It will actually already be available in the $data (second parameter) that is passed into the formatter. That contains the data for the whole row, so `$row['DT_RowId']` will be the ID.
Allan