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.endpoint;
23  
24  import static org.opencastproject.util.Jsons.Obj;
25  import static org.opencastproject.util.Jsons.Val;
26  import static org.opencastproject.util.Jsons.arr;
27  import static org.opencastproject.util.Jsons.obj;
28  import static org.opencastproject.util.Jsons.p;
29  
30  import org.opencastproject.authorization.xacml.manager.api.ManagedAcl;
31  import org.opencastproject.security.api.AccessControlEntry;
32  import org.opencastproject.security.api.AccessControlList;
33  
34  import java.util.List;
35  import java.util.function.Function;
36  
37  /** Converter functions from business objects to JSON structures. */
38  public final class JsonConv {
39  
40    public static final String KEY_ID = "id";
41    public static final String KEY_NAME = "name";
42    public static final String KEY_ORGANIZATION_ID = "organizationId";
43    public static final String KEY_ACL = "acl";
44    public static final String KEY_ACE = "ace";
45    public static final String KEY_ROLE = "role";
46    public static final String KEY_ACTION = "action";
47    public static final String KEY_ALLOW = "allow";
48  
49    private JsonConv() {
50    }
51  
52    public static Obj digest(ManagedAcl acl) {
53      return obj(p(KEY_ID, acl.getId()),
54                 p(KEY_NAME, acl.getName()));
55    }
56  
57    public static Obj full(ManagedAcl acl) {
58      return obj(p(KEY_ID, acl.getId()),
59                 p(KEY_NAME, acl.getName()),
60                 p(KEY_ORGANIZATION_ID, acl.getOrganizationId()),
61                 p(KEY_ACL, full(acl.getAcl())));
62    }
63  
64    public static final Function<ManagedAcl, Val> fullManagedAcl = JsonConv::full;
65  
66    public static Obj full(AccessControlList acl) {
67      List<Val> entries = acl.getEntries().stream()
68          .map(fullAccessControlEntry)
69          .toList();
70      return obj(p(KEY_ACE, arr(entries)));
71    }
72  
73    public static Obj full(AccessControlEntry ace) {
74      return obj(p(KEY_ROLE, ace.getRole()),
75                 p(KEY_ACTION, ace.getAction()),
76                 p(KEY_ALLOW, ace.isAllow()));
77    }
78  
79    public static final Function<AccessControlEntry, Val> fullAccessControlEntry = JsonConv::full;
80  }