Skip to content

Commit

Permalink
HAWKULAR-59 - Listing users in organization
Browse files Browse the repository at this point in the history
  • Loading branch information
jpkrohling committed Sep 30, 2015
1 parent 03d94b6 commit b74366d
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,22 @@ public Response deleteOrganization(@NotNull @PathParam("id") String id) {

return Response.status(Response.Status.FORBIDDEN).build();
}

/**
* Retrieves a specific organization based on its ID.
*
* @return a {@link javax.ws.rs.core.Response} whose entity is an {@link Organization}
*/
@GET
@Path("/{id}")
public Response getOrganization(@PathParam("id") String id) {
Organization organization = organizationService.get(id);

if (organization == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}

return Response.ok().entity(organization).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.hawkular.accounts.backend.boundary;

import java.util.List;

import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

import org.hawkular.accounts.api.OrganizationMembershipService;
import org.hawkular.accounts.api.OrganizationService;
import org.hawkular.accounts.api.model.Organization;
import org.hawkular.accounts.api.model.OrganizationMembership;

/**
* REST service responsible for managing {@link org.hawkular.accounts.api.model.OrganizationMembership}.
*
* @author Juraci Paixão Kröhling
*/
@Path("/organizationMemberships/{organizationId}")
@PermitAll
@Stateless
public class OrganizationMembershipEndpoint {

@Inject
OrganizationMembershipService membershipService;

@Inject
OrganizationService organizationService;

@GET
public Response getOrganizationMembershipsForOrganization(@PathParam("organizationId") String organizationId) {
Organization organization = organizationService.get(organizationId);
List<OrganizationMembership> memberships = membershipService.getMembershipsForOrganization(organization);
return Response.ok().entity(memberships).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,13 @@ public interface OrganizationService {
* @param organization the organization to be removed
*/
void deleteOrganization(Organization organization);

/**
* Retrieves an {@link Organization} based on its ID.
*
* @param id the organization's ID
* @return the existing {@link Organization} or null if the resource doesn't exists.
* @throws IllegalArgumentException if the given ID is null
*/
Organization get(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.hawkular.accounts.api.internal.impl;

import javax.annotation.security.PermitAll;
import javax.ejb.Asynchronous;
import javax.ejb.Singleton;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.hawkular.accounts.api.UserService;
import org.hawkular.accounts.api.internal.adapter.HawkularAccounts;
import org.hawkular.accounts.api.model.HawkularUser;
import org.hawkular.accounts.api.model.NameChangedEvent;

/**
* @author Juraci Paixão Kröhling
*/
@Singleton
@PermitAll
public class NameChangeObserver {

@Inject
@HawkularAccounts
EntityManager em;

@Inject
UserService userService;

@Asynchronous
public void persistNameChange(@Observes NameChangedEvent event) {
HawkularUser user = userService.getById(event.getUser().getId());
user.setName(event.getUser().getName());
em.persist(user);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.hawkular.accounts.api.NamedRole;
import org.hawkular.accounts.api.OrganizationMembershipService;
import org.hawkular.accounts.api.OrganizationService;
import org.hawkular.accounts.api.internal.adapter.HawkularAccounts;
import org.hawkular.accounts.api.model.Organization;
import org.hawkular.accounts.api.model.OrganizationMembership;
import org.hawkular.accounts.api.model.Organization_;
import org.hawkular.accounts.api.model.Persona;
import org.hawkular.accounts.api.model.Role;

Expand Down Expand Up @@ -79,4 +83,28 @@ public void deleteOrganization(Organization organization) {
membershipService.getMembershipsForOrganization(organization).stream().forEach(em::remove);
em.remove(organization);
}

@Override
public Organization get(String id) {
if (null == id) {
throw new IllegalArgumentException("The given resource ID is invalid (null).");
}

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Organization> query = builder.createQuery(Organization.class);
Root<Organization> root = query.from(Organization.class);
query.select(root);
query.where(builder.equal(root.get(Organization_.id), id));

List<Organization> results = em.createQuery(query).getResultList();
if (results.size() == 1) {
return results.get(0);
}

if (results.size() > 1) {
throw new IllegalStateException("More than one organization found for ID " + id);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.security.PermitAll;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
Expand All @@ -34,6 +35,7 @@
import org.hawkular.accounts.api.internal.adapter.HawkularAccounts;
import org.hawkular.accounts.api.model.HawkularUser;
import org.hawkular.accounts.api.model.HawkularUser_;
import org.hawkular.accounts.api.model.NameChangedEvent;
import org.keycloak.KeycloakPrincipal;

/**
Expand All @@ -52,14 +54,24 @@ public class UserServiceImpl implements UserService {
@Resource
SessionContext sessionContext;

@Inject
Event<NameChangedEvent> event;

@Produces @CurrentUser
public HawkularUser getCurrent() {
KeycloakPrincipal principal = (KeycloakPrincipal) sessionContext.getCallerPrincipal();
String id = principal.getName();
String name = principal.getKeycloakSecurityContext().getToken().getName();

HawkularUser user = getOrCreateById(id);
user.setName(name);

if (user.getName() == null || !user.getName().equals(name)) {
user.setName(name);
if (event != null) {
// if we are in an environment where events are supported, let's propagate this event
event.fire(new NameChangedEvent(user));
}
}

return user;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* 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.hawkular.accounts.api.model;

/**
* CDI event for propagating the change of an user's full name.
*
* @author Juraci Paixão Kröhling
*/
public class NameChangedEvent {

private HawkularUser user;

public NameChangedEvent(HawkularUser user) {
this.user = user;
}

public HawkularUser getUser() {
return user;
}
}

0 comments on commit b74366d

Please sign in to comment.