From 146a0fb8b0e90f9196e569152f649baf60d6cc8f Mon Sep 17 00:00:00 2001
From: Joaquín Reñé <jrene@curisit.net>
Date: Tue, 07 Oct 2025 14:52:57 +0000
Subject: [PATCH] #4410 - Comments on classes

---
 securis/src/main/java/net/curisit/securis/services/OrganizationResource.java |  186 ++++++++++++++++++++++++++++++----------------
 1 files changed, 121 insertions(+), 65 deletions(-)

diff --git a/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java b/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
index ee23619..30d5940 100644
--- a/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
+++ b/securis/src/main/java/net/curisit/securis/services/OrganizationResource.java
@@ -1,3 +1,6 @@
+/*
+ * Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
+ */
 package net.curisit.securis.services;
 
 import java.util.Date;
@@ -39,10 +42,12 @@
 import net.curisit.securis.utils.TokenHelper;
 
 /**
- * Organization resource, this service will provide methods to create, modify
- * and delete organizations
- * 
- * @author roberto <roberto.sanchez@curisit.net>
+ * OrganizationResource
+ * <p>
+ * CRUD and listing of organizations. Non-admin users are scoped by their
+ * accessible organization ids when listing.
+ *
+ * Last reviewed by JRA on Oct 5, 2025.
  */
 @Path("/organization")
 @RequestScoped
@@ -50,18 +55,18 @@
 
 	private static final Logger LOG = LogManager.getLogger(OrganizationResource.class);
 
-	@Context
-	EntityManager em;
+	@Context EntityManager em;
+	@Context BasicSecurityContext bsc;
 
-	@Context
-	BasicSecurityContext bsc;
-
-	public OrganizationResource() {
-	}
+	public OrganizationResource() { }
 
 	/**
-	 * 
-	 * @return the server version in format majorVersion.minorVersion
+	 * index
+	 * <p>
+	 * List organizations. For admins returns all; for non-admins filters
+	 * by the ids in {@link BasicSecurityContext#getOrganizationsIds()}.
+	 *
+	 * @return 200 OK with the list.
 	 */
 	@GET
 	@Path("/")
@@ -69,8 +74,6 @@
 	@Securable
 	public Response index() {
 		LOG.info("Getting organizations list ");
-
-		// EntityManager em = emProvider.get();
 		em.clear();
 		TypedQuery<Organization> q;
 		if (bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) {
@@ -84,15 +87,18 @@
 				q.setParameter("list_ids", bsc.getOrganizationsIds());
 			}
 		}
-
 		List<Organization> list = q.getResultList();
-
 		return Response.ok(list).build();
 	}
 
 	/**
-	 * 
-	 * @return the server version in format majorVersion.minorVersion
+	 * get
+	 * <p>
+	 * Fetch an organization by id.
+	 *
+	 * @param orgid organization id (string form).
+	 * @param token header token (unused).
+	 * @return 200 OK with entity or 404 if not found.
 	 */
 	@GET
 	@Path("/{orgid}")
@@ -104,8 +110,6 @@
 			LOG.error("Organization ID is mandatory");
 			return Response.status(Status.NOT_FOUND).build();
 		}
-
-		// EntityManager em = emProvider.get();
 		em.clear();
 		Organization org = em.find(Organization.class, Integer.parseInt(orgid));
 		if (org == null) {
@@ -115,16 +119,15 @@
 		return Response.ok(org).build();
 	}
 
-	private boolean isCyclicalRelationship(int currentId, Organization parent) {
-		while (parent != null) {
-			if (parent.getId() == currentId) {
-				return true;
-			}
-			parent = parent.getParentOrganization();
-		}
-		return false;
-	}
-
+	/**
+	 * create
+	 * <p>
+	 * Create a new organization, setting optional parent and user members.
+	 * Requires ADMIN.
+	 *
+	 * @param org payload with code/name/etc., optional parentOrgId and usersIds.
+	 * @return 200 OK with created organization or 404 when parent/user not found.
+	 */
 	@POST
 	@Path("/")
 	@Consumes(MediaType.APPLICATION_JSON)
