Search in sources :

Example 66 with GeneralResponse

use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.

the class DepartmentServiceImpl method modifyDepartment.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> modifyDepartment(DepartmentModifyRequest request) throws UnExpectedRequestException {
    request.checkRequest();
    Department departmentInDb = departmentDao.findById(request.getDepartmentId());
    if (departmentInDb == null) {
        LOGGER.error("Department donot exist.");
        throw new UnExpectedRequestException("Department {&DOES_NOT_EXIST}");
    }
    departmentInDb.setName(request.getDepartmentName());
    Department savedDepartment = departmentDao.saveDepartment(departmentInDb);
    LOGGER.info("Succeed to modify department, saved department info is : {}", savedDepartment.toString());
    return new GeneralResponse<>("200", "{&MODIFY_DEPARTMENT_SUCCESSFULLY}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Department(com.webank.wedatasphere.qualitis.entity.Department) Transactional(org.springframework.transaction.annotation.Transactional)

Example 67 with GeneralResponse

use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.

the class LoginServiceImpl method localLogin.

@Override
public GeneralResponse<?> localLogin(LocalLoginRequest request) throws LoginFailedException, UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    String username = request.getUsername();
    String password = request.getPassword();
    long currentLoginTime = System.currentTimeMillis();
    User userInDb = userDao.findByUsername(username);
    if (userInDb == null) {
        throw new LoginFailedException("{&USER_NOT_EXIST}");
    }
    if (userInDb.getLockTime() != null && (currentLoginTime - userInDb.getLockTime()) / (1000 * 60) < 10) {
        String lockTime = SDF.format(new Date(userInDb.getLockTime()));
        LOGGER.info("Login locked. user: {}, lock time: {}", username, lockTime);
        throw new LoginFailedException("{&LOGIN_LOCKED}" + lockTime);
    }
    if (localLogin(userInDb, password)) {
        addToSession(username, httpRequest);
        clearErrorLoginRecord(userInDb);
        userDao.saveUser(userInDb);
        LOGGER.info("Succeed to login. user: {}, current_user: {}", username, username);
        return new GeneralResponse<>("200", "{&LOGIN_SUCCESS}", null);
    } else {
        // Login failed in first time.
        if (userInDb.getLoginErrorTime() == null || userInDb.getLoginErrorCount() == null) {
            userInDb.setLoginErrorTime(currentLoginTime);
            userInDb.setLoginErrorCount(1);
        } else {
            // Check error count in 5 minutes decide to lock
            boolean consecutiveError = (currentLoginTime - userInDb.getLoginErrorTime()) / (1000 * 60) < 5;
            if (consecutiveError) {
                userInDb.setLoginErrorCount(userInDb.getLoginErrorCount() + 1);
                if (userInDb.getLoginErrorCount() >= 5) {
                    userInDb.setLockTime(currentLoginTime);
                }
            } else {
                userInDb.setLoginErrorTime(currentLoginTime);
                userInDb.setLoginErrorCount(1);
            }
        }
        userDao.saveUser(userInDb);
        throw new LoginFailedException("{&LOGIN_FAILED}" + (5 - userInDb.getLoginErrorCount()));
    }
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) LoginFailedException(com.webank.wedatasphere.qualitis.exception.LoginFailedException) User(com.webank.wedatasphere.qualitis.entity.User) Date(java.util.Date)

Example 68 with GeneralResponse

use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.

the class LoginServiceImpl method logout.

@Override
public GeneralResponse<?> logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
    String username = HttpUtils.getUserName(httpServletRequest);
    HttpSession session = httpServletRequest.getSession();
    session.removeAttribute("permissions");
    session.removeAttribute("user");
    session.removeAttribute("proxyUser");
    Cookie[] cookies = httpServletRequest.getCookies();
    // Clear cookies.
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            if ("JSESSIONID".equals(cookieName)) {
                cookieName = cookieName.replace("\r", "").replace("\n", "");
                Cookie newCookie = new Cookie(cookieName, null);
                newCookie.setSecure(true);
                newCookie.setMaxAge(0);
                newCookie.setPath("/");
                httpServletResponse.addCookie(newCookie);
            }
        }
    }
    LOGGER.info("Succeed to logout, user: {}, current_user: {}", username, username);
    String logoutUrl = frontEndConfig.getDomainName() + "/#/home";
    httpServletResponse.sendRedirect(logoutUrl);
    return new GeneralResponse<>("200", "{&LOGOUT_SUCCESSFULLY}", null);
}
Also used : Cookie(javax.servlet.http.Cookie) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) HttpSession(javax.servlet.http.HttpSession)

Example 69 with GeneralResponse

use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.

the class ApplicationServiceImpl method filterStatusApplication.

