Search in sources :

Example 1 with User

use of org.asqatasun.entity.user.User in project Asqatasun by Asqatasun.

the class AuditLauncherController method setUserParameters.

/**
 * Some user options have to be converted as parameters and added to the
 * general audit parameters.
 *
 * @param paramSet
 * @param referentialKey
 * @return
 */
private Set<Parameter> setUserParameters(Set<Parameter> paramSet, String referentialKey) {
    User user = getCurrentUser();
    Collection<OptionElement> optionElementSet = new HashSet<>();
    for (String optionFamily : USER_OPTION_DEPENDING_ON_REFERENTIAL) {
        optionElementSet.addAll(optionElementDataService.getOptionElementFromUserAndFamilyCode(user, referentialKey + "_" + optionFamily));
    }
    for (String optionFamily : userOption) {
        optionElementSet.addAll(optionElementDataService.getOptionElementFromUserAndFamilyCode(user, optionFamily));
    }
    paramSet.addAll(parameterDataService.getParameterSetFromOptionElementSet(optionElementSet));
    return paramSet;
}
Also used : User(org.asqatasun.entity.user.User) OptionElement(org.asqatasun.entity.option.OptionElement)

Example 2 with User

use of org.asqatasun.entity.user.User in project Asqatasun by Asqatasun.

the class ContractManagementController method submitAddContractAdminPage.

/**
 * @param createContractCommand
 * @param result
 * @param request
 * @param response
 * @param model
 * @return The pages audit set-up form page
 */
