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.app.web.base.login;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.HashSet;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.codelibs.fess.entity.FessUser;
25  import org.codelibs.fess.helper.SystemHelper;
26  import org.codelibs.fess.mylasta.direction.FessConfig;
27  import org.codelibs.fess.util.ComponentUtil;
28  import org.lastaflute.web.login.credential.LoginCredential;
29  
30  public class OpenIdConnectCredential implements LoginCredential {
31  
32      private final Map<String, Object> attributes;
33  
34      public OpenIdConnectCredential(final Map<String, Object> attributes) {
35          this.attributes = attributes;
36      }
37  
38      @Override
39      public String toString() {
40          return "{" + getEmail() + "}";
41      }
42  
43      public String getEmail() {
44          return (String) attributes.get("email");
45      }
46  
47      public User getUser() {
48          final FessConfig fessConfig = ComponentUtil.getFessConfig();
49          return new User(getEmail(), fessConfig.getOicDefaultGroupsAsArray(), fessConfig.getOicDefaultRolesAsArray());
50      }
51  
52      public static class User implements FessUser {
53  
54          private static final long serialVersionUID = 1L;
55  
56          protected final String name;
57  
58          protected String[] groups;
59  
60          protected String[] roles;
61  
62          protected String[] permissions;
63  
64          protected User(final String name, final String[] groups, final String[] roles) {
65              this.name = name;
66              this.groups = groups;
67              this.roles = roles;
68          }
69  
70          @Override
71          public String getName() {
72              return name;
73          }
74  
75          @Override
76          public String[] getRoleNames() {
77              return roles;
78          }
79  
80          @Override
81          public String[] getGroupNames() {
82              return groups;
83          }
84  
85          @Override
86          public String[] getPermissions() {
87              if (permissions == null) {
88                  final SystemHelper systemHelper = ComponentUtil.getSystemHelper();
89                  final Set<String> permissionSet = new HashSet<>();
90                  permissionSet.add(systemHelper.getSearchRoleByUser(name));
91                  stream(groups).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByGroup(s))));
92                  stream(roles).of(stream -> stream.forEach(s -> permissionSet.add(systemHelper.getSearchRoleByRole(s))));
93                  permissions = permissionSet.toArray(new String[permissionSet.size()]);
94              }
95              return permissions;
96          }
97  
98          @Override
99          public boolean isEditable() {
100             return false;
101         }
102 
103     }
104 }