Skip to content

Commit

Permalink
reformated based on the formatting rules for intellij (see hawkular-b…
Browse files Browse the repository at this point in the history
…uild-tools)
  • Loading branch information
jpkrohling committed Feb 5, 2015
1 parent c48dde5 commit f44b7bc
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 315 deletions.
3 changes: 2 additions & 1 deletion backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
*/
package org.hawkular.accounts.backend.boundary;

import javax.ejb.Stateless;
import javax.inject.Inject;
import org.hawkular.accounts.backend.entity.HawkularUser;
import org.hawkular.accounts.backend.entity.Organization;
import org.hawkular.accounts.backend.entity.Owner;
import org.hawkular.accounts.backend.entity.Resource;
import org.keycloak.KeycloakPrincipal;

import javax.ejb.Stateless;
import javax.inject.Inject;

/**
*
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@Stateless
Expand All @@ -38,7 +38,7 @@ public class PermissionChecker {
* Determines whether the current {@link HawkularUser} has access to the {@link Resource}
*
* @param currentUser the user to be checked
* @param resource the resource to be checked
* @param resource the resource to be checked
* @return true if the user is the owner or if the user belongs to an organization that owns the resource
*/
public boolean hasAccessTo(HawkularUser currentUser, Resource resource) {
Expand All @@ -49,7 +49,7 @@ public boolean hasAccessTo(HawkularUser currentUser, Resource resource) {
* Determines whether the current {@KeycloakPrincipal} has access to the {@link Resource}.
*
* @param principal the {@link KeycloakPrincipal} representing the current user
* @param resource the resource to be checked
* @param resource the resource to be checked
* @return true if the user is the owner or if the user belongs to an organization that owns the resource
* @see PermissionChecker#hasAccessTo(org.keycloak.KeycloakPrincipal, org.hawkular.accounts.backend.entity.Owner)
*/
Expand All @@ -61,7 +61,7 @@ public boolean hasAccessTo(KeycloakPrincipal principal, Resource resource) {
* Determines whether the current {@link HawkularUser} has access to the resource owned by {@link Owner}
*
* @param currentUser the user to be checked
* @param owner the owner of the resource
* @param owner the owner of the resource
* @return true if the user is the owner or if the user belongs to an organization that owns the resource
*/
public boolean hasAccessTo(HawkularUser currentUser, Owner owner) {
Expand All @@ -82,10 +82,10 @@ public boolean hasAccessTo(HawkularUser currentUser, Owner owner) {
* Determines whether the current {@link KeycloakPrincipal} has access to the resources owned by {@link Owner}.
*
* @param principal the {@link KeycloakPrincipal} representing the current user
* @param owner the owner of the resource
* @param owner the owner of the resource
* @return true if the user is the owner or if the user belongs to an organization that owns the resource
* @see PermissionChecker#hasAccessTo(
* org.hawkular.accounts.backend.entity.User, org.hawkular.accounts.backend.entity.Owner)
*org.hawkular.accounts.backend.entity.User, org.hawkular.accounts.backend.entity.Owner)
*/
public boolean hasAccessTo(KeycloakPrincipal principal, Owner owner) {
// Here's a bit of explanation: judging by the name of this class, we wouldn't expect any record to be created.
Expand All @@ -97,13 +97,13 @@ public boolean hasAccessTo(KeycloakPrincipal principal, Owner owner) {

/**
* Recursively checks whether an user is a member or owner of an organization. Examples:
*
* <p/>
* jdoe is owner of acme -> true
* jdoe is member of acme -> true
* jdoe is member of emca which is member of acme -> true
* emca is member of acme -> true
*
* @param member the member to be checked
* @param member the member to be checked
* @param organization the organization that might contain the user
* @return true if the user belongs to the organization recursively
*/
Expand Down Expand Up @@ -146,7 +146,7 @@ public boolean isMemberOf(Owner member, Organization organization) {
* Recursively checks if the specified owner is a direct or indirect owner of the given organization. For instance,
* if jdoe is the owner of acme, and acme owns emca, then jdoe owns emca indirectly.
*
* @param owner the {@link Owner} to check. In our example above, it would be jdoe or acme.
* @param owner the {@link Owner} to check. In our example above, it would be jdoe or acme.
* @param organization the {@link Organization} to verify ownership of. In our example above, it would be emca.
* @return whether or not the specified owner is directly or indirectly the owner of the given organization.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,81 +16,81 @@
*/
package org.hawkular.accounts.backend.boundary;

import java.util.List;
import org.hawkular.accounts.backend.entity.Owner;
import org.hawkular.accounts.backend.entity.Resource;
import org.hawkular.accounts.backend.entity.Resource_;
import org.keycloak.KeycloakPrincipal;

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.backend.entity.Owner;
import org.hawkular.accounts.backend.entity.Resource;
import org.hawkular.accounts.backend.entity.Resource_;
import org.keycloak.KeycloakPrincipal;
import java.util.List;

/**
*
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@Stateless
public class ResourceService {
@Inject
EntityManager em;
@Inject
EntityManager em;

@Inject
UserService userService;
@Inject
UserService userService;

/**
* Retrieves a {@link Resource} based on its ID.
*
* @param id the resource's ID
* @return the existing {@link Resource} or null if the resource doesn't exists.
*/
public Resource getById(String id) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Resource> query = builder.createQuery(Resource.class);
Root<Resource> root = query.from(Resource.class);
query.select(root);
query.where(builder.equal(root.get(Resource_.id), id));
/**
* Retrieves a {@link Resource} based on its ID.
*
* @param id the resource's ID
* @return the existing {@link Resource} or null if the resource doesn't exists.
*/
public Resource getById(String id) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Resource> query = builder.createQuery(Resource.class);
Root<Resource> root = query.from(Resource.class);
query.select(root);
query.where(builder.equal(root.get(Resource_.id), id));

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

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

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

return null;
}
/**
* Retrieves a {@link Resource} based on its ID or creates a new {@link Resource} if it doesn't exists.
*
* @param id the resource's ID
* @param owner if the resource doesn't exists, a new one is created with the specified owner
* @return the existing {@link Resource} or a new one if it doesn't exists yet.
*/
public Resource getOrCreateById(String id, Owner owner) {
Resource resource = getById(id);
if (null == resource) {
resource = new Resource(id, owner);
em.persist(resource);
}

/**
* Retrieves a {@link Resource} based on its ID or creates a new {@link Resource} if it doesn't exists.
*
* @param id the resource's ID
* @param owner if the resource doesn't exists, a new one is created with the specified owner
* @return the existing {@link Resource} or a new one if it doesn't exists yet.
*/
public Resource getOrCreateById(String id, Owner owner) {
Resource resource = getById(id);
if (null == resource) {
resource = new Resource(id, owner);
em.persist(resource);
return resource;
}

return resource;
}

/**
* Retrieves a {@link Resource} based on its ID or creates a new {@link Resource} if it doesn't exists.
*
* @param id the resource's ID
* @param principal if the resource doesn't exists, a new one is created with the specified principal
* @return the existing {@link Resource} or a new one if it doesn't exists yet.
* @see ResourceService#getOrCreateById(java.lang.String, org.hawkular.accounts.backend.entity.Owner)
*/
public Resource getOrCreateById(String id, KeycloakPrincipal principal) {
return getOrCreateById(id, userService.getByPrincipal(principal));
}
/**
* Retrieves a {@link Resource} based on its ID or creates a new {@link Resource} if it doesn't exists.
*
* @param id the resource's ID
* @param principal if the resource doesn't exists, a new one is created with the specified principal
* @return the existing {@link Resource} or a new one if it doesn't exists yet.
* @see ResourceService#getOrCreateById(java.lang.String, org.hawkular.accounts.backend.entity.Owner)
*/
public Resource getOrCreateById(String id, KeycloakPrincipal principal) {
return getOrCreateById(id, userService.getByPrincipal(principal));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,75 @@
*/
package org.hawkular.accounts.backend.boundary;

import java.util.List;
import org.hawkular.accounts.backend.entity.HawkularUser;
import org.hawkular.accounts.backend.entity.HawkularUser_;
import org.keycloak.KeycloakPrincipal;

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.backend.entity.HawkularUser;
import org.hawkular.accounts.backend.entity.HawkularUser_;
import org.keycloak.KeycloakPrincipal;
import java.util.List;

/**
*
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@Stateless
public class UserService {

@Inject
EntityManager em;
@Inject
EntityManager em;

/**
* Retrieves an {@link HawkularUser} based in its ID.
* @param id the user ID
* @return the existing user with the given ID or null if the user is not found.
*/
public HawkularUser getById(String id) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<HawkularUser> query = builder.createQuery(HawkularUser.class);
Root<HawkularUser> root = query.from(HawkularUser.class);
query.select(root);
query.where(builder.equal(root.get(HawkularUser_.id), id));
/**
* Retrieves an {@link HawkularUser} based in its ID.
*
* @param id the user ID
* @return the existing user with the given ID or null if the user is not found.
*/
public HawkularUser getById(String id) {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<HawkularUser> query = builder.createQuery(HawkularUser.class);
Root<HawkularUser> root = query.from(HawkularUser.class);
query.select(root);
query.where(builder.equal(root.get(HawkularUser_.id), id));

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

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

return null;
}

return null;
}
/**
* Retrieves an {@link HawkularUser} based on the {@link KeycloakPrincipal}.
*
* @param principal the {@link KeycloakPrincipal}
* @return an {@link HawkularUser} instance representing the user for the {@link KeycloakPrincipal}.It's never null.
*/
public HawkularUser getByPrincipal(KeycloakPrincipal principal) {
return getOrCreateById(principal.getName());
}

/**
* Retrieves an {@link HawkularUser} based on the {@link KeycloakPrincipal}.
* @param principal the {@link KeycloakPrincipal}
* @return an {@link HawkularUser} instance representing the user for the {@link KeycloakPrincipal}. It's never null.
*/
public HawkularUser getByPrincipal(KeycloakPrincipal principal) {
return getOrCreateById(principal.getName());
}
/**
* Retrieves an {@link HawkularUser} based on its ID. If no user is found, a new one is created and returned.
*
* @param id the user ID
* @return an {@link HawkularUser} instance representing the user with the given ID. It's never null.
*/
public HawkularUser getOrCreateById(String id) {
HawkularUser user = getById(id);
if (null == user) {
user = new HawkularUser(id);
em.persist(user);
}

/**
* Retrieves an {@link HawkularUser} based on its ID. If no user is found, a new one is created and returned.
* @param id the user ID
* @return an {@link HawkularUser} instance representing the user with the given ID. It's never null.
*/
public HawkularUser getOrCreateById(String id) {
HawkularUser user = getById(id);
if (null == user) {
user = new HawkularUser(id);
em.persist(user);
return user;
}

return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import javax.persistence.PersistenceContext;

/**
*
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@ApplicationScoped
public class ApplicationResources {
@Produces
@PersistenceContext
private static javax.persistence.EntityManager em;
@Produces
@PersistenceContext
private static javax.persistence.EntityManager em;

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
import org.jboss.logging.annotations.ValidIdRange;

/**
*
* @author Juraci Paixão Kröhling <juraci at kroehling.de>
*/
@MessageLogger(projectCode = "HAWKACC")
@ValidIdRange(min = 100000, max = 109999)
public interface HawkularAccountsMessageLogger {
HawkularAccountsMessageLogger LOGGER = Logger.getMessageLogger
(HawkularAccountsMessageLogger.class, HawkularAccountsMessageLogger.class.getPackage().getName());
HawkularAccountsMessageLogger LOGGER = Logger.getMessageLogger
(HawkularAccountsMessageLogger.class, HawkularAccountsMessageLogger.class.getPackage().getName());

}

0 comments on commit f44b7bc

Please sign in to comment.