@RequestMapping(value = TgolKeyStore.ADD_CONTRACT_FROM_CONTRACT_MNGT_URL, method = RequestMethod.POST)
@Secured(TgolKeyStore.ROLE_ADMIN_KEY)
public String submitAddContractAdminPage(@ModelAttribute(TgolKeyStore.CREATE_CONTRACT_COMMAND_KEY) CreateContractCommand createContractCommand, BindingResult result, HttpServletRequest request, HttpServletResponse response, Model model) {
    Object userId = request.getSession().getAttribute(TgolKeyStore.USER_ID_KEY);
    Long lUserId;
    if (userId instanceof Long) {
        lUserId = (Long) userId;
    } else {
        try {
            lUserId = Long.valueOf(userId.toString());
        } catch (NumberFormatException nfe) {
            throw new ForbiddenUserException();
        }
    }
    Map<String, List<ContractOptionFormField>> optionFormFieldMap = ContractOptionFormFieldHelper.getFreshContractOptionFormFieldMap(contractOptionFormFieldBuilderMap);
    createContractFormValidator.setContractOptionFormFieldMap(optionFormFieldMap);
    // We check whether the form is valid
    createContractFormValidator.validate(createContractCommand, result);
    // If the form has some errors, we display it again with errors' details
    User currentModifiedUser = userDataService.read(lUserId);
    if (result.hasErrors()) {
        return displayFormWithErrors(model, createContractCommand, currentModifiedUser.getEmail1(), lUserId, optionFormFieldMap, TgolKeyStore.EDIT_CONTRACT_VIEW_NAME);
    }
    Contract contract = contractDataService.create();
    contract.setUser(currentModifiedUser);
    contract = createContractCommandFactory.updateContractFromCommand(createContractCommand, contract);
    saveOrUpdateContract(contract);
    request.getSession().setAttribute(TgolKeyStore.ADDED_CONTRACT_NAME_KEY, contract.getLabel());
    model.addAttribute(TgolKeyStore.USER_ID_KEY, contract.getUser().getId());
    request.getSession().removeAttribute(TgolKeyStore.USER_ID_KEY);
    return TgolKeyStore.MANAGE_CONTRACTS_VIEW_REDIRECT_NAME;
}
Also used : User(org.asqatasun.entity.user.User) List(java.util.List) ForbiddenUserException(org.asqatasun.webapp.exception.ForbiddenUserException) Contract(org.asqatasun.entity.contract.Contract) Secured(org.springframework.security.access.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with User

use of org.asqatasun.entity.user.User in project Asqatasun by Asqatasun.

the class ContractManagementController method displayManageContractsAdminPage.

/**
 * @param userId
 * @param request
 * @param model
 * @return The pages audit set-up form page
 */
@RequestMapping(value = TgolKeyStore.MANAGE_CONTRACTS_URL, method = RequestMethod.GET)
@Secured({ TgolKeyStore.ROLE_ADMIN_KEY })
public String displayManageContractsAdminPage(@RequestParam(TgolKeyStore.USER_ID_KEY) String userId, HttpServletRequest request, Model model) {
    Long lUserId;
    try {
        lUserId = Long.valueOf(userId);
    } catch (NumberFormatException nfe) {
        throw new ForbiddenUserException();
    }
    if (request.getSession().getAttribute(TgolKeyStore.DELETED_CONTRACT_NAME_KEY) != null) {
        model.addAttribute(TgolKeyStore.DELETED_CONTRACT_NAME_KEY, request.getSession().getAttribute(TgolKeyStore.DELETED_CONTRACT_NAME_KEY));
        request.getSession().removeAttribute(TgolKeyStore.DELETED_CONTRACT_NAME_KEY);
    }
    if (request.getSession().getAttribute(TgolKeyStore.DELETED_CONTRACT_AUDITS_NAME_KEY) != null) {
        model.addAttribute(TgolKeyStore.DELETED_CONTRACT_AUDITS_NAME_KEY, request.getSession().getAttribute(TgolKeyStore.DELETED_CONTRACT_AUDITS_NAME_KEY));
        request.getSession().removeAttribute(TgolKeyStore.DELETED_CONTRACT_AUDITS_NAME_KEY);
    }
    if (request.getSession().getAttribute(TgolKeyStore.UPDATED_CONTRACT_NAME_KEY) != null) {
        model.addAttribute(TgolKeyStore.UPDATED_CONTRACT_NAME_KEY, request.getSession().getAttribute(TgolKeyStore.UPDATED_CONTRACT_NAME_KEY));
        request.getSession().removeAttribute(TgolKeyStore.UPDATED_CONTRACT_NAME_KEY);
    }
    if (request.getSession().getAttribute(TgolKeyStore.ADDED_CONTRACT_NAME_KEY) != null) {
        model.addAttribute(TgolKeyStore.ADDED_CONTRACT_NAME_KEY, request.getSession().getAttribute(TgolKeyStore.ADDED_CONTRACT_NAME_KEY));
        request.getSession().removeAttribute(TgolKeyStore.ADDED_CONTRACT_NAME_KEY);
    }
    User userToManage = userDataService.read(lUserId);
    model.addAttribute(TgolKeyStore.CONTRACT_LIST_KEY, contractSortCommandHelper.prepareContract(userToManage, null, displayOptionFieldsBuilderList, model));
    model.addAttribute(TgolKeyStore.USER_NAME_KEY, userToManage.getEmail1());
    return TgolKeyStore.MANAGE_CONTRACTS_VIEW_NAME;
}
Also used : User(org.asqatasun.entity.user.User) ForbiddenUserException(org.asqatasun.webapp.exception.ForbiddenUserException) Secured(org.springframework.security.access.annotation.Secured) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with User

use of org.asqatasun.entity.user.User in project Asqatasun by Asqatasun.

the class ForgottenOrChangePasswordController method displayChangePasswordView.

/**
 * @param id
 * @param token
 * @param model
 * @param request
 * @return
 */
private String displayChangePasswordView(String id, String token, Model model, HttpServletRequest request) {
    Long userId;
    try {
        userId = Long.valueOf(id);
    } catch (NumberFormatException nfe) {
        throw new ForbiddenUserException();
    }
    if (StringUtils.isBlank(token)) {
        return TgolKeyStore.ACCESS_DENIED_VIEW_REDIRECT_NAME;
    }
    User currentUser = getCurrentUser();
    User user;
    // an admin
    if (token.equalsIgnoreCase(TgolKeyStore.AUTHENTICATED_KEY)) {
        if (currentUser == null || (!currentUser.getId().equals(userId) && !currentUser.getRole().getRoleName().equals(TgolKeyStore.ROLE_ADMIN_NAME_KEY)) || forbiddenUserList.contains(currentUser.getEmail1())) {
            return TgolKeyStore.ACCESS_DENIED_VIEW_REDIRECT_NAME;
        } else {
            if (!currentUser.getId().equals(userId)) {
                user = userDataService.read(userId);
            } else {
                user = currentUser;
            }
        }
    // the request is submitted through an unauthentified user and the token
    // has to be checked.
    } else {
        user = userDataService.read(userId);
        try {
            // if the token is invalid
            if (!tokenManager.checkUserToken(user.getEmail1(), token)) {
                model.addAttribute(TgolKeyStore.INVALID_CHANGE_PASSWORD_URL_KEY, true);
                return TgolKeyStore.CHANGE_PASSWORD_VIEW_NAME;
            } else {
                // if the token is valid but the request comes from the
                // form submission with success
                Object passwordModified = model.asMap().get(TgolKeyStore.PASSWORD_MODIFIED_KEY);
                if (passwordModified instanceof Boolean && (Boolean) passwordModified) {
                    tokenManager.setTokenUsed(token);
                    return TgolKeyStore.CHANGE_PASSWORD_VIEW_NAME;
                }
            }
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            model.addAttribute(TgolKeyStore.INVALID_CHANGE_PASSWORD_URL_KEY, true);
            return TgolKeyStore.CHANGE_PASSWORD_VIEW_NAME;
        }
    }
    if (user == null) {
        return TgolKeyStore.ACCESS_DENIED_VIEW_REDIRECT_NAME;
    }
    ChangePasswordCommand cpc = new ChangePasswordCommand();
    model.addAttribute(TgolKeyStore.CHANGE_PASSWORD_COMMAND_KEY, cpc);
    model.addAttribute(TgolKeyStore.USER_NAME_KEY, user.getEmail1());
    request.getSession().setAttribute(TgolKeyStore.USER_ID_KEY, user.getId());
    return TgolKeyStore.CHANGE_PASSWORD_VIEW_NAME;
}
Also used : ChangePasswordCommand(org.asqatasun.webapp.command.ChangePasswordCommand) User(org.asqatasun.entity.user.User) ForbiddenUserException(org.asqatasun.webapp.exception.ForbiddenUserException)

Example 5 with User

use of org.asqatasun.entity.user.User in project Asqatasun by Asqatasun.

the class ForgottenOrChangePasswordController method changePassword.

/**
 * @param changePasswordCommand
 * @param result
 * @param model
 * @param request
 * @param isrequestFromAdmin
 * @return
 * @throws Exception
 */
protected String changePassword(ChangePasswordCommand changePasswordCommand, BindingResult result, Model model, HttpServletRequest request, boolean isrequestFromAdmin) throws Exception {
    User user = userDataService.read((Long) request.getSession().getAttribute(TgolKeyStore.USER_ID_KEY));
    if (forbiddenUserList.contains(user.getEmail1())) {
        throw new ForbiddenPageException();
    }
    // We check whether the form is valid
    changePasswordFormValidator.validate(changePasswordCommand, result, user);
    // If the form has some errors, we display it again with errors' details
    if (result.hasErrors()) {
        model.addAttribute(TgolKeyStore.USER_NAME_KEY, user.getEmail1());
        return displayChangePasswordFormWithErrors(model, changePasswordCommand, isrequestFromAdmin);
    }
    request.getSession().removeAttribute(TgolKeyStore.USER_ID_KEY);
    model.addAttribute(TgolKeyStore.PASSWORD_MODIFIED_KEY, true);
    updateUserPassword(user, changePasswordCommand);
    if (isrequestFromAdmin) {
        return displayChangePasswordFromAdminPage(user.getId().toString(), request, model);
    } else {
        return displayChangePasswordFromUserPage(user.getId().toString(), request.getParameter("token"), request, model);
    }
}
Also used : User(org.asqatasun.entity.user.User) ForbiddenPageException(org.asqatasun.webapp.exception.ForbiddenPageException)

Aggregations

User (org.asqatasun.entity.user.User)40 Secured (org.springframework.security.access.annotation.Secured)14 ForbiddenUserException (org.asqatasun.webapp.exception.ForbiddenUserException)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 Contract (org.asqatasun.entity.contract.Contract)5 ForbiddenPageException (org.asqatasun.webapp.exception.ForbiddenPageException)5 Test (org.junit.Test)5 NoResultException (javax.persistence.NoResultException)4 Query (javax.persistence.Query)4 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)4 Test (org.junit.jupiter.api.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)4 Model (org.springframework.ui.Model)4 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)4 BindingResult (org.springframework.validation.BindingResult)4 List (java.util.List)2 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 ArrayList (java.util.ArrayList)1 OptionElement (org.asqatasun.entity.option.OptionElement)1