Search in sources :

Example 31 with User

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

the class AbstractAuditSetUpController method isUserAllowedToDisplaySetUpPage.

/**
 * This methods checks whether the current user is allowed to display the
 * audit set-up for a given contract. To do so, we verify that the contract
 * belongs to the current user. We also check that the current contract handles
 * the functionality associated with the set-up page.
 *
 * @param contract
 * @param viewName
 * @return
 *      true if the user is allowed to display the result, false otherwise.
 */
protected boolean isUserAllowedToDisplaySetUpPage(Contract contract, String viewName) {
    if (contract == null) {
        throw new ForbiddenPageException(getCurrentUser());
    }
    User user = getCurrentUser();
    if (!contract.getUser().getId().equals(user.getId())) {
        throw new ForbiddenPageException(user);
    }
    Collection<String> functionalitySet = getAuthorisedFunctionalityCodeFromContract(contract);
    if (!functionalitySet.contains(getViewFunctionalityBindingMap().get(viewName))) {
        throw new ForbiddenPageException(user);
    }
    return true;
}
Also used : User(org.asqatasun.entity.user.User) ForbiddenPageException(org.asqatasun.webapp.exception.ForbiddenPageException)

Example 32 with User

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

the class AbstractUserAndContractsController method submitUpdateUserForm.

/**
 * This methods controls the validity of the form and updated the user
 *
 * @param createUserCommand
 * @param result
 * @param request
 * @param model
 * @param userToModify
 * @param successViewName
 * @param errorViewName
 * @param updateAllUserData
 * @param updateUserFromAdmin
 * @param successMessageKey
 * @return
 * @throws Exception
 */
protected String submitUpdateUserForm(CreateUserCommand createUserCommand, BindingResult result, HttpServletRequest request, Model model, User userToModify, String successViewName, String errorViewName, boolean updateAllUserData, boolean updateUserFromAdmin, String successMessageKey) throws Exception {
    // We check whether the form is valid
    createUserFormValidator.validateUpdate(createUserCommand, result, userToModify.getEmail1());
    // If the form has some errors, we display it again with errors' details
    if (result.hasErrors()) {
        return displayFormWithErrors(model, createUserCommand, errorViewName);
    }
    User user = updateUser(createUserCommand, userToModify, updateAllUserData, updateUserFromAdmin);
    // through the session and needs to be cleaned up once updated.
    if (updateUserFromAdmin) {
        model.addAttribute(TgolKeyStore.USER_LIST_KEY, userDataService.findAll());
        request.getSession().removeAttribute(TgolKeyStore.USER_ID_KEY);
    }
    if (successMessageKey != null) {
        model.addAttribute(successMessageKey, user.getEmail1());
    }
    return successViewName;
}
Also used : User(org.asqatasun.entity.user.User)

Example 33 with User

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

the class UserManagementController method displayEditUserAdminPage.

/**
 * @param userId
 * @param request
 * @param response
 * @param model
 * @return The pages audit set-up form page
 */
@RequestMapping(value = TgolKeyStore.EDIT_USER_URL, method = RequestMethod.GET)
@Secured(TgolKeyStore.ROLE_ADMIN_KEY)
public String displayEditUserAdminPage(@RequestParam(TgolKeyStore.USER_ID_KEY) String userId, HttpServletRequest request, HttpServletResponse response, Model model) {
    Long lUserId;
    try {
        lUserId = Long.valueOf(userId);
    } catch (NumberFormatException nfe) {
        throw new ForbiddenUserException();
    }
    User userToModify = userDataService.read(lUserId);
    model.addAttribute(TgolKeyStore.USER_NAME_KEY, userToModify.getEmail1());
    request.getSession().setAttribute(TgolKeyStore.USER_ID_KEY, lUserId);
    return prepateDataAndReturnCreateUserView(model, userToModify, TgolKeyStore.EDIT_USER_VIEW_NAME);
}
Also used : User(org.asqatasun.entity.user.User) ForbiddenUserException(org.asqatasun.webapp.exception.ForbiddenUserException) Secured(org.springframework.security.access.annotation.Secured)

