PermissionController.java

/*
 * Copyright 2018 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.genesys.server.api.v1;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.oauth.model.OAuthClient;
import org.genesys.blocks.oauth.service.OAuthClientService;
import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.blocks.security.model.AclSid;
import org.genesys.blocks.security.serialization.SidPermissions;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.blocks.security.service.CustomAclService.AclObjectIdentityExt;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.Partner;
import org.genesys.server.model.UserRole;
import org.genesys.server.model.impl.User;
import org.genesys.server.service.PartnerService;
import org.genesys.server.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;

import io.swagger.annotations.Api;

/**
 * The Class PermissionController.
 *
 * @author Andrey Lugovskoy
 * @author Matija Obreza
 */
@RestController("permissionApi1")
@RequestMapping(value =  PermissionController.CONTROLLER_URL )
@Api(tags = { "permission" })
public class PermissionController {

	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/permission";

	private static final Logger LOG = LoggerFactory.getLogger(PermissionController.class);

	/** The acl service. */
	@Autowired
	protected CustomAclService aclService;

	@Autowired
	private UserService userService;

	@Autowired
	private OAuthClientService clientDetailsService;

	@Autowired
	private PartnerService partnerService;

	/**
	 * Adds the permission.
	 *
	 * @param className the class name
	 * @param id the id
	 * @param sidPermissions the sid permissions
	 * @return the acl object identity
	 */
	@PostMapping(value = "/permissions/{clazz}/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	@JsonView(JsonViews.Minimal.class)
	public AclObjectIdentityExt addPermission(@PathVariable(value = "clazz") final String className, @PathVariable("id") final long id,
			@RequestBody final SidPermissions sidPermissions) {

		final AclObjectIdentity objectIdentity = aclService.ensureObjectIdentity(id, className);
		LOG.info("Setting permissions {}", sidPermissions);

		final AclSid sid = aclService.getSid(sidPermissions.sid.getId());

		return lazyLoadForJson(aclService.setPermissions(objectIdentity, sid, sidPermissions));
	}

	/**
	 * Adds the permission.
	 *
	 * @param className the class name
	 * @param id the id
	 * @param sidPermissions the sid permissions
	 * @return the acl object identity
	 */
	@DeleteMapping(value = "/permissions/{clazz}/{id}/{sid}", produces = MediaType.APPLICATION_JSON_VALUE)
	@JsonView(JsonViews.Minimal.class)
	public AclObjectIdentityExt deletePermissionsForSid(@PathVariable(value = "clazz") final String className, @PathVariable("id") final long id,
			@PathVariable("sid") final String sid) {

		final AclObjectIdentity objectIdentity = aclService.ensureObjectIdentity(id, className);
		LOG.info("Removing permissions for {}", sid);

		final AclSid aclSid = aclService.getSid(aclService.getSidId(sid));

		return lazyLoadForJson(aclService.removePermissions(objectIdentity, aclSid));
	}
	
	/**
	 * Update inheriting status
	 *
	 * @param inheriting is inheriting value
	 * @param id the id
	 * @return updated acl object identity
	 */
	@JsonView(JsonViews.Minimal.class)
	@PostMapping(value = "/update-inheriting/{inheriting}/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public AclObjectIdentityExt updateInheriting(@PathVariable(value = "inheriting") final boolean inheriting,
			@PathVariable(name = "id") final long id) {

		final AclObjectIdentity objectIdentity = aclService.updateInheriting(id, inheriting);
		if (objectIdentity == null) {
			throw new NotFoundElement("No such ACL object");
		}
		return lazyLoadForJson(objectIdentity);
	}

	/**
	 * Update parent object
	 *
	 * @param id the id
	 * @param parentId the parentId
	 * @return updated acl object identity
	 */
	@JsonView(JsonViews.Minimal.class)
	@PostMapping(value = "/update-parent/{id}/{parentId}", produces = MediaType.APPLICATION_JSON_VALUE)
	public AclObjectIdentityExt updateParentObject(@PathVariable(name = "id") final long id, @PathVariable(name = "parentId") final long parentId) {
		final AclObjectIdentity objectIdentity = aclService.updateParentObject(id, parentId);
		if (objectIdentity == null) {
			throw new NotFoundElement("No such ACL object");
		}
		return lazyLoadForJson(objectIdentity);
	}

	/**
	 * Return all information related to the AclAwareModel.
	 *
	 * @param className the class name
	 * @param id the id
	 * @return the acl object identity
	 */
	@GetMapping(value = "/permissions/{clazz}/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	@JsonView(JsonViews.Minimal.class)
	public AclObjectIdentityExt permissions(@PathVariable(value = "clazz") final String className, @PathVariable("id") final long id) {
		final AclObjectIdentity objectIdentity = aclService.getObjectIdentity(id, className);
		if (objectIdentity == null) {
			throw new NotFoundElement("No such ACL object");
		}
		return lazyLoadForJson(objectIdentity);
	}

	/**
	 * Lazy load for json.
	 *
	 * @param objectIdentity the object identity
	 * @return the acl object identity
	 */
	protected AclObjectIdentityExt lazyLoadForJson(final AclObjectIdentity objectIdentity) {
		AclObjectIdentityExt ext = aclService.loadObjectIdentityExt(objectIdentity);
		if (ext == null) {
			throw new NotFoundElement("No such ACL object");
		}
		return ext;
	}

	/**
	 * Return all information for {@link AclObjectIdentity} by its id.
	 *
	 * @param id the internal ID of aclObjectIdentity
	 * @return the acl object identity
	 */
	@GetMapping(value = "/permissions/{aclObjectIdentityId}", produces = MediaType.APPLICATION_JSON_VALUE)
	@JsonView(JsonViews.Minimal.class)
	public AclObjectIdentityExt permissions(@PathVariable(value = "aclObjectIdentityId") final long id) {
		final AclObjectIdentity objectIdentity = aclService.getObjectIdentity(id);
		if (objectIdentity == null) {
			throw new NotFoundElement("No such ACL object");
		}

		return lazyLoadForJson(objectIdentity);
	}

	/**
	 * Auto-complete users, roles and clients.
	 *
	 * @param term the search term
	 * @return Map of SID labels and SID IDs
	 * @since 1.6
	 */
	@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<Long, String> acSid(@RequestParam("term") final String term) {
		final Map<Long, String> sidIds = new HashMap<>();
		sidIds.putAll(acRole(term));
		sidIds.putAll(acUser(term));
		sidIds.putAll(acOauthClient(term));
		sidIds.putAll(acPartnerSid(term));
		return sidIds;
	}

	/**
	 * Ac user.
	 *
	 * @param term the term
	 * @return the map
	 */
	private Map<Long, String> acUser(@RequestParam("term") final String term) {
		final Map<Long, String> userIds = new HashMap<>();
		for (final User user : userService.autocompleteUser(term, 10)) {
			userIds.put(user.getId(), user.getEmail());
		}
		return userIds;
	}

	/**
	 * Ac partner SID.
	 *
	 * @param term the term
	 * @return the map
	 */
	private Map<Long, String> acPartnerSid(@RequestParam("term") final String term) {
		final Map<Long, String> partnerSidIds = new HashMap<>();
		for (final Partner partner : partnerService.autocompletePartners(term, 10)) {
			AclSid partnerSid = aclService.getAuthoritySid(partner.getAuthorityName());
			if (partnerSid != null) {
				partnerSidIds.put(partnerSid.getId(), partner.getShortName());
			}
		}
		return partnerSidIds;
	}

	/**
	 * Ac role.
	 *
	 * @param term the term
	 * @return the map
	 */
	private Map<Long, String> acRole(@RequestParam("term") final String term) {
		final Map<Long, String> roleSids = new HashMap<>();

		final List<UserRole> matchingRoles = Arrays.stream(UserRole.values()).filter(role -> role.name().toLowerCase().startsWith(term.toLowerCase())).collect(Collectors.toList());

		for (final UserRole role : matchingRoles) {
			roleSids.put(aclService.getAuthoritySid(role.getAuthority()).getId(), role.name());
		}

		return roleSids;
	}

	/**
	 * Ac oauth client.
	 *
	 * @param term the term
	 * @return the map
	 */
	private Map<Long, String> acOauthClient(@RequestParam("term") final String term) {
		final Map<Long, String> oauthMap = new HashMap<>();
		for (final OAuthClient client : clientDetailsService.autocompleteClients(term, 10)) {
			oauthMap.put(client.getId(), client.getTitle());
		}
		return oauthMap;
	}
}