<commit_before>package org.esupportail.smsu.business;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsu.dao.DaoService;
import org.esupportail.smsu.dao.beans.Message;
import org.esupportail.smsu.dao.beans.Person;
import org.esupportail.smsu.domain.beans.User;
import org.esupportail.smsu.domain.beans.message.MessageStatus;
import org.esupportail.smsu.exceptions.CreateMessageException;
import org.esupportail.smsu.web.beans.UIMessage;
import org.esupportail.smsu.web.controllers.InvalidParameterException;
import org.springframework.beans.factory.annotation.Autowired;

public class ApprovalManager {
	
	@Autowired private DaoService daoService;
	@Autowired private MessageManager messageManager;
	@Autowired private SendSmsManager sendSmsManager;

	@SuppressWarnings("unused")
	private final Logger logger = new LoggerImpl(getClass());

	public List<UIMessage> getApprovalUIMessages(HttpServletRequest request) {
		List<Message> messages = daoService.getApprovalMessages();
		if (!request.isUserInRole("FCTN_GESTIONS_RESPONSABLES"))
			messages = filterApprovalMessagesASupervisorCanApprove(messages, request.getRemoteUser());
		return messageManager.convertToUI(messages);
	}

	public void cancelOrApproveMessage(int messageId, User currentUser, MessageStatus newStatus) throws CreateMessageException {
		Message message = daoService.getMessageById(messageId);
		if (message == null)
			throw new InvalidParameterException("unknown message " + messageId);
		if (!message.getStateAsEnum().equals(MessageStatus.WAITING_FOR_APPROVAL))
			throw new InvalidParameterException("message is not waiting for approval");
		checkCanApprove(message, currentUser.getId());

		sendSmsManager.sendMailMessageApprovedOrCanceled(message, newStatus, currentUser);
		message.setStateAsEnum(newStatus);
		if (newStatus.equals(MessageStatus.CANCEL)) {
			daoService.updateMessage(message);
		} else {
			sendSmsManager.treatMessage(message);
		}
	}

	private List<Message> filterApprovalMessagesASupervisorCanApprove(List<Message> msgs, String user) {
		Person p = daoService.getPersonByLogin(user);
		List<Message> messages = new ArrayList<Message>();
		for (Message mess : msgs)
			if (mess.getSupervisors().contains(p))
				messages.add(mess);
		return messages;
	}
	
	private void checkCanApprove(Message msg, String userId) {
		Person p = daoService.getPersonByLogin(userId);
		if (!msg.getSupervisors().contains(p))
			throw new InvalidParameterException("user " + userId + " is not allowed to approve this message");
	}	

}
<commit_msg>ApprovalManager: we must allow RESPONSABLES to approve a msg (with JSF checking it was only needed when creating the list of msgs to approve)<commit_after>package org.esupportail.smsu.business;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.esupportail.commons.services.logging.Logger;
import org.esupportail.commons.services.logging.LoggerImpl;
import org.esupportail.smsu.dao.DaoService;
import org.esupportail.smsu.dao.beans.Message;
import org.esupportail.smsu.dao.beans.Person;
import org.esupportail.smsu.domain.beans.User;
import org.esupportail.smsu.domain.beans.message.MessageStatus;
import org.esupportail.smsu.exceptions.CreateMessageException;
import org.esupportail.smsu.web.beans.UIMessage;
import org.esupportail.smsu.web.controllers.InvalidParameterException;
import org.springframework.beans.factory.annotation.Autowired;

public class ApprovalManager {
	
	@Autowired private DaoService daoService;
	@Autowired private MessageManager messageManager;
	@Autowired private SendSmsManager sendSmsManager;

	@SuppressWarnings("unused")
	private final Logger logger = new LoggerImpl(getClass());

	public List<UIMessage> getApprovalUIMessages(HttpServletRequest request) {
		List<Message> messages = daoService.getApprovalMessages();
		if (!request.isUserInRole("FCTN_GESTIONS_RESPONSABLES"))
			messages = filterApprovalMessagesASupervisorCanApprove(messages, request.getRemoteUser());
		return messageManager.convertToUI(messages);
	}

	public void cancelOrApproveMessage(int messageId, User currentUser, MessageStatus newStatus) throws CreateMessageException {
		Message message = daoService.getMessageById(messageId);
		if (message == null)
			throw new InvalidParameterException("unknown message " + messageId);
		if (!message.getStateAsEnum().equals(MessageStatus.WAITING_FOR_APPROVAL))
			throw new InvalidParameterException("message is not waiting for approval");
		checkCanApprove(message, currentUser);

		sendSmsManager.sendMailMessageApprovedOrCanceled(message, newStatus, currentUser);
		message.setStateAsEnum(newStatus);
		if (newStatus.equals(MessageStatus.CANCEL)) {
			daoService.updateMessage(message);
		} else {
			sendSmsManager.treatMessage(message);
		}
	}

	private List<Message> filterApprovalMessagesASupervisorCanApprove(List<Message> msgs, String user) {
		Person p = daoService.getPersonByLogin(user);
		List<Message> messages = new ArrayList<Message>();
		for (Message mess : msgs)
			if (mess.getSupervisors().contains(p))
				messages.add(mess);
		return messages;
	}
	
	private void checkCanApprove(Message msg, User user) {
		if (user.rights.contains("FCTN_GESTIONS_RESPONSABLES"))
			return;
		
		Person p = daoService.getPersonByLogin(user.getId());
		if (!msg.getSupervisors().contains(p))
			throw new InvalidParameterException("user " + user.getId() + " is not allowed to approve this message");
	}	

}

{'fraction_non_alphanumeric': 0.09635011243729459, 'fraction_numerical': 0.0, 'mean_word_length': 5.474804031354983, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1889749', 'n_tokens_mistral': 1804, 'n_tokens_neox': 1756, 'n_words': 330}