154 lines
5.8 KiB
Java
154 lines
5.8 KiB
Java
package org.gcube.idm.rest;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import javax.ws.rs.GET;
|
|
import javax.ws.rs.Path;
|
|
import javax.ws.rs.Produces;
|
|
import javax.ws.rs.QueryParam;
|
|
import javax.ws.rs.core.Response;
|
|
import javax.ws.rs.core.Response.Status;
|
|
|
|
import org.gcube.common.authorization.library.policies.Users;
|
|
import org.gcube.common.security.Owner;
|
|
import org.gcube.common.security.providers.SecretManagerProvider;
|
|
import org.gcube.keycloack.KeycloackApiClient;
|
|
import org.gcube.keycloack.KeycloakAPIFactory;
|
|
import org.gcube.rest.ResponseBean;
|
|
import org.gcube.smartgears.ContextProvider;
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
|
import org.gcube.smartgears.security.SimpleCredentials;
|
|
import org.gcube.smartgears.security.defaults.DefaultAuthorizationProvider;
|
|
import org.gcube.smartgears.utils.InnerMethodName;
|
|
import org.jboss.resteasy.spi.NotImplementedYetException;
|
|
import org.keycloak.representations.idm.ClientRepresentation;
|
|
import org.keycloak.representations.idm.UserRepresentation;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeader;
|
|
import com.webcohesion.enunciate.metadata.rs.RequestHeaders;
|
|
import com.webcohesion.enunciate.metadata.rs.ResourceGroup;
|
|
import com.webcohesion.enunciate.metadata.rs.ResourceLabel;
|
|
|
|
@Path("2/users")
|
|
@ResourceGroup("Users APIs")
|
|
@ResourceLabel("Greetings APIs")
|
|
@RequestHeaders({
|
|
@RequestHeader(name = "Authorization", description = "Bearer token, see <a href=\"https://dev.d4science.org/how-to-access-resources\">https://dev.d4science.org/how-to-access-resources</a>")
|
|
})
|
|
public class UsersRest {
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Users.class);
|
|
|
|
@GET
|
|
@Path("/get-usernames-by-role")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public Response getUsernamesByRole(
|
|
@QueryParam("role-name") String roleName) {
|
|
Status status = Status.OK;
|
|
ResponseBean responseBean = new ResponseBean();
|
|
|
|
List<String> usernames = new ArrayList<String>();
|
|
try {
|
|
String ctx = SecretManagerProvider.instance.get().getContext();
|
|
KeycloackApiClient keycloackApiClient = KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx);
|
|
|
|
List<UserRepresentation> users = searchByRole(keycloackApiClient, roleName);
|
|
if (users != null) {
|
|
for (UserRepresentation user : users) {
|
|
usernames.add(user.getUsername());
|
|
}
|
|
}
|
|
responseBean.setResult(usernames);
|
|
responseBean.setSuccess(true);
|
|
} catch (Exception e) {
|
|
logger.error("Unable to retrieve user with the requested role", e);
|
|
responseBean.setMessage(e.getMessage());
|
|
status = Status.INTERNAL_SERVER_ERROR;
|
|
}
|
|
|
|
return Response.status(status).entity(responseBean).build();
|
|
|
|
}
|
|
|
|
private static List<UserRepresentation> searchByRole(KeycloackApiClient keycloackApiClient, String roleName) {
|
|
logger.info("Searching by role: {}", roleName);
|
|
|
|
List<ClientRepresentation> clients = keycloackApiClient.kclient.realm(keycloackApiClient.realmName)
|
|
.clients().findByClientId(keycloackApiClient.clientIdContext);
|
|
|
|
String id = "";
|
|
for (ClientRepresentation client : clients) {
|
|
logger.info("found client =" + client.getClientId());
|
|
logger.info("found client id=" + client.getId());
|
|
id = client.getId();
|
|
}
|
|
|
|
List<UserRepresentation> users = keycloackApiClient.kclient.realm(keycloackApiClient.realmName)
|
|
.clients()
|
|
.get(id).roles().get(roleName)
|
|
.getUserMembers(0, 100000);
|
|
return users;
|
|
}
|
|
|
|
@GET
|
|
@Path("/{get-profile}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public String getCurrentProfile() {
|
|
// SMARTGEARS Specializza il tracciamento della chiamata su Accounting
|
|
InnerMethodName.instance.set("getCurrentProfile");
|
|
Owner owner = SecretManagerProvider.instance.get().getOwner();
|
|
|
|
ApplicationContext appContext = ContextProvider.get();
|
|
SimpleCredentials credentials = ((DefaultAuthorizationProvider) appContext.container().authorizationProvider())
|
|
.getCredentials();
|
|
|
|
String ctx = SecretManagerProvider.instance.get().getContext();
|
|
KeycloackApiClient keycloackApiClient = KeycloakAPIFactory.getSingleton().createtKeycloakInstance(ctx);
|
|
return null;
|
|
}
|
|
|
|
@GET
|
|
@Path("/{get-email}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public String getCurrentEmail() {
|
|
throw new NotImplementedYetException();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{get-fullname}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public String getCurrentFullname() {
|
|
throw new NotImplementedYetException();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{get-all-usernames}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public String getAllUsernames() {
|
|
throw new NotImplementedYetException();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{get-all-fullnames-and-usernames}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public String getAllUsernamesFullnames() {
|
|
throw new NotImplementedYetException();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{user-exists}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public boolean checkUserExists() {
|
|
throw new NotImplementedYetException();
|
|
}
|
|
|
|
@GET
|
|
@Path("/{get-oauth-profile}")
|
|
@Produces({ "application/json;charset=UTF-8", "application/vnd.api+json" })
|
|
public boolean getCurrentOAuthProfile() {
|
|
throw new NotImplementedYetException();
|
|
}
|
|
|
|
}
|