SecurityEndpoint.java

/*
 * Copyright 2019 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gringlobal.soap;

import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.genesys.blocks.security.UserException;
import org.gringlobal.compatibility.service.DataviewService;
import org.gringlobal.compatibility.service.UserService;
import org.gringlobal.soap.model.ChangeLanguage;
import org.gringlobal.soap.model.ChangePassword;
import org.gringlobal.soap.model.TransferOwnership;
import org.gringlobal.soap.model.ValidateLogin;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
@Slf4j
public class SecurityEndpoint extends BaseEndpoint {

	@Autowired
	private UserService userService;
	@Autowired
	private DataviewService dataviewService;

	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "ValidateLogin")
	@ResponsePayload
	public Element login(@RequestPayload final ValidateLogin request) {

		log.info("Received login request: {}", request.getUserName());

		final Element response = new Element("ValidateLoginResponse", GGXml.NAMESPACE_URI);
		response.addContent(userService.login(request.getUserName()).toDiffgram("ValidateLoginResult", GGXml.NAMESPACE_URI));
		return response;
	}

	@PreAuthorize("hasAuthority('GROUP_CTUSERS') || hasAuthority('GROUP_ADMINS')")
	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "ChangePassword")
	@ResponsePayload
	public Element setPassword(@RequestPayload final ChangePassword request) throws UserException {

		log.info("Received change password request by {} for {}", request.getUserName(), request.getTargetUserName());

		final Element response = new Element("ChangePasswordResponse", GGXml.NAMESPACE_URI);
		response.addContent(userService.setPassword(request.getTargetUserName(), request.getNewPassword()).toDiffgram("ChangePasswordResult", GGXml.NAMESPACE_URI));
		return response;
	}

	@PreAuthorize("hasAuthority('GROUP_CTUSERS') || hasAuthority('GROUP_ADMINS')")
	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "ChangeLanguage")
	@ResponsePayload
	public Element setLanguage(@RequestPayload final ChangeLanguage request) {

		log.info("Received change language request by {} to {}", request.getUserName(), request.getNewLanguageID());

		final Element response = new Element("ChangeLanguageResponse", GGXml.NAMESPACE_URI);
		response.addContent(userService.setLanguage(request.getNewLanguageID()).toDiffgram("ChangeLanguageResult", GGXml.NAMESPACE_URI));
		return response;
	}

	@PreAuthorize("hasAuthority('GROUP_CTUSERS') || hasAuthority('GROUP_ADMINS')")
	@PayloadRoot(namespace = GGXml.NAMESPACE_URI, localPart = "TransferOwnership")
	@ResponsePayload
	public Element transferOwnership(@RequestPayload final TransferOwnership request) {

		log.info("Received transfer ownership request by {} to {}", request.getUserName(), request.getNewOwnerCooperatorID());

		final Element diffgram = getElement((org.w3c.dom.Element) request.getDs().getAny());
		final List<Element> targets = DataviewEndpoint.xpDatasetAfter.evaluate(diffgram);

		if (log.isTraceEnabled()) {
			log.trace("diffgram: {}", new XMLOutputter().outputString(diffgram));
			log.trace("NewDataSet: {}", new XMLOutputter().outputString(targets));
		}

		final Element response = new Element("ChangeLanguageResponse", GGXml.NAMESPACE_URI);
		response.addContent(dataviewService.transferOwnership(request.getNewOwnerCooperatorID(), request.isIncludeDescendents(), targets).toDiffgram("TransferOwnershipResult",
			GGXml.NAMESPACE_URI));
		return response;
	}
}