View Javadoc
1   /*
2    * Copyright (C) 2023 B3Partners B.V.
3    *
4    * SPDX-License-Identifier: MIT
5    */
6   package org.tailormap.api.persistence;
7   
8   import jakarta.persistence.Column;
9   import jakarta.persistence.Entity;
10  import jakarta.persistence.EntityListeners;
11  import jakarta.persistence.Id;
12  import jakarta.persistence.ManyToMany;
13  import jakarta.persistence.PreRemove;
14  import jakarta.persistence.Table;
15  import jakarta.persistence.Version;
16  import jakarta.validation.constraints.Pattern;
17  import java.time.OffsetDateTime;
18  import java.time.ZoneId;
19  import java.util.ArrayList;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  import java.util.function.Function;
24  import org.hibernate.annotations.JdbcTypeCode;
25  import org.hibernate.envers.Audited;
26  import org.hibernate.envers.NotAudited;
27  import org.hibernate.type.SqlTypes;
28  import org.springframework.data.jpa.domain.support.AuditingEntityListener;
29  import org.tailormap.api.persistence.helper.AdminAdditionalPropertyHelper;
30  import org.tailormap.api.persistence.json.AdminAdditionalProperty;
31  import org.tailormap.api.persistence.json.GroupOidcInfo;
32  import org.tailormap.api.persistence.listener.EntityEventPublisher;
33  import org.tailormap.api.util.Constants;
34  
35  @Audited
36  @Entity
37  @Table(name = "groups")
38  @EntityListeners({EntityEventPublisher.class, AuditingEntityListener.class})
39  public class Group extends AuditMetadata {
40    // Group to make authorization rules for anonymous users
41    public static final String ANONYMOUS = "anonymous";
42    // Group to make authorization rules for authenticated users
43    public static final String AUTHENTICATED = "authenticated";
44    public static final String ADMIN = "admin";
45    public static final String REFRESH_CAPABILITIES = "refresh-capabilities";
46    public static final String ACTUATOR = "actuator";
47  
48    @Id
49    @Pattern(regexp = Constants.NAME_REGEX, message = "Group " + Constants.NAME_REGEX_INVALID_MESSAGE) private String name;
50  
51    @Version
52    private Long version;
53  
54    private String label;
55  
56    private boolean systemGroup;
57  
58    private String description;
59  
60    @Column(columnDefinition = "text")
61    private String notes;
62  
63    @ManyToMany(mappedBy = "groups")
64    private Set<User> members = new HashSet<>();
65  
66    /**
67     * Enables the use of a group as an alias for another group. This is useful for example when the 'admin' group name
68     * can't be sent from a single sign-on provider. In that case, the single sign-on provider can send a different
69     * group name and the viewer can map that group name to the 'admin' group.
70     */
71    private String aliasForGroup;
72  
73    /**
74     * Generic additional properties which can be set on a group. A viewer admin frontend extension component can define
75     * attributes for the purposes of the extension and the viewer admin UI will show a control to edit the attribute in
76     * the group detail form.
77     */
78    @JdbcTypeCode(SqlTypes.JSON)
79    @Column(columnDefinition = "jsonb")
80    private List<AdminAdditionalProperty> additionalProperties = new ArrayList<>();
81  
82    @NotAudited
83    @JdbcTypeCode(SqlTypes.JSON)
84    @Column(columnDefinition = "jsonb")
85    private GroupOidcInfo oidcInfo;
86  
87    public String getName() {
88      return name;
89    }
90  
91    public Group setName(String name) {
92      this.name = name;
93      return this;
94    }
95  
96    public Long getVersion() {
97      return version;
98    }
99  
100   public Group setVersion(Long version) {
101     this.version = version;
102     return this;
103   }
104 
105   public String getLabel() {
106     return label;
107   }
108 
109   public Group setLabel(String label) {
110     this.label = label;
111     return this;
112   }
113 
114   public boolean isSystemGroup() {
115     return systemGroup;
116   }
117 
118   public Group setSystemGroup(boolean systemGroup) {
119     this.systemGroup = systemGroup;
120     return this;
121   }
122 
123   public String getDescription() {
124     return description;
125   }
126 
127   public Group setDescription(String title) {
128     this.description = title;
129     return this;
130   }
131 
132   public String getNotes() {
133     return notes;
134   }
135 
136   public Group setNotes(String notes) {
137     this.notes = notes;
138     return this;
139   }
140 
141   public Set<User> getMembers() {
142     return members;
143   }
144 
145   public Group setMembers(Set<User> members) {
146     this.members = members;
147     return this;
148   }
149 
150   public String getAliasForGroup() {
151     return aliasForGroup;
152   }
153 
154   public Group setAliasForGroup(String aliasFor) {
155     this.aliasForGroup = aliasFor;
156     return this;
157   }
158 
159   public List<AdminAdditionalProperty> getAdditionalProperties() {
160     return additionalProperties;
161   }
162 
163   public Group setAdditionalProperties(List<AdminAdditionalProperty> additionalProperties) {
164     this.additionalProperties = additionalProperties;
165     return this;
166   }
167 
168   public GroupOidcInfo getOidcInfo() {
169     return oidcInfo;
170   }
171 
172   public Group setOidcInfo(GroupOidcInfo oidcInfo) {
173     this.oidcInfo = oidcInfo;
174     return this;
175   }
176 
177   @PreRemove
178   @SuppressWarnings("PMD.UnusedPrivateMethod")
179   private void removeMembers() {
180     for (User user : this.members) {
181       user.getGroups().remove(this);
182     }
183   }
184 
185   public void addOrUpdateAdminProperty(String key, Object value, boolean isPublic) {
186     AdminAdditionalPropertyHelper.addOrUpdateAdminProperty(additionalProperties, key, value, isPublic);
187   }
188 
189   /**
190    * Maps a property value in the additional properties of the group. If the property does not exist, it will be
191    * created, and the valueMapper function will be called with a null value.
192    *
193    * @param key the key of the property
194    * @param isPublic whether the property is public
195    * @param valueMapper the function to map the value
196    */
197   public void mapAdminPropertyValue(String key, boolean isPublic, Function<Object, Object> valueMapper) {
198     AdminAdditionalPropertyHelper.mapAdminPropertyValue(additionalProperties, key, isPublic, valueMapper);
199   }
200 
201   public void oidcClientIdSeen(String clientId) {
202     if (oidcInfo == null) {
203       oidcInfo = new GroupOidcInfo();
204     }
205     oidcInfo.addClientIdsItem(clientId);
206     oidcInfo.putLastSeenByClientIdItem(clientId, OffsetDateTime.now(ZoneId.of("UTC")));
207   }
208 }