2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
25 package org.collectionspace.services.authorization.storage;
27 import org.collectionspace.services.authorization.Permission;
28 import org.collectionspace.services.authorization.PermissionRole;
29 import org.collectionspace.services.authorization.PermissionValue;
30 import org.collectionspace.services.authorization.Role;
31 import org.collectionspace.services.authorization.RoleValue;
32 import org.collectionspace.services.common.ServiceMessages;
33 import org.collectionspace.services.common.context.ServiceContext;
34 import org.collectionspace.services.common.document.DocumentHandler.Action;
35 import org.collectionspace.services.common.document.InvalidDocumentException;
36 import org.collectionspace.services.common.document.ValidatorHandler;
37 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * PermissionRoleValidatorHandler executes validation rules for permRole permission permRole
45 public class PermissionRoleValidatorHandler implements ValidatorHandler {
47 final Logger logger = LoggerFactory.getLogger(PermissionRoleValidatorHandler.class);
50 public void validate(Action action, ServiceContext ctx)
51 throws InvalidDocumentException {
52 if (logger.isDebugEnabled()) {
53 logger.debug("validate() action=" + action.name());
56 PermissionRole permRole = (PermissionRole) ctx.getInput();
57 StringBuilder msgBldr = new StringBuilder(ServiceMessages.VALIDATION_FAILURE);
58 boolean invalid = false;
60 if (action.equals(Action.CREATE)) {
62 for (PermissionValue pv : permRole.getPermissions()) {
63 if (isPermissionInvalid(pv.getPermissionId(), msgBldr)) {
67 for (RoleValue rv : permRole.getRoles()) {
68 if (isRoleInvalid(rv.getRoleId(), msgBldr)) {
74 String msg = msgBldr.toString();
76 throw new InvalidDocumentException(msg);
78 } catch (InvalidDocumentException ide) {
80 } catch (Exception e) {
81 throw new InvalidDocumentException(e);
85 private boolean isPermissionInvalid(String id, StringBuilder msgBldr) {
86 boolean invalid = false;
88 if (id == null || id.isEmpty()) {
90 msgBldr.append("\n permissionId : permissionId is missing");
93 Object permissionFound = JpaStorageUtils.getEntity(id, Permission.class);
94 if (permissionFound == null) {
96 msgBldr.append("\n permissionId : permission for permissionId=" + id
103 private boolean isRoleInvalid(String id, StringBuilder msgBldr) {
104 boolean invalid = false;
106 if (id == null || id.isEmpty()) {
108 msgBldr.append("\n roleId : roleId is missing");
111 Object roleFound = JpaStorageUtils.getEntity(id, Role.class);
112 if (roleFound == null) {
114 msgBldr.append("\n roleId : role for roleId=" + id