MeController.java

/**
 * Copyright 2014 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.v0;

import org.genesys.server.api.ApiBaseController;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.impl.User;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.AbstractOAuth2TokenAuthenticationToken;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;

@RestController("meApi0")
@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { MeController.CONTROLLER_URL, "/json/v0" })
@Api(tags = { "me" })
public class MeController extends ApiBaseController {

	// Rest controller base URL
	protected static final String CONTROLLER_URL = ApiBaseController.APIv0_BASE + "/me";

	@Autowired
	protected UserService userService;

	@Autowired
	protected InstituteService instituteService;

	/**
	 * Returns logged in user's profile
	 *
	 * @return - user's profile
	 */
	@RequestMapping(value = "", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
	public @ResponseBody
	Object getProfile() {
		final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication instanceof AbstractOAuth2TokenAuthenticationToken<?>) {
			var oauthAuth = (AbstractOAuth2TokenAuthenticationToken<?>) authentication;
			final User currentUser = (User) oauthAuth.getPrincipal();
			return userService.loadUserByUsername(currentUser.getUsername());
		}
		// This is added for unit test support
		if (authentication instanceof UsernamePasswordAuthenticationToken) {
			final User currentUser = (User) authentication.getPrincipal();
			return userService.loadUserByUsername(currentUser.getUsername());
		}
		throw new NotFoundElement("Not using user authentication");
	}
	
	@RequestMapping(value = "/institutes", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody
	Object listInstitutes() {
		return instituteService.listMyInstitutes(Sort.by("code"));
	}


}