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/db/User.java | 586 +++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 370 insertions(+), 216 deletions(-)
diff --git a/securis/src/main/java/net/curisit/securis/db/User.java b/securis/src/main/java/net/curisit/securis/db/User.java
index 052082f..13a0f51 100644
--- a/securis/src/main/java/net/curisit/securis/db/User.java
+++ b/securis/src/main/java/net/curisit/securis/db/User.java
@@ -1,3 +1,6 @@
+/*
+* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
+*/
package net.curisit.securis.db;
import java.io.Serializable;
@@ -27,275 +30,426 @@
import com.fasterxml.jackson.annotation.JsonProperty;
/**
- * Entity implementation class for Entity: Users
- *
- */
+* User
+* <p>
+* Application user with bitmask-based roles and membership in organizations
+* and applications. Exposes convenience JSON properties to fetch/set related
+* entity IDs without fetching full entities.
+*
+* Mapping details:
+* - Table: user
+* - ManyToMany organizations via user_organization
+* - ManyToMany applications via user_application
+* - Named queries: list-users, get-user, auth-user, delete-all-users
+*
+* Roles:
+* - Stored as integer bitmask; see {@link Rol}.
+*
+* @author JRA
+* Last reviewed by JRA on Oct 5, 2025.
+*/
@JsonAutoDetect
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Entity
@Table(name = "user")
-@NamedQueries({ @NamedQuery(name = "list-users", query = "SELECT u FROM User u"), @NamedQuery(name = "get-user", query = "SELECT u FROM User u where u.username = :username"),
- @NamedQuery(name = "auth-user", query = "SELECT u FROM User u where u.username = :username and u.password = :password"),
- @NamedQuery(name = "delete-all-users", query = "delete FROM User u") })
+@NamedQueries({
+ @NamedQuery(name = "list-users", query = "SELECT u FROM User u"),
+ @NamedQuery(name = "get-user", query = "SELECT u FROM User u where u.username = :username"),
+ @NamedQuery(name = "auth-user", query = "SELECT u FROM User u where u.username = :username and u.password = :password"),
+ @NamedQuery(name = "delete-all-users", query = "delete FROM User u")
+})
public class User implements Serializable {
- private static final long serialVersionUID = 1L;
+ private static final long serialVersionUID = 1L;
- @Id
- private String username;
+ /** Username (PK). */
+ @Id
+ private String username;
- private String password;
+ /** Password hash/string (not exposed in JSON). */
+ private String password;
- @JsonProperty(value = "first_name")
- @Column(name = "first_name")
- private String firstName;
+ @JsonProperty("first_name")
+ @Column(name = "first_name")
+ private String firstName;
- @JsonProperty(value = "last_name")
- @Column(name = "last_name")
- private String lastName;
+ @JsonProperty("last_name")
+ @Column(name = "last_name")
+ private String lastName;
- private int roles;
+ /** Roles bitmask (see Rol constants). */
+ private int roles;
- @Column(name = "last_login")
- private Date lastLogin;
+ @Column(name = "last_login")
+ private Date lastLogin;
- @Column(name = "modification_timestamp")
- private Date modificationTimestamp;
+ @Column(name = "modification_timestamp")
+ private Date modificationTimestamp;
- @Column(name = "creation_timestamp")
- @JsonProperty("creation_timestamp")
- private Date creationTimestamp;
+ @Column(name = "creation_timestamp")
+ @JsonProperty("creation_timestamp")
+ private Date creationTimestamp;
- private String lang;
+ private String lang;
+ private String email;
- private String email;
+ @JsonIgnore
+ @ManyToMany
+ @JoinTable(name = "user_organization",
+ joinColumns = { @JoinColumn(name = "username", referencedColumnName = "username") },
+ inverseJoinColumns = { @JoinColumn(name = "organization_id", referencedColumnName = "id") })
+ private Set<Organization> organizations;
- @JsonIgnore
- @ManyToMany
- @JoinTable(name = "user_organization", //
- joinColumns = { @JoinColumn(name = "username", referencedColumnName = "username") }, //
- inverseJoinColumns = { @JoinColumn(name = "organization_id", referencedColumnName = "id") } //
- )
- private Set<Organization> organizations;
+ @JsonIgnore
+ @ManyToMany
+ @JoinTable(name = "user_application",
+ joinColumns = { @JoinColumn(name = "username", referencedColumnName = "username") },
+ inverseJoinColumns = { @JoinColumn(name = "application_id", referencedColumnName = "id") })
+ private Set<Application> applications;
- @JsonIgnore
- @ManyToMany
- @JoinTable(name = "user_application", //
- joinColumns = { @JoinColumn(name = "username", referencedColumnName = "username") }, //
- inverseJoinColumns = { @JoinColumn(name = "application_id", referencedColumnName = "id") } //
- )
- private Set<Application> applications;
+ // -------- Getters & setters --------
- public String getUsername() {
- return username;
- }
+ /**
+ * getUsername<p>
+ * Return username (PK).
+ *
+ * @return username
+ */
+ public String getUsername() { return username; }
- public void setUsername(String username) {
- this.username = username;
- }
+ /**
+ * setUsername<p>
+ * Set username (PK).
+ *
+ * @param username
+ */
+ public void setUsername(String username) { this.username = username; }
- @JsonProperty("password")
- public String getDummyPassword() {
- return null;
- }
+ /**
+ * getDummyPassword<p>
+ * Forces password to be omitted in JSON responses.
+ *
+ * @return always null
+ */
+ @JsonProperty("password")
+ public String getDummyPassword() { return null; }
- public String getPassword() {
- return password;
- }
+ /**
+ * getPassword<p>
+ * Return raw/hashed password (internal use).
+ *
+ * @return password
+ */
+ public String getPassword() { return password; }
- public void setPassword(String password) {
- this.password = password;
- }
+ /**
+ * setPassword<p>
+ * Set raw/hashed password (internal use).
+ *
+ * @param password
+ */
+ public void setPassword(String password) { this.password = password; }
- public List<Integer> getRoles() {
- if (roles == 0) {
- return null;
- }
- List<Integer> aux = new ArrayList<>();
- for (int rol : Rol.ALL) {
- if ((roles & rol) != 0) { // Each rol is a number with only 1 bit ==
- // 1 in binary representation
- aux.add(rol);
- }
- }
- return aux;
- }
+ /**
+ * getRoles<p>
+ * Return list of individual role flags contained in the bitmask.
+ *
+ * @return list of role integers or null if no roles
+ */
+ public List<Integer> getRoles() {
+ if (roles == 0) return null;
+ List<Integer> aux = new ArrayList<>();
+ for (int rol : Rol.ALL) {
+ if ((roles & rol) != 0) aux.add(rol);
+ }
+ return aux;
+ }
- public void setRoles(List<Integer> roles) {
- this.roles = 0;
- if (roles != null) {
- for (Integer rol : roles) {
- this.roles |= rol;
- }
- }
- }
+ /**
+ * setRoles<p>
+ * Set the roles bitmask from a list of role flags.
+ *
+ * @param roles list of flags
+ */
+ public void setRoles(List<Integer> roles) {
+ this.roles = 0;
+ if (roles != null) {
+ for (Integer rol : roles) this.roles |= rol;
+ }
+ }
- public String getFirstName() {
- return firstName;
- }
+ /**
+ * getFirstName<p>
+ * Return first name.
+ *
+ * @return firstName
+ */
+ public String getFirstName() { return firstName; }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
+ /**
+ * setFirstName<p>
+ * Set first name.
+ *
+ * @param firstName
+ */
+ public void setFirstName(String firstName) { this.firstName = firstName; }
- public String getLastName() {
- return lastName;
- }
+ /**
+ * getLastName<p>
+ * Return last name.
+ *
+ * @return lastName
+ */
+ public String getLastName() { return lastName; }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
+ /**
+ * setLastName<p>
+ * Set last name.
+ *
+ * @param lastName
+ */
+ public void setLastName(String lastName) { this.lastName = lastName; }
- public Date getLastLogin() {
- return lastLogin;
- }
+ /**
+ * getLastLogin<p>
+ * Return last login timestamp.
+ *
+ * @return lastLogin
+ */
+ public Date getLastLogin() { return lastLogin; }
- public void setLastLogin(Date lastLogin) {
- this.lastLogin = lastLogin;
- }
+ /**
+ * setLastLogin<p>
+ * Set last login timestamp.
+ *
+ * @param lastLogin
+ */
+ public void setLastLogin(Date lastLogin) { this.lastLogin = lastLogin; }
- public Date getModificationTimestamp() {
- return modificationTimestamp;
- }
+ /**
+ * getModificationTimestamp<p>
+ * Return modification timestamp.
+ *
+ * @return modificationTimestamp
+ */
+ public Date getModificationTimestamp() { return modificationTimestamp; }
- public void setModificationTimestamp(Date modificationTimestamp) {
- this.modificationTimestamp = modificationTimestamp;
- }
+ /**
+ * setModificationTimestamp<p>
+ * Set modification timestamp.
+ *
+ * @param modificationTimestamp
+ */
+ public void setModificationTimestamp(Date modificationTimestamp) { this.modificationTimestamp = modificationTimestamp; }
- public Date getCreationTimestamp() {
- return creationTimestamp;
- }
+ /**
+ * getCreationTimestamp<p>
+ * Return creation timestamp.
+ *
+ * @return creationTimestamp
+ */
+ public Date getCreationTimestamp() { return creationTimestamp; }
- public void setCreationTimestamp(Date creationTimestamp) {
- this.creationTimestamp = creationTimestamp;
- }
+ /**
+ * setCreationTimestamp<p>
+ * Set creation timestamp.
+ *
+ * @param creationTimestamp
+ */
+ public void setCreationTimestamp(Date creationTimestamp) { this.creationTimestamp = creationTimestamp; }
- @Override
- public String toString() {
- return "{User: " + username + " Name: " + firstName + " " + lastName + ", last login: " + lastLogin + "}";
- }
+ /**
+ * getLang<p>
+ * Return preferred language.
+ *
+ * @return lang
+ */
+ public String getLang() { return lang; }
- public String getLang() {
- return lang;
- }
+ /**
+ * setLang<p>
+ * Set preferred language.
+ *
+ * @param lang
+ */
+ public void setLang(String lang) { this.lang = lang; }
- public void setLang(String lang) {
- this.lang = lang;
- }
+ /**
+ * getEmail<p>
+ * Return email address.
+ *
+ * @return email
+ */
+ public String getEmail() { return email; }
- public Set<Organization> getOrganizations() {
- return organizations;
- }
+ /**
+ * setEmail<p>
+ * Set email address.
+ *
+ * @param email
+ */
+ public void setEmail(String email) { this.email = email; }
- public void setOrganizations(Set<Organization> organizations) {
- this.organizations = organizations;
- }
+ /**
+ * getOrganizations<p>
+ * Return organizations (entity set).
+ *
+ * @return organizations
+ */
+ public Set<Organization> getOrganizations() { return organizations; }
- public Set<Application> getApplications() {
- return applications;
- }
+ /**
+ * setOrganizations<p>
+ * Set organizations (entity set).
+ *
+ * @param organizations
+ */
+ public void setOrganizations(Set<Organization> organizations) { this.organizations = organizations; }
- public void setApplications(Set<Application> applications) {
- this.applications = applications;
- }
+ /**
+ * getApplications<p>
+ * Return applications (entity set).
+ *
+ * @return applications
+ */
+ public Set<Application> getApplications() { return applications; }
- @JsonProperty("organizations_ids")
- public void setOrgsIds(List<Integer> orgsIds) {
- organizations = new HashSet<>();
- for (Integer orgid : orgsIds) {
- Organization o = new Organization();
- o.setId(orgid);
- organizations.add(o);
- }
- }
+ /**
+ * setApplications<p>
+ * Set applications (entity set).
+ *
+ * @param applications
+ */
+ public void setApplications(Set<Application> applications) { this.applications = applications; }
- @JsonProperty("organizations_ids")
- public Set<Integer> getOrgsIds() {
- if (organizations == null) {
- return null;
- }
- Set<Integer> ids = new HashSet<>();
- for (Organization org : organizations) {
- ids.add(org.getId());
- }
- return ids;
- }
+ // -------- JSON helpers for related IDs --------
- @JsonProperty("applications_ids")
- public void setAppsIds(Collection<Integer> appIds) {
- applications = new HashSet<>();
- for (Integer appid : appIds) {
- Application a = new Application();
- a.setId(appid);
- applications.add(a);
- }
- }
+ /**
+ * setOrgsIds<p>
+ * Replace organizations from a list of org IDs.
+ *
+ * @param orgsIds
+ */
+ @JsonProperty("organizations_ids")
+ public void setOrgsIds(List<Integer> orgsIds) {
+ organizations = new HashSet<>();
+ for (Integer orgid : orgsIds) {
+ Organization o = new Organization();
+ o.setId(orgid);
+ organizations.add(o);
+ }
+ }
- @JsonProperty("applications_ids")
- public Set<Integer> getAppsIds() {
- if (applications == null) {
- return null;
- }
- Set<Integer> ids = new HashSet<>();
- for (Application app : applications) {
- ids.add(app.getId());
- }
- return ids;
- }
+ /**
+ * getOrgsIds<p>
+ * Expose organization IDs.
+ *
+ * @return orgsIds
+ */
+ @JsonProperty("organizations_ids")
+ public Set<Integer> getOrgsIds() {
+ if (organizations == null) return null;
+ Set<Integer> ids = new HashSet<>();
+ for (Organization org : organizations) ids.add(org.getId());
+ return ids;
+ }
- @JsonIgnore
- public Set<Integer> getAllOrgsIds() {
- if (organizations == null) {
- return null;
- }
- Set<Integer> ids = new HashSet<>();
- includeAllOrgs(this.organizations, ids);
- return ids;
- }
+ /**
+ * setAppsIds<p>
+ * Replace applications from a collection of app IDs.
+ *
+ * @param appIds
+ */
+ @JsonProperty("applications_ids")
+ public void setAppsIds(Collection<Integer> appIds) {
+ applications = new HashSet<>();
+ for (Integer appid : appIds) {
+ Application a = new Application();
+ a.setId(appid);
+ applications.add(a);
+ }
+ }
- @JsonIgnore
- public Set<Integer> getAllAppsIds() {
- if (applications == null) {
- return null;
- }
- Set<Integer> ids = this.applications.parallelStream().map(app -> app.getId()).collect(Collectors.toSet());
+ /**
+ * getAppsIds<p>
+ * Expose application IDs.
+ *
+ * @return appsIds
+ */
+ @JsonProperty("applications_ids")
+ public Set<Integer> getAppsIds() {
+ if (applications == null) return null;
+ Set<Integer> ids = new HashSet<>();
+ for (Application app : applications) ids.add(app.getId());
+ return ids;
+ }
- return ids;
- }
+ // -------- Derived scopes --------
- /**
- * Walk into the organization hierarchy to include all descendants
- *
- * @param list
- * @param orgIds
- */
- private void includeAllOrgs(Set<Organization> list, Set<Integer> orgIds) {
- for (Organization org : list) {
- orgIds.add(org.getId());
- includeAllOrgs(org.getChildOrganizations(), orgIds);
- }
- }
+ /**
+ * getAllOrgsIds<p>
+ * Compute full organization scope including descendants.
+ *
+ * @return set of org IDs (may be null when no organizations)
+ */
+ @JsonIgnore
+ public Set<Integer> getAllOrgsIds() {
+ if (organizations == null) return null;
+ Set<Integer> ids = new HashSet<>();
+ includeAllOrgs(this.organizations, ids);
+ return ids;
+ }
- public String getEmail() {
- return email;
- }
+ /**
+ * getAllAppsIds<p>
+ * Compute application scope (direct associations only).
+ *
+ * @return set of application IDs (may be null when no applications)
+ */
+ @JsonIgnore
+ public Set<Integer> getAllAppsIds() {
+ if (applications == null) return null;
+ return this.applications.parallelStream().map(Application::getId).collect(Collectors.toSet());
+ }
- public void setEmail(String email) {
- this.email = email;
- }
+ /**
+ * includeAllOrgs<p>
+ * Walk organization hierarchy to include all descendants.
+ *
+ * @param list current level orgs
+ * @param orgIds accumulator of ids
+ */
+ private void includeAllOrgs(Set<Organization> list, Set<Integer> orgIds) {
+ for (Organization org : list) {
+ orgIds.add(org.getId());
+ includeAllOrgs(org.getChildOrganizations(), orgIds);
+ }
+ }
- /**
- * Numeric rol mask. Be aware to use different bit position for each role
- *
- * @author rob
- */
- public static class Rol {
- public static final int ADVANCE = 0x01;
- public static final int ADMIN = 0x02;
- public static final int BASIC = 0x04;
- public static final int API_CLIENT = 0x80;
- public static final int[] ALL = new int[] { ADVANCE, ADMIN, BASIC, API_CLIENT };
- }
+ /**
+ * toString<p>
+ * Get the string describing the current object
+ *
+ * @return object string
+ */
+ @Override
+ public String toString() {
+ return "{User: " + username + " Name: " + firstName + " " + lastName + ", last login: " + lastLogin + "}";
+ }
+
+ /**
+ * Rol
+ * <p>
+ * Bitmask constants for user roles. Each constant must occupy a distinct bit.
+ */
+ public static class Rol {
+ public static final int ADVANCE = 0x01;
+ public static final int ADMIN = 0x02;
+ public static final int BASIC = 0x04;
+ public static final int API_CLIENT= 0x80;
+ public static final int[] ALL = new int[] { ADVANCE, ADMIN, BASIC, API_CLIENT };
+ }
}
+
--
Gitblit v1.3.2