View Javadoc
1   /*
2    * Copyright 2012-2025 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.opensearch.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.mylasta.direction.FessConfig;
30  import org.codelibs.fess.opensearch.user.bsentity.BsUser;
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      public String getOriginalPassword() {
84          return originalPassword;
85      }
86  
87      /**
88       * Clears the original password from memory.
89       * Should be called after the password has been used for authentication or provisioning.
90       */
91      public void clearOriginalPassword() {
92          this.originalPassword = null;
93      }
94  
95      @Override
96      public String[] getPermissions() {
97          final FessConfig fessConfig = ComponentUtil.getFessConfig();
98          final List<String> list = new ArrayList<>();
99          list.add(fessConfig.getRoleSearchUserPrefix() + getName());
100         stream(getRoles()).of(stream -> stream.forEach(s -> list.add(fessConfig.getRoleSearchRolePrefix() + decode(s))));
101         stream(getGroups()).of(stream -> stream.forEach(s -> list.add(fessConfig.getRoleSearchGroupPrefix() + decode(s))));
102         return list.toArray(new String[list.size()]);
103     }
104 
105     @Override
106     public boolean isEditable() {
107         return true;
108     }
109 
110     public Map<String, String> getAttributes() {
111         return attributes;
112     }
113 
114     public void setAttributes(final Map<String, String> attributes) {
115         this.attributes = attributes;
116     }
117 
118     @Override
119     public Map<String, Object> toSource() {
120         final Map<String, Object> sourceMap = new HashMap<>();
121         if (name != null) {
122             sourceMap.put("name", name);
123         }
124         if (password != null) {
125             sourceMap.put("password", password);
126         }
127         if (groups != null) {
128             sourceMap.put("groups", groups);
129         }
130         if (roles != null) {
131             sourceMap.put("roles", roles);
132         }
133         if (attributes != null) {
134             sourceMap.putAll(attributes);
135         }
136         return sourceMap;
137     }
138 }