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 if (parents.isEmpty()) 099 { 100 parents.add(ReferenceTableAccessController.ROOT_CONTEXT); 101 } 102 103 return parents; 104 } 105 106 return null; 107 } 108 109 @Override 110 public List<Object> getRootContexts(Map<String, Object> contextParameters) 111 { 112 List<Object> rootContexts = new ArrayList<>(); 113 if (matchWorkspace(contextParameters)) 114 { 115 rootContexts.add(ReferenceTableAccessController.ROOT_CONTEXT); 116 } 117 return rootContexts; 118 } 119 120 @Override 121 public final List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 122 { 123 if (!_hasAnyReferenceTable()) 124 { 125 return Collections.EMPTY_LIST; 126 } 127 else 128 { 129 return super.getScripts(ignoreRights, contextParameters); 130 } 131 } 132 133 private boolean _hasAnyReferenceTable() 134 { 135 if (_hasAnyReferenceTable != null) 136 { 137 return _hasAnyReferenceTable; 138 } 139 140 for (String extensionId : _contentTypeExtensionPoint.getExtensionsIds()) 141 { 142 ContentType contentType = _contentTypeExtensionPoint.getExtension(extensionId); 143 if (contentType.isReferenceTable()) 144 { 145 _hasAnyReferenceTable = true; 146 return true; 147 } 148 } 149 150 _hasAnyReferenceTable = false; 151 return false; 152 } 153}