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.mylasta.action;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import org.codelibs.core.lang.StringUtil;
21  import org.codelibs.fess.Constants;
22  import org.codelibs.fess.entity.FessUser;
23  import org.lastaflute.web.login.TypicalUserBean;
24  
25  /**
26   * @author jflute
27   */
28  public class FessUserBean extends TypicalUserBean<String> { // #change_it also LoginAssist
29  
30      // ===================================================================================
31      //                                                                          Definition
32      //                                                                          ==========
33      /** The serial version UID for object serialization. (Default) */
34      private static final long serialVersionUID = 1L;
35      private final FessUser user;
36  
37      // ===================================================================================
38      //                                                                           Attribute
39      //                                                                           =========
40  
41      // ===================================================================================
42      //                                                                         Constructor
43      //                                                                         ===========
44      public FessUserBean(final FessUser user) {
45          this.user = user;
46      }
47  
48      // ===================================================================================
49      //                                                                      Implementation
50      //                                                                      ==============
51      @Override
52      public String getUserId() {
53          return user.getName();
54      }
55  
56      // ===================================================================================
57      //                                                                            Accessor
58      //                                                                            ========
59      public String[] getPermissions() {
60          return user.getPermissions();
61      }
62  
63      public String[] getRoles() {
64          return user.getRoleNames();
65      }
66  
67      public String[] getGroups() {
68          return user.getGroupNames();
69      }
70  
71      public boolean isEditable() {
72          return user.isEditable();
73      }
74  
75      public boolean hasRole(final String role) {
76          return stream(user.getRoleNames()).get(stream -> stream.anyMatch(s -> s.equals(role)));
77      }
78  
79      public boolean hasRoles(final String[] acceptedRoles) {
80          return stream(user.getRoleNames())
81                  .get(stream -> stream.anyMatch(s1 -> stream(acceptedRoles).get(s3 -> s3.anyMatch(s2 -> s2.equals(s1)))));
82      }
83  
84      public boolean hasGroup(final String group) {
85          return stream(user.getGroupNames()).get(stream -> stream.anyMatch(s -> s.equals(group)));
86      }
87  
88      public boolean hasGroups(final String[] acceptedGroups) {
89          return stream(user.getGroupNames())
90                  .get(stream -> stream.anyMatch(s1 -> stream(acceptedGroups).get(s3 -> s3.anyMatch(s2 -> s2.equals(s1)))));
91      }
92  
93      public FessUser getFessUser() {
94          return user;
95      }
96  
97      public static FessUserBean empty() {
98          return new FessUserBean(null) {
99              private static final long serialVersionUID = 1L;
100 
101             @Override
102             public String getUserId() {
103                 return Constants.EMPTY_USER_ID;
104             }
105 
106             @Override
107             public boolean hasRoles(final String[] acceptedRoles) {
108                 return true;
109             }
110 
111             @Override
112             public String[] getRoles() {
113                 return StringUtil.EMPTY_STRINGS;
114             }
115 
116             @Override
117             public boolean isEditable() {
118                 return false;
119             }
120         };
121     }
122 }