1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.hermesftp.usermanager.model;
25
26 import java.util.Properties;
27
28 import net.sf.hermesftp.exception.FtpConfigException;
29 import net.sf.hermesftp.utils.VarMerger;
30
31 import org.apache.commons.io.FilenameUtils;
32 import org.springframework.util.AntPathMatcher;
33
34 /***
35 * Represents the permission on one or more file pathes. The paths a configured using ant style path
36 * naming conventions.
37 *
38 * @author Lars Behnke
39 *
40 */
41 public class PermissionData {
42
43 private String template;
44
45 private int permission;
46
47 /***
48 * Getter method for the java bean <code>permission</code>.
49 *
50 * @return Returns the value of the java bean <code>permission</code>.
51 */
52 public int getPermission() {
53 return permission;
54 }
55
56 /***
57 * Setter method for the java bean <code>permission</code>.
58 *
59 * @param permission The value of permission to set.
60 */
61 public void setPermission(int permission) {
62 this.permission = permission;
63 }
64
65 /***
66 * Getter method for the java bean <code>template</code>.
67 *
68 * @return Returns the value of the java bean <code>template</code>.
69 */
70 public String getTemplate() {
71 return template;
72 }
73
74 /***
75 * Setter method for the java bean <code>template</code>.
76 *
77 * @param template The value of template to set.
78 */
79 public void setTemplate(String template) {
80 this.template = template;
81 }
82
83 /***
84 * Fills the placeholders in the path template and checks if the passed path matches the
85 * template.
86 *
87 * @param checkPath The path to check.
88 * @param ftproot The ftp root folder.
89 * @param username The username.
90 * @return True, if the path matches the configured pattern.
91 * @throws FtpConfigException Error on reading or processing a configuration file.
92 */
93 public boolean matches(String checkPath, String ftproot, String username)
94 throws FtpConfigException {
95 if (checkPath == null) {
96 return false;
97 }
98 AntPathMatcher pathMatcher = new AntPathMatcher();
99 String antPath = replacePlaceholders(ftproot, username);
100 antPath = FilenameUtils.normalizeNoEndSeparator(antPath);
101 antPath = FilenameUtils.separatorsToUnix(antPath);
102 checkPath = FilenameUtils.normalizeNoEndSeparator(checkPath);
103 checkPath = FilenameUtils.separatorsToUnix(checkPath);
104 return pathMatcher.match(antPath, checkPath);
105
106 }
107
108 private String replacePlaceholders(String ftproot, String username) throws FtpConfigException {
109 VarMerger varMerger = new VarMerger(getTemplate());
110 Properties props = new Properties();
111 props.setProperty("ftproot", FilenameUtils.separatorsToUnix(ftproot));
112 props.setProperty("user", username);
113 varMerger.merge(props);
114 if (!varMerger.isReplacementComplete()) {
115 throw new FtpConfigException("Unresolved placeholders in user configuration file found.");
116 }
117 return varMerger.getText();
118 }
119 }