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.auth;
17
18 import static org.codelibs.core.stream.StreamUtil.stream;
19
20 import org.apache.commons.lang3.ArrayUtils;
21 import org.codelibs.core.stream.StreamUtil.StreamOf;
22 import org.codelibs.fess.auth.chain.AuthenticationChain;
23 import org.codelibs.fess.opensearch.user.exentity.User;
24
25 /**
26 * Manages authentication operations across multiple authentication chains.
27 * This class coordinates user operations across different authentication providers.
28 */
29 public class AuthenticationManager {
30 /** Array of authentication chains to process user operations. */
31 protected AuthenticationChain[] chains = {};
32
33 /**
34 * Default constructor for AuthenticationManager.
35 */
36 public AuthenticationManager() {
37 // Default constructor
38 }
39
40 /**
41 * Inserts a new user across all authentication chains.
42 * @param user The user to insert.
43 */
44 public void insert(final User user) {
45 chains().of(stream -> stream.forEach(c -> c.update(user)));
46 }
47
48 /**
49 * Changes the password for a user across all authentication chains.
50 * @param username The username for which to change the password.
51 * @param password The new password.
52 * @return True if the password was successfully changed in all chains, false otherwise.
53 */
54 public boolean changePassword(final String username, final String password) {
55 return chains().get(stream -> stream.allMatch(c -> c.changePassword(username, password)));
56 }
57
58 /**
59 * Deletes a user from all authentication chains.
60 * @param user The user to delete.
61 */
62 public void delete(final User user) {
63 chains().of(stream -> stream.forEach(c -> c.delete(user)));
64 }
65
66 /**
67 * Loads user information by processing through all authentication chains.
68 * @param user The user template containing search criteria.
69 * @return The loaded and enriched user information.
70 */
71 public User load(final User user) {
72 User u = user;
73 for (final AuthenticationChain chain : chains) {
74 u = chain.load(u);
75 }
76 return u;
77 }
78
79 /**
80 * Adds an authentication chain to the manager.
81 * @param chain The authentication chain to add.
82 */
83 public void addChain(final AuthenticationChain chain) {
84 chains = ArrayUtils.addAll(chains, chain);
85 }
86
87 /**
88 * Returns a stream of authentication chains.
89 * @return A stream wrapper of the authentication chains.
90 */
91 protected StreamOf<AuthenticationChain> chains() {
92 return stream(chains);
93 }
94
95 }