Joaquín Reñé
2025-10-07 146a0fb8b0e90f9196e569152f649baf60d6cc8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright @ 2013 CurisTEC, S.A.S. All Rights Reserved.
*/
package net.curisit.securis.db;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* 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")
})
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    /** Username (PK). */
    @Id
    private String username;
    /** Password hash/string (not exposed in JSON). */
    private String password;
    @JsonProperty("first_name")
    @Column(name = "first_name")
    private String firstName;
    @JsonProperty("last_name")
    @Column(name = "last_name")
    private String lastName;
    /** Roles bitmask (see Rol constants). */
    private int roles;
    @Column(name = "last_login")
    private Date lastLogin;
    @Column(name = "modification_timestamp")
    private Date modificationTimestamp;
    @Column(name = "creation_timestamp")
    @JsonProperty("creation_timestamp")
    private Date creationTimestamp;
    private String lang;
    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_application",
        joinColumns = { @JoinColumn(name = "username", referencedColumnName = "username") },
        inverseJoinColumns = { @JoinColumn(name = "application_id", referencedColumnName = "id") })
    private Set<Application> applications;
    // -------- Getters & setters --------
    /** 
     * getUsername<p>
     * Return username (PK). 
     * 
     * @return username
     */
    public String getUsername() { return username; }
    /** 
     * setUsername<p>
     * Set username (PK). 
     * 
     * @param username
     */
    public void setUsername(String username) { this.username = username; }
    /**
    * getDummyPassword<p>
    * Forces password to be omitted in JSON responses.
    *
    * @return always null
    */
    @JsonProperty("password")
    public String getDummyPassword() { return null; }
    /** 
     * getPassword<p>
     * Return raw/hashed password (internal use). 
     * 
     * @return password
     */
    public String getPassword() { return password; }
    /** 
     * setPassword<p>
     * Set raw/hashed password (internal use).
     * 
     * @param password
     */
    public void setPassword(String password) { this.password = password; }
    /**
    * 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;
    }
    /**
    * 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;
        }
    }
    /** 
     * getFirstName<p>
     * Return first name. 
     * 
     * @return firstName
     */
    public String getFirstName() { return firstName; }
    /** 
     * setFirstName<p>
     * Set first name. 
     * 
     * @param firstName
     */
    public void setFirstName(String firstName) { this.firstName = firstName; }
    /** 
     * getLastName<p>
     * Return last name. 
     * 
     * @return lastName
     */
    public String getLastName() { return lastName; }
    /** 
     * setLastName<p>
     * Set last name. 
     * 
     * @param lastName
     */
    public void setLastName(String lastName) { this.lastName = lastName; }
    /** 
     * getLastLogin<p>
     * Return last login timestamp. 
     * 
     * @return lastLogin
     */
    public Date getLastLogin() { return lastLogin; }
    /** 
     * setLastLogin<p>
     * Set last login timestamp. 
     * 
     * @param lastLogin
     */
    public void setLastLogin(Date lastLogin) { this.lastLogin = lastLogin; }
    /** 
     * getModificationTimestamp<p>
     * Return modification timestamp. 
     * 
     * @return modificationTimestamp
     */
    public Date getModificationTimestamp() { return modificationTimestamp; }
    /** 
     * setModificationTimestamp<p>
     * Set modification timestamp. 
     * 
     * @param modificationTimestamp
     */
    public void setModificationTimestamp(Date modificationTimestamp) { this.modificationTimestamp = modificationTimestamp; }
    /** 
     * getCreationTimestamp<p>
     * Return creation timestamp. 
     * 
     * @return creationTimestamp
     */
    public Date getCreationTimestamp() { return creationTimestamp; }
    /** 
     * setCreationTimestamp<p>
     * Set creation timestamp. 
     * 
     * @param creationTimestamp
     */
    public void setCreationTimestamp(Date creationTimestamp) { this.creationTimestamp = creationTimestamp; }
    /** 
     * getLang<p>
     * Return preferred language. 
     * 
     * @return lang
     */
    public String getLang() { return lang; }
    /** 
     * setLang<p>
     * Set preferred language. 
     * 
     * @param lang
     */
    public void setLang(String lang) { this.lang = lang; }
    /** 
     * getEmail<p>
     * Return email address. 
     * 
     * @return email
     */
    public String getEmail() { return email; }
    /** 
     * setEmail<p>
     * Set email address. 
     * 
     * @param email
     */
    public void setEmail(String email) { this.email = email; }
    /** 
     * getOrganizations<p>
     * Return organizations (entity set). 
     * 
     * @return organizations
     */
    public Set<Organization> getOrganizations() { return organizations; }
    /** 
     * setOrganizations<p>
     * Set organizations (entity set). 
     * 
     * @param organizations
     */
    public void setOrganizations(Set<Organization> organizations) { this.organizations = organizations; }
    /** 
     * getApplications<p>
     * Return applications (entity set). 
     * 
     * @return applications
     */
    public Set<Application> getApplications() { return applications; }
    /** 
     * setApplications<p>
     * Set applications (entity set). 
     * 
     * @param applications
     */
    public void setApplications(Set<Application> applications) { this.applications = applications; }
    // -------- JSON helpers for related IDs --------
    /** 
     * 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);
        }
    }
    /** 
     * 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;
    }
    /** 
     * 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);
        }
    }
    /** 
     * 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;
    }
    // -------- Derived scopes --------
    /**
    * 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;
    }
    /**
    * 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());
    }
    /**
    * 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);
        }
    }
    /**
     * 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 };
    }
}