php - How to prevent user from entering previous passwords in a new password field using ajax in sugarcrm? ← (PHP, JavaScript)

I want to add some JavaScript validations in existing JavaScript validations when user changes his password.

Now, I want to use AJAX to connect with PHP file which runs db query against the current user and check whether the entered password in new_password field is matches with his last 10 passwords.

Now, I found out those function which SugarCRM uses to match passwords with the hashed passwords stored in database. But, the problem is I don't know how to use AJAX to connect to PHP file in SugarCRM.

I want to add JavaScript validation using onblur function. When user enter his new password, using onblur, I want to send that password value to PHP file using AJAX and return that value. And when user click save button, it should give alert box if his new password matches with his last passwords and if not it should allow him to change his password.

Can anyone here guide me soon? Any help is appreciated.

Answer



Solution:

First of all you have to stored last 10 passwords of the users because in default behavior of Sugar you will only get Old password and New password.

For AJAX you have to create an action in Users module with the help of controller or just create file actionName.php in custom/modules/Users. (Ref Link)

var URL = "index.php?module=Users&action=actionName";
YAHOO.util.Connect.asyncRequest('POST', URL, callBack);
var callBack = {
   success: function(){},
   failure:function(){}
}

You can use this code for AJAX call or you can also fellow this Link

Answer



Solution:

I have managed to find the answer as I want. I am sharing my code with you all.

if(form.new_password.value!=''){
        $.ajax({
            url:'index.php?entryPoint=check_last_passwords',
            data:{new_password: $('#new_password').val(),
            record: $('#record').val()},
            success: function(data){
                if(data!=''){
                    alert("Error: New password should not match with the last 10 passwords.");
                    return false;
                }else{
                    var _form = $('#EditView')[0];
                    if (!Admin_check())
                        return false;
                    _form.action.value='Save';
                    set_chooser();
                    if(verify_data(EditView))
                        _form.submit();
                }
            }
        });
    }

Source