Subversion Repositories SmartDukaan

Rev

Rev 2949 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/**
 * 
 */
package in.shop2020.serving.controllers;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.serving.utils.DesEncrypter;
import in.shop2020.thrift.clients.UserClient;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.thrift.TException;

/**
 * @author rajveer
 *
 */

@InterceptorRefs({
    @InterceptorRef("myDefault"),
    @InterceptorRef("login")
})

@Results({
    @Result(name="success", type="redirectAction", 
                params = {"actionName" , "login-details"})
})
public class LoginDetailsController extends BaseController {

        private static final long serialVersionUID = 1L;

        private static Logger log = Logger.getLogger(Class.class);
        private DesEncrypter desEncrypter = new DesEncrypter("saholic");
        
        public LoginDetailsController() {
                super();
        }
        
    // POST /logindetails
    public String create() {
        log.info("LogindetailsController.create");

                
                String email = this.request.getParameter("txtLoginID");
                String oldPassword = this.request.getParameter("txtOldPassword");
                String newPassword = this.request.getParameter("txtNewPassword");
                
                if(this.userinfo.isLoggedIn()){
                        if(changePassword(userinfo.getUserId(), email, desEncrypter.encrypt(oldPassword), desEncrypter.encrypt(newPassword)))
                        {
                                addActionMessage("Your password is updated successfully.");
                                return "success";
                        }
        }
                addActionError("Unable to update password. Either email or password is not correct.");
                return "success";
    }

    
    // GET /test
    public String index() throws UnsupportedEncodingException {
        log.info("this.request=" + this.request);
        
                htmlSnippets.put("MYACCOUNT_HEADER",
                                pageLoader.getMyaccountHeaderHtml());
                htmlSnippets.put("LOGIN_DETAILS",
                                pageLoader.getLoginDetailsHtml(userinfo.getUserId()));
                return "index";
    }
    
    
        public boolean changePassword(long userId, String email, String oldPassword, String newPassword) {

                
                try {
                        UserClient userContextServiceClient = new UserClient();
                        in.shop2020.model.v1.user.UserContextService.Client userClient = userContextServiceClient.getClient();

                        return userClient.updatePassword(userId,oldPassword, newPassword);
                } catch (UserContextException e) {
                        log.error("Unable to update password", e);
                } catch (TException e) {
                    log.error("Unable to update password", e);
                } catch (Exception e) {
                    log.error("Unable to update password", e);
                }
                return false;
        }



        public String getMyaccountHeaderSnippet(){
                return htmlSnippets.get("MYACCOUNT_HEADER");
        }
        
        public String getLoginDetailsSnippet(){
                return htmlSnippets.get("LOGIN_DETAILS");
        }
        
        public String getEmail(){
        return userinfo.getEmail();
    }
    
}