@@ -134,7 +137,6 @@
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
 	public Response create(Organization org) {
 		LOG.info("Creating new organization");
-		// EntityManager em = emProvider.get();
 
 		try {
 			this.setParentOrg(org, org.getParentOrgId(), em);
@@ -162,36 +164,17 @@
 		return Response.ok(org).build();
 	}
 
-	private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException {
-		Organization parentOrg = null;
-		if (parentOrgId != null) {
-			parentOrg = em.find(Organization.class, parentOrgId);
-			if (parentOrg == null) {
-				LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId());
-				throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId());
-			}
-		}
-
-		org.setParentOrganization(parentOrg);
-	}
-
-	private void setOrgUsers(Organization org, Set<String> usersIds, EntityManager em) throws SeCurisException {
-		Set<User> users = null;
-		if (usersIds != null && !usersIds.isEmpty()) {
-			users = new HashSet<>();
-			for (String username : usersIds) {
-				User user = em.find(User.class, username);
-				if (user == null) {
-					LOG.error("Organization user with id '{}' not found in DB", username);
-					throw new SecurityException("Organization's user not found with ID: " + username);
-				}
-				users.add(user);
-			}
-		}
-
-		org.setUsers(users);
-	}
-
+	/**
+	 * modify
+	 * <p>
+	 * Update an organization. Validates no cyclic parent relationship,
+	 * updates parent and user set. Requires ADMIN.
+	 *
+	 * @param org   new values (including optional parent/usersIds).
+	 * @param orgid target id.
+	 * @param token (unused) header token.
+	 * @return 200 OK with updated organization, or specific error status.
+	 */
 	@PUT
 	@POST
 	@Path("/{orgid}")
@@ -202,7 +185,6 @@
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
 	public Response modify(Organization org, @PathParam("orgid") String orgid, @HeaderParam(TokenHelper.TOKEN_HEADER_PÀRAM) String token) {
 		LOG.info("Modifying organization with id: {}", orgid);
-		// EntityManager em = emProvider.get();
 		Organization currentOrg = em.find(Organization.class, Integer.parseInt(orgid));
 		if (currentOrg == null) {
 			LOG.error("Organization with id {} not found in DB", orgid);
@@ -233,15 +215,23 @@
 		return Response.ok(currentOrg).build();
 	}
 
+	/**
+	 * delete
+	 * <p>
+	 * Delete an organization if it has no children. Requires ADMIN.
+	 *
+	 * @param orgid target id.
+	 * @param req   request (unused).
+	 * @return 200 OK with success map, or 404/403 on constraints.
+	 */
 	@DELETE
 	@Path("/{orgid}")
 	@EnsureTransaction
 	@Produces({ MediaType.APPLICATION_JSON })
 	@Securable(roles = Rol.ADMIN)
 	@RolesAllowed(BasicSecurityContext.ROL_ADMIN)
-	public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest request) {
+	public Response delete(@PathParam("orgid") String orgid, @Context HttpServletRequest req) {
 		LOG.info("Deleting organization with id: {}", orgid);
-		// EntityManager em = emProvider.get();
 		Organization org = em.find(Organization.class, Integer.parseInt(orgid));
 		if (org == null) {
 			LOG.error("Organization with id {} can not be deleted, It was not found in DB", orgid);
@@ -256,4 +246,70 @@
 		return Response.ok(Utils.createMap("success", true, "id", orgid)).build();
 	}
 
+	// ---------------------------------------------------------------------
+	// Helpers
+	// ---------------------------------------------------------------------
+
+	/** 
+	 * isCyclicalRelationship<p>
+	 * Detects cycles by walking up the parent chain. 
+	 * 
+	 * @param currentId
+	 * @param parent
+	 * @return isCyclicalRelationship
+	 */
+	private boolean isCyclicalRelationship(int currentId, Organization parent) {
+		while (parent != null) {
+			if (parent.getId() == currentId) return true;
+			parent = parent.getParentOrganization();
+		}
+		return false;
+	}
+
+	/** 
+	 * setParentOrg<p>
+	 * Resolve and set parent organization (nullable). 
+	 * 
+	 * @param org
+	 * @param parentOrgId
+	 * @param entitymanager
+	 * @throws SeCurisException
+	 */
+	private void setParentOrg(Organization org, Integer parentOrgId, EntityManager em) throws SeCurisException {
+		Organization parentOrg = null;
+		if (parentOrgId != null) {
+			parentOrg = em.find(Organization.class, parentOrgId);
+			if (parentOrg == null) {
+				LOG.error("Organization parent with id {} not found in DB", org.getParentOrgId());
+				throw new SecurityException("Organization's parent not found with ID: " + org.getParentOrgId());
+			}
+		}
+		org.setParentOrganization(parentOrg);
+	}
+
+	/** 
+	 * setOrgUsers<p>
+	 * Replace organization users from the provided usernames set. 
+	 * 
+	 * @param org
+	 * @param usersIds
+	 * @param entityManager
+	 * @throws SeCurisException
+	 */
+	private void setOrgUsers(Organization org, Set<String> usersIds, EntityManager em) throws SeCurisException {
+		Set<User> users = null;
+		if (usersIds != null && !usersIds.isEmpty()) {
+			users = new HashSet<>();
+			for (String username : usersIds) {
+				User user = em.find(User.class, username);
+				if (user == null) {
+					LOG.error("Organization user with id '{}' not found in DB", username);
+					throw new SecurityException("Organization's user not found with ID: " + username);
+				}
+				users.add(user);
+			}
+		}
+		org.setUsers(users);
+	}
 }
+

--
Gitblit v1.3.2