Example 34 with User

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

the class UserManagementController method submitAddContractAdminPage.

/**
 * @param ccc the CreateContractCommand
 * @param result
 * @param request
 * @param response
 * @param model
 * @return The pages audit set-up form page
 */
@RequestMapping(value = TgolKeyStore.ADD_CONTRACT_URL, method = RequestMethod.POST)
@Secured(TgolKeyStore.ROLE_ADMIN_KEY)
public String submitAddContractAdminPage(@ModelAttribute(TgolKeyStore.CREATE_CONTRACT_COMMAND_KEY) CreateContractCommand ccc, BindingResult result, HttpServletRequest request, HttpServletResponse response, Model model) {
    Map<String, List<ContractOptionFormField>> optionFormFieldMap = ContractOptionFormFieldHelper.getFreshContractOptionFormFieldMap(contractOptionFormFieldBuilderMap);
    createContractFormValidator.setContractOptionFormFieldMap(optionFormFieldMap);
    // We check whether the form is valid
    createContractFormValidator.validateMultipleUsers(ccc, result);
    if (result.hasErrors()) {
        return displayFormWithErrors(model, ccc, null, null, optionFormFieldMap, TgolKeyStore.ADD_CONTRACT_VIEW_NAME);
    }
    Collection<User> userList = ccc.getUserList();
    StringBuilder strb = new StringBuilder();
    for (User user : userList) {
        if (user != null) {
            Contract contract = contractDataService.create();
            contract.setUser(user);
            contract = createContractCommandFactory.updateContractFromCommand(ccc, contract);
            contractDataService.saveOrUpdate(contract);
            strb.append(user.getEmail1());
            strb.append(", ");
        }
    }
    request.getSession().setAttribute(TgolKeyStore.ADDED_CONTRACT_NAME_KEY, ccc.getLabel());
    request.getSession().setAttribute(TgolKeyStore.ADDED_CONTRACT_USERS_NAME_KEY, strb.toString());
    return TgolKeyStore.ADMIN_VIEW_REDIRECT_NAME;
}
Also used : User(org.asqatasun.entity.user.User) List(java.util.List) Contract(org.asqatasun.entity.contract.Contract) Secured(org.springframework.security.access.annotation.Secured)

Example 35 with User

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

the class UserManagementController method displayDeleteUserAuditsConfirmationPage.

/**
 * @param request
 * @param response
 * @param model
 * @return the name of the view that displays the confirmation page
 * when trying to delete all the audits of a user
 */
@RequestMapping(value = TgolKeyStore.DELETE_USER_AUDITS_URL, method = RequestMethod.POST)
@Secured(TgolKeyStore.ROLE_ADMIN_KEY)
public String displayDeleteUserAuditsConfirmationPage(HttpServletRequest request, HttpServletResponse response, Model model) {
    Object userId = request.getSession().getAttribute(TgolKeyStore.USER_ID_TO_DELETE_KEY);
    Long lUserId;
    if (userId instanceof Long) {
        lUserId = (Long) userId;
    } else {
        try {
            lUserId = Long.valueOf(userId.toString());
        } catch (NumberFormatException nfe) {
            throw new ForbiddenUserException();
        }
    }
    User userToDelete = userDataService.read(lUserId);
    for (Contract contract : userToDelete.getContractSet()) {
        deleteAllAuditsFromContract(contract);
    }
    request.getSession().removeAttribute(TgolKeyStore.USER_ID_TO_DELETE_KEY);
    request.getSession().setAttribute(TgolKeyStore.DELETED_USER_AUDITS_KEY, userToDelete.getEmail1());
    return TgolKeyStore.ADMIN_VIEW_REDIRECT_NAME;
}
Also used : User(org.asqatasun.entity.user.User) ForbiddenUserException(org.asqatasun.webapp.exception.ForbiddenUserException) Contract(org.asqatasun.entity.contract.Contract) Secured(org.springframework.security.access.annotation.Secured)

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