View Javadoc
1   /*
2    * Copyright 2012-2021 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.es.user.exentity;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.Base64;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.codelibs.fess.Constants;
28  import org.codelibs.fess.entity.FessUser;
29  import org.codelibs.fess.es.user.bsentity.BsUser;
30  import org.codelibs.fess.mylasta.direction.FessConfig;
31  import org.codelibs.fess.util.ComponentUtil;
32  
33  /**
34   * @author FreeGen
35   */
36  public class User extends BsUser implements FessUser {
37  
38      private static final long serialVersionUID = 1L;
39  
40      private String originalPassword;
41  
42      private Map<String, String> attributes;
43  
44      public Long getVersionNo() {
45          return asDocMeta().version();
46      }
47  
48      public void setVersionNo(final Long version) {
49          asDocMeta().version(version);
50      }
51  
52      public String getId() {
53          return asDocMeta().id();
54      }
55  
56      public void setId(final String id) {
57          asDocMeta().id(id);
58      }
59  
60      @Override
61      public String[] getRoleNames() {
62          return stream(getRoles()).get(stream -> stream.map(this::decode).toArray(n -> new String[n]));
63      }
64  
65      @Override
66      public String[] getGroupNames() {
67          return stream(getGroups()).get(stream -> stream.map(this::decode).toArray(n -> new String[n]));
68      }
69  
70      private String decode(final String value) {
71          return new String(Base64.getDecoder().decode(value), Constants.CHARSET_UTF_8);
72      }
73  
74      @Override
75      public String toString() {
76          return "User [name=" + name + ", roles=" + Arrays.toString(roles) + ", groups=" + Arrays.toString(groups) + "]";
77      }
78  
79      public void setOriginalPassword(final String originalPassword) {
80          this.originalPassword = originalPassword;
81  
82      }
83  
84      public String getOriginalPassword() {
85          return originalPassword;
86      }
87  
88      @Override
89      public String[] getPermissions() {
90          final FessConfig fessConfig = ComponentUtil.getFessConfig();
91          final List<String> list = new ArrayList<>();
92          list.add(fessConfig.getRoleSearchUserPrefix() + getName());
93          stream(getRoles()).of(stream -> stream.forEach(s -> list.add(fessConfig.getRoleSearchRolePrefix() + decode(s))));
94          stream(getGroups()).of(stream -> stream.forEach(s -> list.add(fessConfig.getRoleSearchGroupPrefix() + decode(s))));
95          return list.toArray(new String[list.size()]);
96      }
97  
98      @Override
99      public boolean isEditable() {
100         return true;
101     }
102 
103     public Map<String, String> getAttributes() {
104         return attributes;
105     }
106 
107     public void setAttributes(final Map<String, String> attributes) {
108         this.attributes = attributes;
109     }
110 
111     @Override
112     public Map<String, Object> toSource() {
113         final Map<String, Object> sourceMap = new HashMap<>();
114         if (name != null) {
115             sourceMap.put("name", name);
116         }
117         if (password != null) {
118             sourceMap.put("password", password);
119         }
120         if (groups != null) {
121             sourceMap.put("groups", groups);
122         }
123         if (roles != null) {
124             sourceMap.put("roles", roles);
125         }
126         if (attributes != null) {
127             sourceMap.putAll(attributes);
128         }
129         return sourceMap;
130     }
131 }