|
| 1 | +package ch.cyberduck.core.ctera; |
| 2 | + |
| 3 | +import ch.cyberduck.core.Acl; |
| 4 | +import ch.cyberduck.core.LocaleFactory; |
| 5 | +import ch.cyberduck.core.Path; |
| 6 | +import ch.cyberduck.core.PathAttributes; |
| 7 | +import ch.cyberduck.core.dav.DAVAttributesFinderFeature; |
| 8 | +import ch.cyberduck.core.dav.DAVPathEncoder; |
| 9 | +import ch.cyberduck.core.dav.DAVSession; |
| 10 | +import ch.cyberduck.core.exception.AccessDeniedException; |
| 11 | +import ch.cyberduck.core.exception.BackgroundException; |
| 12 | + |
| 13 | +import org.apache.logging.log4j.LogManager; |
| 14 | +import org.apache.logging.log4j.Logger; |
| 15 | + |
| 16 | +import javax.xml.namespace.QName; |
| 17 | +import java.io.IOException; |
| 18 | +import java.text.MessageFormat; |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +import com.github.sardine.DavResource; |
| 29 | + |
| 30 | +public class CteraAttributesFinderFeature extends DAVAttributesFinderFeature { |
| 31 | + private static final Logger log = LogManager.getLogger(CteraAttributesFinderFeature.class); |
| 32 | + |
| 33 | + public static final String CTERA_GUID = "guid"; |
| 34 | + public static final String CTERA_NAMESPACE_URI = "http://www.ctera.com/ns"; |
| 35 | + public static final String CTERA_NAMESPACE_PREFIX = "ctera"; |
| 36 | + /** |
| 37 | + * Read Data: Allows or denies viewing data in files. |
| 38 | + */ |
| 39 | + public static final Acl.Role READPERMISSION = new Acl.Role("readpermission"); |
| 40 | + /** |
| 41 | + * Write Data: Allows or denies making changes to a file and overwriting existing content. |
| 42 | + */ |
| 43 | + public static final Acl.Role WRITEPERMISSION = new Acl.Role("writepermission"); |
| 44 | + /** |
| 45 | + * Execute File: Allows or denies running program (executable) files. |
| 46 | + * Files only. |
| 47 | + */ |
| 48 | + public static final Acl.Role EXECUTEPERMISSION = new Acl.Role("executepermission"); |
| 49 | + /** |
| 50 | + * Allows or denies deleting the file or folder. If you don't have Delete permission on a file or folder, |
| 51 | + * you can still delete it if you have been granted Delete Subfolders and Files on the parent folder. |
| 52 | + */ |
| 53 | + public static final Acl.Role DELETEPERMISSION = new Acl.Role("deletepermission"); |
| 54 | + /** |
| 55 | + * Create Files: Allows or denies creating files within the folder. |
| 56 | + * Directories only. |
| 57 | + * For future use, not used yet. |
| 58 | + */ |
| 59 | + @Deprecated |
| 60 | + public static final Acl.Role CREATEFILEPERMISSION = new Acl.Role(WRITEPERMISSION.getName()); |
| 61 | + /** |
| 62 | + * Create Folders: Allows or denies creating subfolders within the folder. |
| 63 | + * Directories only. |
| 64 | + */ |
| 65 | + public static final Acl.Role CREATEDIRECTORIESPERMISSION = new Acl.Role("createdirectoriespermission"); |
| 66 | + |
| 67 | + public static final Set<Acl.Role> ALL_ACL_ROLES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList( |
| 68 | + READPERMISSION, WRITEPERMISSION, EXECUTEPERMISSION, DELETEPERMISSION, CREATEDIRECTORIESPERMISSION |
| 69 | + ))); |
| 70 | + public static final Set<QName> ALL_ACL_QN = Collections.unmodifiableSet(ALL_ACL_ROLES.stream().map(CteraAttributesFinderFeature::toQn).collect(Collectors.toSet())); |
| 71 | + public static final QName GUID_QN = new QName(CteraAttributesFinderFeature.CTERA_NAMESPACE_URI, CteraAttributesFinderFeature.CTERA_GUID, CteraAttributesFinderFeature.CTERA_NAMESPACE_PREFIX); |
| 72 | + |
| 73 | + private final DAVSession session; |
| 74 | + |
| 75 | + public CteraAttributesFinderFeature(final DAVSession session) { |
| 76 | + super(session); |
| 77 | + this.session = session; |
| 78 | + } |
| 79 | + |
| 80 | + public static QName toQn(final Acl.Role role) { |
| 81 | + return new QName(CTERA_NAMESPACE_URI, role.getName(), CTERA_NAMESPACE_PREFIX); |
| 82 | + } |
| 83 | + |
| 84 | + public static String toProp(final QName qn) { |
| 85 | + return qn.getLocalPart(); |
| 86 | + } |
| 87 | + |
| 88 | + public static Acl.Role toRole(final QName qn) { |
| 89 | + return new Acl.Role(toProp(qn)); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param customProps Custom properties from DAV resource |
| 94 | + * @return Known ACLs in custom namespace |
| 95 | + */ |
| 96 | + public static Acl toAcl(final Map<String, String> customProps) { |
| 97 | + if(customProps.keySet().stream().anyMatch(property -> ALL_ACL_QN.stream().anyMatch(qn -> toProp(qn).equals(property)))) { |
| 98 | + return new Acl(new Acl.CanonicalUser(), customProps.entrySet().stream().filter(property -> ALL_ACL_QN.stream().anyMatch(qn -> toProp(qn).equals(property.getKey()))) |
| 99 | + .filter(property -> Boolean.parseBoolean(property.getValue())).map(property -> new Acl.Role(property.getKey())).distinct().toArray(Acl.Role[]::new)); |
| 100 | + } |
| 101 | + // Ignore ACL in preflight checks as none of the custom roles is found |
| 102 | + return Acl.EMPTY; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @param file File with ACLs to validate role |
| 107 | + * @param role Assumed role |
| 108 | + * @throws AccessDeniedException ACLs do not contain role |
| 109 | + */ |
| 110 | + protected static void assumeRole(final Path file, final Acl.Role role) throws BackgroundException { |
| 111 | + assumeRole(file, file.getName(), role); |
| 112 | + } |
| 113 | + |
| 114 | + protected static void assumeRole(final Path file, final String filename, final Acl.Role role) throws BackgroundException { |
| 115 | + final Acl acl = file.attributes().getAcl(); |
| 116 | + if(acl == Acl.EMPTY) { |
| 117 | + if(log.isWarnEnabled()) { |
| 118 | + log.warn(String.format("Missing ACL for %s", file)); |
| 119 | + } |
| 120 | + return; |
| 121 | + } |
| 122 | + if(!acl.get(new Acl.CanonicalUser()).contains(role)) { |
| 123 | + if(log.isWarnEnabled()) { |
| 124 | + log.warn(String.format("ACL %s for %s does not include %s", acl, file, role)); |
| 125 | + } |
| 126 | + if(role == READPERMISSION) { |
| 127 | + throw new AccessDeniedException(MessageFormat.format(LocaleFactory.localizedString("Download {0} failed", "Error"), |
| 128 | + filename)).withFile(file); |
| 129 | + } |
| 130 | + if(role == WRITEPERMISSION) { |
| 131 | + throw new AccessDeniedException(MessageFormat.format(LocaleFactory.localizedString("Upload {0} failed", "Error"), |
| 132 | + filename)).withFile(file); |
| 133 | + } |
| 134 | + if(role == DELETEPERMISSION) { |
| 135 | + throw new AccessDeniedException(MessageFormat.format( |
| 136 | + LocaleFactory.localizedString("Cannot delete {0}", "Error"), file.getName())).withFile(file); |
| 137 | + } |
| 138 | + if(role == CREATEDIRECTORIESPERMISSION) { |
| 139 | + throw new AccessDeniedException(MessageFormat.format(LocaleFactory.localizedString("Cannot create folder {0}", "Error"), |
| 140 | + filename)).withFile(file); |
| 141 | + } |
| 142 | + throw new AccessDeniedException(MessageFormat.format( |
| 143 | + LocaleFactory.localizedString("Cannot create {0}", "Error"), file.getName())).withFile(file); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + protected List<DavResource> list(final Path file) throws IOException { |
| 149 | + return session.getClient().list(new DAVPathEncoder().encode(file), 0, Collections.unmodifiableSet(Stream.concat( |
| 150 | + Stream.of(GUID_QN), ALL_ACL_QN.stream() |
| 151 | + ).collect(Collectors.toSet()))); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public PathAttributes toAttributes(final DavResource resource) { |
| 156 | + final Map<String, String> customProps = resource.getCustomProps(); |
| 157 | + final Acl acl = toAcl(customProps); |
| 158 | + final PathAttributes attributes = super.toAttributes(resource); |
| 159 | + if(customProps.containsKey(CTERA_GUID)) { |
| 160 | + attributes.setFileId(customProps.get(CTERA_GUID)); |
| 161 | + } |
| 162 | + return attributes.withAcl(acl); |
| 163 | + } |
| 164 | +} |
0 commit comments