View Javadoc
1   /*
2    * Copyright 2012-2017 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import java.util.Locale;
19  
20  import javax.annotation.Resource;
21  
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.fess.mylasta.direction.FessConfig;
24  import org.codelibs.fess.util.ComponentUtil;
25  
26  public class PermissionHelper {
27      protected String rolePrefix = "{role}";
28  
29      protected String groupPrefix = "{group}";
30  
31      protected String userPrefix = "{user}";
32  
33      @Resource
34      protected SystemHelper systemHelper;
35  
36      public String encode(final String value) {
37          if (StringUtil.isBlank(value)) {
38              return null;
39          }
40  
41          final String permission = value.trim();
42          final String lower = permission.toLowerCase(Locale.ROOT);
43          if (lower.startsWith(userPrefix)) {
44              if (permission.length() > userPrefix.length()) {
45                  return systemHelper.getSearchRoleByUser(permission.substring(userPrefix.length()));
46              } else {
47                  return null;
48              }
49          } else if (lower.startsWith(groupPrefix)) {
50              if (permission.length() > groupPrefix.length()) {
51                  return systemHelper.getSearchRoleByGroup(permission.substring(groupPrefix.length()));
52              } else {
53                  return null;
54              }
55          } else if (lower.startsWith(rolePrefix)) {
56              if (permission.length() > rolePrefix.length()) {
57                  return systemHelper.getSearchRoleByRole(permission.substring(rolePrefix.length()));
58              } else {
59                  return null;
60              }
61          }
62          return permission;
63      }
64  
65      public String decode(final String value) {
66          if (StringUtil.isBlank(value)) {
67              return null;
68          }
69  
70          final FessConfig fessConfig = ComponentUtil.getFessConfig();
71          if (value.startsWith(fessConfig.getRoleSearchUserPrefix()) && value.length() > fessConfig.getRoleSearchUserPrefix().length()) {
72              return userPrefix + value.substring(fessConfig.getRoleSearchUserPrefix().length());
73          } else if (value.startsWith(fessConfig.getRoleSearchGroupPrefix())
74                  && value.length() > fessConfig.getRoleSearchGroupPrefix().length()) {
75              return groupPrefix + value.substring(fessConfig.getRoleSearchGroupPrefix().length());
76          } else if (value.startsWith(fessConfig.getRoleSearchRolePrefix()) && value.length() > fessConfig.getRoleSearchRolePrefix().length()) {
77              return rolePrefix + value.substring(fessConfig.getRoleSearchRolePrefix().length());
78          }
79          return value;
80      }
81  
82      public void setRolePrefix(final String rolePrefix) {
83          this.rolePrefix = rolePrefix;
84      }
85  
86      public void setGroupPrefix(final String groupPrefix) {
87          this.groupPrefix = groupPrefix;
88      }
89  
90      public void setUserPrefix(final String userPrefix) {
91          this.userPrefix = userPrefix;
92      }
93  }