Subversion Repositories SmartDukaan

Rev

Rev 21812 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spice.profitmandi.admin.util.JWTUtil;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.model.UserInfo;
import com.spice.profitmandi.common.services.Department;
import com.spice.profitmandi.common.services.HelperServiceUtils;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.web.request.EmailPassword;

import in.shop2020.utils.Agent;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

@Controller
public class AuthController {

        private static final Logger LOGGER = LoggerFactory.getLogger(AuthController.class);
        

        @Autowired
        private ResponseSender<?> responseSender;
        
        @RequestMapping(value = "/login", method = RequestMethod.POST)
        public ResponseEntity<?> login(HttpServletRequest request, @RequestBody EmailPassword emailPassword) throws Throwable {
                
                Map<String, Object> responseMap = new HashMap<>();
                
                if(HelperServiceUtils.authenticate(emailPassword.getEmail(), emailPassword.getPassword())){
                        List<String> roles = HelperServiceUtils.getRolesByAgent(emailPassword.getEmail());
                        Agent agent = HelperServiceUtils.getAgent(emailPassword.getEmail());
                        responseMap.put(ProfitMandiConstants.ACCESS_TOKEN, JWTUtil.create(agent.getId(), roles.toArray(new String[(roles.size())])));
                        responseMap.put(ProfitMandiConstants.AUTHENTICATED, true);
                } else {
                        responseMap.put(ProfitMandiConstants.AUTHENTICATED, false);
                }
                
                return responseSender.ok(responseMap);
                
        }

        @RequestMapping(value = "/departments", method = RequestMethod.POST)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        public ResponseEntity<?> getRoles(@RequestAttribute("userInfo") UserInfo userInfo) throws Throwable {
                return responseSender.ok(Arrays.asList(Department.values()));
                
        }
        
}