View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.mcr.acl.accesskey.model;
20  
21  import java.util.Date;
22  
23  import org.mycore.backend.jpa.MCRObjectIDConverter;
24  import org.mycore.datamodel.metadata.MCRObjectID;
25  
26  import com.fasterxml.jackson.annotation.JsonIgnore;
27  import com.fasterxml.jackson.annotation.JsonInclude;
28  
29  import jakarta.persistence.Column;
30  import jakarta.persistence.Convert;
31  import jakarta.persistence.Entity;
32  import jakarta.persistence.GeneratedValue;
33  import jakarta.persistence.GenerationType;
34  import jakarta.persistence.Id;
35  import jakarta.persistence.NamedQueries;
36  import jakarta.persistence.NamedQuery;
37  import jakarta.persistence.Table;
38  
39  @NamedQueries({
40      @NamedQuery(name = "MCRAccessKey.getWithObjectId",
41          query = "SELECT k"
42              + "  FROM MCRAccessKey k"
43              + "  WHERE k.objectId = :objectId"
44              + "  ORDER BY k.lastModified ASC"),
45      @NamedQuery(name = "MCRAccessKey.getWithSecret",
46          query = "SELECT k"
47              + "  FROM MCRAccessKey k"
48              + "  WHERE k.secret = :secret AND k.objectId = :objectId"),
49      @NamedQuery(name = "MCRAccessKey.getWithType",
50          query = "SELECT k"
51              + "  FROM MCRAccessKey k"
52              + "  WHERE k.type = :type AND k.objectId = :objectId"),
53      @NamedQuery(name = "MCRAccessKey.clearWithObjectId",
54          query = "DELETE"
55              + "  FROM MCRAccessKey k"
56              + "  WHERE k.objectId = :objectId"),
57      @NamedQuery(name = "MCRAccessKey.clear",
58          query = "DELETE"
59              + "  FROM MCRAccessKey k"),
60  })
61  
62  /**
63   * Access keys for a {@link MCRObject}.
64   * An access keys contains a secret and a type.
65   * Value is the key secret of the key and type the permission.
66   */
67  @Entity
68  @Table(name = "MCRAccessKey")
69  @JsonInclude(JsonInclude.Include.NON_NULL)
70  public class MCRAccessKey {
71  
72      private static final long serialVersionUID = 1L;
73  
74      /** The unique and internal information id */
75      private int id;
76  
77      /** The access key information */
78      private MCRObjectID objectId;
79  
80      /** The secret */
81      private String secret;
82  
83      /** The permission type */
84      private String type;
85  
86      /** The status */
87      private Boolean isActive;
88  
89      /** The expiration date* */
90      private Date expiration;
91  
92      /** The comment */
93      private String comment;
94  
95      /** The date of creation */
96      private Date created;
97  
98      /** The name of creator */
99      private String createdBy;
100 
101     /** The date of last modification */
102     private Date lastModified;
103 
104     /** The name of the last modifier */
105     private String lastModifiedBy;
106 
107     protected MCRAccessKey() {
108     }
109 
110     /**
111      * Creates a new access key with secret and type.
112      *
113      * @param secret the secret the user must know to acquire permission.
114      * @param type the type of permission.
115      */
116     public MCRAccessKey(final String secret, final String type) {
117         this();
118         setSecret(secret);
119         setType(type);
120     }
121 
122     /**
123      * @return the linked objectId
124      */
125     @JsonIgnore
126     @Column(name = "object_id",
127         length = MCRObjectID.MAX_LENGTH,
128         nullable = false)
129     @Convert(converter = MCRObjectIDConverter.class)
130     public MCRObjectID getObjectId() {
131         return objectId;
132     }
133 
134     /**
135      * @param objectId the {@link MCRObjectID} to set
136      */
137     public void setObjectId(final MCRObjectID objectId) {
138         this.objectId = objectId;
139     }
140 
141     /**
142      * @return internal id
143      */
144     @JsonIgnore
145     @Id
146     @GeneratedValue(strategy = GenerationType.IDENTITY)
147     @Column(name = "accesskey_id",
148         nullable = false)
149     public int getId() {
150         return id;
151     }
152 
153     /**
154      * @param id internal id
155      */
156     public void setId(int id) {
157         this.id = id;
158     }
159 
160     /**
161      * @return the key secret
162      */
163     @Column(name = "secret",
164         nullable = false)
165     public String getSecret() {
166         return secret;
167     }
168 
169     /**
170      * @param secret key secret
171      */
172     public void setSecret(final String secret) {
173         this.secret = secret;
174     }
175 
176     /**
177      * @return permission type 
178      */
179     @Column(name = "type",
180         nullable = false)
181     public String getType() {
182         return type;
183     }
184 
185     /**
186      * @param type permission type
187      */
188     public void setType(String type) {
189         this.type = type;
190     }
191 
192     /**
193      * @return active or not
194      */
195     @Column(name = "isActive",
196         nullable = false)
197     public Boolean getIsActive() {
198         return isActive;
199     }
200 
201     /**
202      * @param isActive the state
203      */
204     public void setIsActive(final Boolean isActive) {
205         this.isActive = isActive;
206     }
207 
208     /**
209      * @return expiration date
210      */
211     @Column(name = "expiration")
212     public Date getExpiration() {
213         return expiration;
214     }
215 
216     /**
217      * @param expiration the expiration date
218      */
219     public void setExpiration(final Date expiration) {
220 
221         this.expiration = expiration;
222     }
223 
224     /**
225      * @return comment
226      */
227     @Column(name = "comment")
228     public String getComment() {
229         return comment;
230     }
231 
232     /**
233      * @param comment the comment
234      */
235     public void setComment(String comment) {
236         this.comment = comment;
237     }
238 
239     /**
240      * @return date of creation
241      */
242     @Column(name = "created")
243     public Date getCreated() {
244         return created;
245     }
246 
247     /**
248      * @param created date of creation
249      */
250     public void setCreated(final Date created) {
251         this.created = created;
252     }
253 
254     /**
255      * @return name of creator
256      */
257     @Column(name = "createdBy")
258     public String getCreatedBy() {
259         return createdBy;
260     }
261 
262     /**
263      * @param createdBy name of creator
264      */
265     public void setCreatedBy(final String createdBy) {
266         this.createdBy = createdBy;
267     }
268 
269     /**
270      * @return date of last modification
271      */
272     @Column(name = "lastModified")
273     public Date getLastModified() {
274         return lastModified;
275     }
276 
277     /**
278      * @param lastModified date of last modification
279      */
280     @Column(name = "lastModified")
281     public void setLastModified(final Date lastModified) {
282         this.lastModified = lastModified;
283     }
284 
285     /**
286      * @return name of last modifier
287      */
288     @Column(name = "lastModifiedBy")
289     public String getLastModifiedBy() {
290         return lastModifiedBy;
291     }
292 
293     /**
294      * @param lastModifiedBy name of last modifier
295      */
296     public void setLastModifiedBy(final String lastModifiedBy) {
297         this.lastModifiedBy = lastModifiedBy;
298     }
299 
300     @Override
301     public boolean equals(Object o) {
302         if (o == this) {
303             return true;
304         }
305         if (!(o instanceof MCRAccessKey)) {
306             return false;
307         }
308         MCRAccessKey other = (MCRAccessKey) o;
309         return this.id == other.getId() && this.type.equals(other.getType())
310             && this.secret.equals(other.getSecret());
311     }
312 }