cancel
Showing results for 
Search instead for 
Did you mean: 

calling change password rest api from a webscript without login

skumar_us
Champ in-the-making
Champ in-the-making
I have written a webscript to change the password of alfresco which need to be called from outside alfresco.
I have taken the webscript from change password module.The problem I am facing here is the webscript is registered as a share webscript . when I am calling this with the admin user ticket, it is returning false.
what are the changes need to be done in the controller script If I have to register it as a repo webscript?

/**
* User Profile Change Password Update method
*
* @method POST
*/

function main()
{
   var oldpass = args.oldpassword;
   var newpass1 = args.newpassword;
   logger.log("here");
  
   // ensure we have valid values and that the new passwords match
  
      // perform the REST API to change the user password
      var params = new Array(2);
      params["oldpw"] = oldpass;
      params["newpw"] = newpass1;
      var connector = remote.connect("alfresco");
      var result = connector.post(
            "/api/person/changepassword/" + args.username,
            jsonUtils.toJSONString(params),
            "application/json");
      if (result.status.code == 401)
      {
         model.success = false;
         if (result.status.message != "")
         {
            model.errormsg = result.status.message;
         }
         else
         {
            // The proxy has eaten the message
            // This is caused by some SSO compatibility code somewhere…
            model.errormsg = msg.get("message.passwordchangeauthfailed");
         }
      }
      else
      {
         var repoJSON = JSON.parse(result);
         if (repoJSON.success !== undefined)
         {
            model.success = repoJSON.success;
         }
         else
         {
            model.success = false;
            model.errormsg = repoJSON.message;
         }
      }
 
}

main();


<webscript>
  <shortname>Change Password POST</shortname>
  <description>Change user password POST form submission processing</description>
  <format default="json" />
  <url>/components/profile/ChangePasswordPostWebscript</url>
  <arg>
      <shortname>username</shortname>
   </arg>
   <arg>
      <shortname>oldpassword</shortname>
   </arg>
   <arg>
      <shortname>newpassword</shortname>
   </arg>
   <arg>
      <shortname>alf_ticket</shortname>
   </arg>
   
  <authentication>user</authentication>
</webscript>

<code>
{
   "success": ${success?string},
   "message": "<#if errormsg??>${errormsg}</#if>"
}
<code>
3 REPLIES 3

skumar_us
Champ in-the-making
Champ in-the-making
Hi, I am trying to call the rest url from client. I am getting the ticket ,but password is not getting updated,what I am missing here?


<code>
var cotextPath  = Alfresco.constants.PROXY_URI.replace("share/proxy/", "");
     var passwordObj =
                  {
                     newpw: "test1"
                  };
        passwordObj.oldpw = "test";
     var actionurl = cotextPath + "s/api/login?u="+encodeURIComponent(username) +"&pw="+password;
     Alfresco.util.Ajax.request({
           url: actionurl,
            successCallback:{
               fn: function(response){
                  var responseJSON = response.serverResponse.responseXML;
                var ticket = responseJSON.getElementsByTagName("ticket")[0].childNodes[0].nodeValue;
                if(ticket != ""){
            var getuserurl = Alfresco.constants.PROXY_URI + "api/person/changepassword/" + encodeURIComponent(username)+"/alf_ticket="+ticket;
            Alfresco.util.Ajax.request({
                  url: getuserurl,
                  method: Alfresco.util.Ajax.POST,
                 dataObj: passwordObj,
                   successCallback:{
                      fn: function(response){
                         var responseJSON = YAHOO.lang.JSON.parse(response.serverResponse.responseText);
                        
                      },
                      scope:this
                   },
                   failureCallback:{
                      fn: function(response){
                        Alfresco.util.PopupManager.displayPrompt(
                                {
                                    text: response.serverResponse.responseText
                                });
                      },
                      scope:this
                   }
                });

                }
               },
                scope:this
            },
            failureCallback:{
               fn: function(response){
                 Alfresco.util.PopupManager.displayPrompt(
                          {
                                 text: "Unable to retrieve the ticket with given Credential, Please Contact your system Administrator "
                           });
               },
               scope:this
            }
         });

</code>

skumar_us
Champ in-the-making
Champ in-the-making
I did try with appending the ticket with passwordobj also. it also did not work out

skumar_us
Champ in-the-making
Champ in-the-making
Solved this. problem was with the url.

var passwordObj =
           {
             newpw: "newpass"
           };
passwordObj.oldpw= "oldpass";
var updatepasswordurl = cotextPath + "service/api/person/changepassword/" + encodeURIComponent("username")+"?alf_ticket="+ticket;

Alfresco.util.Ajax.request({
            url: updatepasswordurl,
            method: Alfresco.util.Ajax.POST,
            requestContentType: Alfresco.util.Ajax.JSON,
            dataObj: passwordObj,
            successCallback:{
            fn: function(response){
            
   
            },
                scope:this
             },
             failureCallback:{
                fn: function(response){
                  Alfresco.util.PopupManager.displayPrompt(
                          {
                              text: response.serverResponse.responseText
                          });
                },
                scope:this
             }
          });