<%

var log = new Log();

var util = require("../../util/utility.jag");
var serverUrl = util.getServerUrl() + "/UserAdmin";
var connectorStub = Packages.org.wso2.carbon.user.mgt.stub.UserAdminStub;
var UserAdminUserAdminException = Packages.org.wso2.carbon.user.mgt.stub.UserAdminUserAdminException;
var HTTPConstants = Packages.org.apache.axis2.transport.http.HTTPConstants;


function changePasswordByUser(userName, oldPassword, newPassword) {
    try {
        var serviceStub = new connectorStub(serverUrl);
        var serviceClient = serviceStub._getServiceClient();
        var option = serviceClient.getOptions();
        option.setManageSession(true);
        var cookie = session.get("auth-cookie");
        option.setProperty(HTTPConstants.COOKIE_STRING, cookie);
        serviceStub.changePasswordByUser(userName, oldPassword, newPassword);
    } catch (e) {
        var exception = e.javaException;
        if (exception instanceof UserAdminUserAdminException) {
            if (exception.getFaultMessage().getUserAdminException() != null) {
                var errorMsg = exception.getFaultMessage().getUserAdminException().getMessage();
                log.error(errorMsg);
                throw errorMsg;
            }
        }

        throw e;
    }
}

%>