@Override
public GeneralResponse<?> filterStatusApplication(FilterStatusRequest request) throws UnExpectedRequestException {
    // Check arguments
    FilterStatusRequest.checkRequest(request);
    String userName = HttpUtils.getUserName(httpServletRequest);
    if (request.getStatus() != null) {
        LOGGER.info("User: {} wants to find applications with status: {}", userName, request.getStatus());
    } else {
        LOGGER.info("User: {} wants to find all applications", userName);
    }
    List<Application> applicationList;
    Long total;
    Integer page = request.getPage();
    Integer size = request.getSize();
    if (request.getStatus() == null || request.getStatus().intValue() == 0) {
        // Paging find applications by user
        long currentTimeUser = System.currentTimeMillis();
        applicationList = applicationDao.findByCreateUser(userName, page, size);
        LOGGER.info("timechecker find page application :" + (System.currentTimeMillis() - currentTimeUser));
        long currentTimeCountUser = System.currentTimeMillis();
        total = applicationDao.countByCreateUser(userName);
        LOGGER.info("timechecker count application :" + (System.currentTimeMillis() - currentTimeCountUser));
    } else {
        // Paging find applications by user and status
        applicationList = applicationDao.findByCreateUserAndStatus(userName, request.getStatus(), request.getCommentType(), page, size);
        total = applicationDao.countByCreateUserAndStatus(userName, request.getStatus(), request.getCommentType());
    }
    long currentTimeResponse = System.currentTimeMillis();
    GetAllResponse<ApplicationResponse> getAllResponse = new GetAllResponse<>();
    List<ApplicationResponse> applicationResponses = new ArrayList<>();
    for (Application application : applicationList) {
        List<Task> tasks = taskDao.findByApplication(application);
        ApplicationResponse response = new ApplicationResponse(application, tasks, httpServletRequest.getHeader("Content-Language"));
        if (application.getCreateUser().equals(userName) || application.getExecuteUser().equals(userName)) {
            response.setKillOption(true);
        } else {
            response.setKillOption(false);
        }
        applicationResponses.add(response);
    }
    getAllResponse.setData(applicationResponses);
    getAllResponse.setTotal(total);
    List<String> applicationIdList = getAllResponse.getData().stream().map(ApplicationResponse::getApplicationId).collect(Collectors.toList());
    LOGGER.info("timechecker response :" + (System.currentTimeMillis() - currentTimeResponse));
    LOGGER.info("Succeed to find applications. size: {}, id of applications: {}", total, applicationIdList);
    return new GeneralResponse<>("200", "{&SUCCEED_TO_GET_APPLICATIONS}", getAllResponse);
}
Also used : Task(com.webank.wedatasphere.qualitis.entity.Task) ApplicationResponse(com.webank.wedatasphere.qualitis.response.ApplicationResponse) ArrayList(java.util.ArrayList) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse) Application(com.webank.wedatasphere.qualitis.entity.Application)

Example 70 with GeneralResponse

use of com.webank.wedatasphere.qualitis.response.GeneralResponse in project Qualitis by WeBankFinTech.

the class ApplicationServiceImpl method filterProjectApplication.

@Override
public GeneralResponse<?> filterProjectApplication(FilterProjectRequest request) throws UnExpectedRequestException {
    // Check arguments
    FilterProjectRequest.checkRequest(request);
    Long userId = HttpUtils.getUserId(httpServletRequest);
    User user = userDao.findById(userId);
    Integer page = request.getPage();
    Integer size = request.getSize();
    Long projectId = request.getProjectId();
    int total = applicationDao.countByCreateUserAndProject(user.getUserName(), projectId).intValue();
    List<Application> applicationList = applicationDao.findByCreateUserAndProject(user.getUserName(), projectId, page, size);
    GetAllResponse<ApplicationResponse> getAllResponse = new GetAllResponse<>();
    List<ApplicationResponse> applicationResponses = new ArrayList<>();
    for (Application application : applicationList) {
        List<Task> tasks = taskDao.findByApplication(application);
        ApplicationResponse response = new ApplicationResponse(application, tasks, httpServletRequest.getHeader("Content-Language"));
        if (application.getCreateUser().equals(user.getUserName()) || application.getExecuteUser().equals(user.getUserName())) {
            response.setKillOption(true);
        } else {
            response.setKillOption(false);
        }
        applicationResponses.add(response);
    }
    getAllResponse.setData(applicationResponses);
    getAllResponse.setTotal(total);
    List<String> applicationIdList = getAllResponse.getData().stream().map(ApplicationResponse::getApplicationId).collect(Collectors.toList());
    LOGGER.info("Succeed to find applications. size: {}, id of applications: {}", total, applicationIdList);
    return new GeneralResponse<>("200", "{&SUCCEED_TO_GET_APPLICATIONS}", getAllResponse);
}
Also used : Task(com.webank.wedatasphere.qualitis.entity.Task) User(com.webank.wedatasphere.qualitis.entity.User) ApplicationResponse(com.webank.wedatasphere.qualitis.response.ApplicationResponse) ArrayList(java.util.ArrayList) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse) Application(com.webank.wedatasphere.qualitis.entity.Application)

Aggregations

GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)146 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)96 Transactional (org.springframework.transaction.annotation.Transactional)49 ArrayList (java.util.ArrayList)48 User (com.webank.wedatasphere.qualitis.entity.User)40 GetAllResponse (com.webank.wedatasphere.qualitis.response.GetAllResponse)30 Project (com.webank.wedatasphere.qualitis.project.entity.Project)28 Map (java.util.Map)27 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)26 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)24 List (java.util.List)22 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)21 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)17 IOException (java.io.IOException)16 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)15 Date (java.util.Date)15 Task (com.webank.wedatasphere.qualitis.entity.Task)14 JSONObject (org.json.JSONObject)14 HttpEntity (org.springframework.http.HttpEntity)14 HttpHeaders (org.springframework.http.HttpHeaders)14