Skip to content

Commit

Permalink
added basic organization management
Browse files Browse the repository at this point in the history
  • Loading branch information
jpkrohling committed Feb 9, 2015
1 parent 5822730 commit 22afd61
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,26 @@
*/
package org.hawkular.accounts.backend.boundary;

import org.hawkular.accounts.backend.entity.HawkularUser;
import org.hawkular.accounts.backend.entity.Organization;
import org.hawkular.accounts.backend.entity.Organization_;
import org.hawkular.accounts.backend.entity.rest.OrganizationRequest;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.ejb.SessionContext;
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 javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.util.List;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

/**
* @author jpkroehling
Expand All @@ -31,10 +44,56 @@
@PermitAll
@Stateless
public class OrganizationService {
@Inject
EntityManager em;

@Resource
SessionContext sessionContext;

@Inject
UserService userService;

@Inject
PermissionChecker permissionChecker;

@GET
@Path("/")
public List<Organization> getOrganizations() {
return null;
public Response getOrganizations() {
HawkularUser user = userService.getOrCreateById(sessionContext.getCallerPrincipal().getName());

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_.owner), user));

return Response.ok().entity(em.createQuery(query).getResultList()).build();
}

@POST
@Path("/")
public Response createOrganization(OrganizationRequest request) {
HawkularUser user = userService.getOrCreateById(sessionContext.getCallerPrincipal().getName());
Organization organization = new Organization(user);

organization.setName(request.getName());
organization.setDescription(request.getDescription());

em.persist(organization); // TODO: before creating, checking if it exists already
return Response.ok().entity(organization).build();
}

@DELETE
@Path("/{id}")
public Response deleteOrganization(@PathParam("id") String id) {
Organization organization = em.find(Organization.class, id);
HawkularUser user = userService.getById(sessionContext.getCallerPrincipal().getName());

if (permissionChecker.isOwnerOf(user, organization)) {
em.remove(organization);
return Response.ok().build();
}

return Response.status(Response.Status.FORBIDDEN).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
import org.hawkular.accounts.backend.entity.Resource;
import org.keycloak.KeycloakPrincipal;

import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.inject.Inject;

/**
* // TODO: this should probably be refactored into an "api" module
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@Stateless
@PermitAll
public class PermissionChecker {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hawkular.accounts.backend.entity.HawkularUser_;
import org.keycloak.KeycloakPrincipal;

import javax.annotation.security.PermitAll;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
Expand All @@ -32,6 +33,7 @@
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@Stateless
@PermitAll
public class UserService {

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

/**
* Represents an non-user entity that can own resources. It has itself an owner.
Expand All @@ -34,8 +35,11 @@ public class Organization extends Owner {
@ManyToOne
private Owner owner;

private String name;
private String description;

@ManyToMany
private final List<Owner> members = new ArrayList<>();
private final transient List<Owner> members = new ArrayList<>();

protected Organization() { // jpa happy
super();
Expand All @@ -46,6 +50,11 @@ public Organization(String id, Owner owner) {
this.owner = owner;
}

public Organization(Owner owner) {
super(UUID.randomUUID().toString());
this.owner = owner;
}

public Owner getOwner() {
return owner;
}
Expand All @@ -54,6 +63,22 @@ public List<Owner> getMembers() {
return Collections.unmodifiableList(members);
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public void addMember(Owner member) {
if (members.contains(member)) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.entity.rest;

/**
* @author jpkroehling
*/
public class OrganizationRequest {
private String name;
private String description;

public OrganizationRequest() {
}

public OrganizationRequest(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
3 changes: 3 additions & 0 deletions backend/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="primary">
<jta-data-source>java:jboss/datasources/HawkularDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>

0 comments on commit 22afd61

Please sign in to comment.