View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.authorization.xacml.manager.impl.persistence;
23  
24  import static org.opencastproject.authorization.xacml.manager.impl.persistence.ManagedAclEntity.findByIdAndOrgQuery;
25  import static org.opencastproject.db.Queries.namedQuery;
26  
27  import org.opencastproject.authorization.xacml.manager.api.ManagedAcl;
28  import org.opencastproject.authorization.xacml.manager.impl.AclDb;
29  import org.opencastproject.db.DBSession;
30  import org.opencastproject.db.DBSessionFactory;
31  import org.opencastproject.security.api.AccessControlList;
32  import org.opencastproject.security.api.Organization;
33  import org.opencastproject.util.data.functions.Misc;
34  
35  import org.osgi.service.component.annotations.Activate;
36  import org.osgi.service.component.annotations.Component;
37  import org.osgi.service.component.annotations.Deactivate;
38  import org.osgi.service.component.annotations.Reference;
39  
40  import java.util.List;
41  import java.util.Optional;
42  
43  import javax.persistence.EntityManagerFactory;
44  import javax.persistence.RollbackException;
45  
46  /** JPA based impl of an {@link org.opencastproject.authorization.xacml.manager.impl.AclDb}. */
47  @Component(
48          property = {
49                  "service.description=JPA based ACL Provider"
50          },
51          immediate = true,
52          service = { AclDb.class }
53  )
54  public final class JpaAclDb implements AclDb {
55    private DBSessionFactory dbSessionFactory;
56    private EntityManagerFactory emf;
57    private DBSession db;
58  
59    @Activate
60    public void activate() {
61      db = dbSessionFactory.createSession(emf);
62    }
63  
64    @Deactivate
65    public synchronized void deactivate() {
66      db.close();
67    }
68  
69    @Reference
70    public void setDBSessionFactory(DBSessionFactory dbSessionFactory) {
71      this.dbSessionFactory = dbSessionFactory;
72    }
73  
74    @Reference(target = "(osgi.unit.name=org.opencastproject.authorization.xacml.manager)")
75    public void setEntityManagerFactory(EntityManagerFactory emf) {
76      this.emf = emf;
77    }
78  
79    @Override
80    public List<ManagedAcl> getAcls(Organization org) {
81      return Misc.widen(db.execTx(ManagedAclEntity.findByOrgQuery(org.getId())));
82    }
83  
84    @Override
85    public Optional<ManagedAcl> getAcl(Organization org, long id) {
86      return Misc.widen(db.execTx(ManagedAclEntity.findByIdAndOrgQuery(org.getId(), id)));
87    }
88  
89    @Override
90    public boolean updateAcl(final ManagedAcl acl) {
91      return db.execTx(em -> {
92        Optional<ManagedAclEntity> e = findByIdAndOrgQuery(acl.getOrganizationId(), acl.getId()).apply(em);
93        if (e.isEmpty()) {
94          return false;
95        }
96        final ManagedAclEntity updated = e.get().update(acl.getName(), acl.getAcl(), acl.getOrganizationId());
97        em.merge(updated);
98        return true;
99      });
100   }
101 
102   @Override
103   public Optional<ManagedAcl> createAcl(Organization org, AccessControlList acl, String name) {
104     try {
105       final ManagedAcl e = new ManagedAclEntity().update(name, acl, org.getId());
106       return db.execTx(namedQuery.persistOpt(e));
107     } catch (RollbackException e) {
108       // DB exception handler that takes care of unique constraint violation and rethrows any other exception.
109       final Throwable cause = e.getCause();
110       String message = cause.getMessage().toLowerCase();
111       if (message.contains("unique") || message.contains("duplicate")) {
112         return Optional.empty();
113       }
114       throw e;
115     }
116   }
117 
118   @Override
119   public boolean deleteAcl(Organization org, long id) {
120     return db.execTx(ManagedAclEntity.deleteByIdAndOrgQuery(org.getId(), id)) > 0;
121   }
122 }