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.common.document.DocumentNotFoundException;
28 import org.collectionspace.services.authorization.Permission;
29 import org.collectionspace.services.authorization.PermissionRole;
30 import org.collectionspace.services.authorization.PermissionValue;
31 import org.collectionspace.services.authorization.Role;
32 import org.collectionspace.services.authorization.RoleValue;
33 import org.collectionspace.services.common.ServiceMessages;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentHandler.Action;
36 import org.collectionspace.services.common.document.InvalidDocumentException;
37 import org.collectionspace.services.common.document.ValidatorHandler;
38 import org.collectionspace.services.common.storage.jpa.JpaStorageUtils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * PermissionRoleValidatorHandler executes validation rules for permRole permission permRole
46 public class PermissionRoleValidatorHandler implements ValidatorHandler {
48 final Logger logger = LoggerFactory.getLogger(PermissionRoleValidatorHandler.class);
51 public void validate(Action action, ServiceContext ctx)
52 throws InvalidDocumentException {
53 if (logger.isDebugEnabled()) {
54 logger.debug("validate() action=" + action.name());
57 PermissionRole permRole = (PermissionRole) ctx.getInput();
58 StringBuilder msgBldr = new StringBuilder(ServiceMessages.VALIDATION_FAILURE);
59 boolean invalid = false;
61 if (action.equals(Action.CREATE)) {
63 for (PermissionValue pv : permRole.getPermissions()) {
64 if (isPermissionInvalid(pv.getPermissionId(), msgBldr)) {
68 for (RoleValue rv : permRole.getRoles()) {
69 if (isRoleInvalid(rv.getRoleId(), msgBldr)) {
75 String msg = msgBldr.toString();
77 throw new InvalidDocumentException(msg);
79 } catch (InvalidDocumentException ide) {
81 } catch (Exception e) {
82 throw new InvalidDocumentException(e);
86 private boolean isPermissionInvalid(String id, StringBuilder msgBldr)
87 throws DocumentNotFoundException {
88 boolean invalid = false;
90 if (id == null || id.isEmpty()) {
92 msgBldr.append("\n permissionId : permissionId is missing");
95 Object permissionFound = JpaStorageUtils.getEntity(id, Permission.class);
96 if (permissionFound == null) {
98 msgBldr.append("\n permissionId : permission for permissionId=" + id
105 private boolean isRoleInvalid(String id, StringBuilder msgBldr)
106 throws DocumentNotFoundException {
107 boolean invalid = false;
109 if (id == null || id.isEmpty()) {
111 msgBldr.append("\n roleId : roleId is missing");
114 Object roleFound = JpaStorageUtils.getEntity(id, Role.class);
115 if (roleFound == null) {
117 msgBldr.append("\n roleId : role for roleId=" + id