001/* 002 * Copyright 2017 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.rights; 017 018import java.util.ArrayList; 019import java.util.Collections; 020import java.util.HashSet; 021import java.util.List; 022import java.util.Map; 023import java.util.Set; 024 025import org.apache.avalon.framework.service.ServiceException; 026import org.apache.avalon.framework.service.ServiceManager; 027import org.apache.commons.lang3.StringUtils; 028 029import org.ametys.cms.contenttype.ContentType; 030import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 031import org.ametys.core.right.AbstractStaticRightAssignmentContext; 032import org.ametys.core.right.RightAssignmentContext; 033 034/** 035 * {@link RightAssignmentContext} for assign rights to a reference table {@link ContentType} or all reference tables root 036 */ 037public class ReferenceTableRightAssignmentContext extends AbstractStaticRightAssignmentContext 038{ 039 /** The id if this right context */ 040 public static final String ID = "right.assignment.context.referencetable"; 041 042 /** The content type extension point */ 043 protected ContentTypeExtensionPoint _contentTypeExtensionPoint; 044 /** Once computed, lets remember it */ 045 protected Boolean _hasAnyReferenceTable; 046 047 @Override 048 public void service(ServiceManager smanager) throws ServiceException 049 { 050 super.service(smanager); 051 _contentTypeExtensionPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 052 } 053 054 @Override 055 public Object convertJSContext(Object context) 056 { 057 if (context instanceof String) 058 { 059 return context; 060 } 061 return null; 062 } 063 064 @Override 065 public String getContextIdentifier(Object context) 066 { 067 if (context instanceof String) 068 { 069 return (String) context; 070 } 071 return null; 072 } 073 074 @Override 075 public Set<Object> getParentContexts(Object context) 076 { 077 if (ReferenceTableAccessController.ROOT_CONTEXT.equals(context)) 078 { 079 return null; 080 } 081 else if (context instanceof String) 082 { 083 Set<Object> parents = new HashSet<>(); 084 085 String ctypeId = StringUtils.substringAfter((String) context, Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/"); 086 ContentType contentType = _contentTypeExtensionPoint.getExtension(ctypeId); 087 088 String[] supertypeIds = contentType.getSupertypeIds(); 089 for (int i = 0; i < supertypeIds.length; i++) 090 { 091 String supertypeId = supertypeIds[i]; 092 if (_contentTypeExtensionPoint.getExtension(supertypeId).isReferenceTable()) 093 { 094 parents.add(Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/" + supertypeId); 095 } 096 } 097 098 return parents; 099 } 100 101 return null; 102 } 103 104 @Override 105 public List<Object> getRootContexts(Map<String, Object> contextParameters) 106 { 107 List<Object> rootContexts = new ArrayList<>(); 108 if (matchWorkspace(contextParameters)) 109 { 110 rootContexts.add(ReferenceTableAccessController.ROOT_CONTEXT); 111 } 112 return rootContexts; 113 } 114 115 @Override 116 public final List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 117 { 118 if (!_hasAnyReferenceTable()) 119 { 120 return Collections.EMPTY_LIST; 121 } 122 else 123 { 124 return super.getScripts(ignoreRights, contextParameters); 125 } 126 } 127 128 private boolean _hasAnyReferenceTable() 129 { 130 if (_hasAnyReferenceTable != null) 131 { 132 return _hasAnyReferenceTable; 133 } 134 135 for (String extensionId : _contentTypeExtensionPoint.getExtensionsIds()) 136 { 137 ContentType contentType = _contentTypeExtensionPoint.getExtension(extensionId); 138 if (contentType.isReferenceTable()) 139 { 140 _hasAnyReferenceTable = true; 141 return true; 142 } 143 } 144 145 _hasAnyReferenceTable = false; 146 return false; 147 } 148}