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.app.service;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.List;
21  
22  import org.codelibs.core.beans.util.BeanUtil;
23  import org.codelibs.fess.Constants;
24  import org.codelibs.fess.app.pager.RolePager;
25  import org.codelibs.fess.mylasta.direction.FessConfig;
26  import org.codelibs.fess.opensearch.user.cbean.RoleCB;
27  import org.codelibs.fess.opensearch.user.exbhv.RoleBhv;
28  import org.codelibs.fess.opensearch.user.exbhv.UserBhv;
29  import org.codelibs.fess.opensearch.user.exentity.Role;
30  import org.codelibs.fess.util.ComponentUtil;
31  import org.dbflute.cbean.result.PagingResultBean;
32  import org.dbflute.optional.OptionalEntity;
33  
34  import jakarta.annotation.Resource;
35  
36  /**
37   * Service class for managing roles.
38   */
39  public class RoleService {
40  
41      /**
42       * The behavior for roles.
43       */
44      @Resource
45      protected RoleBhv roleBhv;
46  
47      /**
48       * The Fess configuration.
49       */
50      @Resource
51      protected FessConfig fessConfig;
52  
53      /**
54       * The behavior for users.
55       */
56      @Resource
57      protected UserBhv userBhv;
58  
59      /**
60       * Constructor.
61       */
62      public RoleService() {
63          super();
64      }
65  
66      /**
67       * Gets a list of roles based on the pager.
68       * @param rolePager The pager for roles.
69       * @return A list of roles.
70       */
71      public List<Role> getRoleList(final RolePager rolePager) {
72  
73          final PagingResultBean<Role> roleList = roleBhv.selectPage(cb -> {
74              cb.paging(rolePager.getPageSize(), rolePager.getCurrentPageNumber());
75              setupListCondition(cb, rolePager);
76          });
77  
78          // update pager
79          BeanUtil.copyBeanToBean(roleList, rolePager, option -> option.include(Constants.PAGER_CONVERSION_RULE));
80          rolePager.setPageNumberList(roleList.pageRange(op -> {
81              op.rangeSize(fessConfig.getPagingPageRangeSizeAsInteger());
82          }).createPageNumberList());
83  
84          return roleList;
85      }
86  
87      /**
88       * Gets a role by its ID.
89       * @param id The ID of the role.
90       * @return An optional entity of the role.
91       */
92      public OptionalEntity<Role> getRole(final String id) {
93          return roleBhv.selectByPK(id);
94      }
95  
96      /**
97       * Stores a role.
98       * @param role The role to store.
99       */
100     public void store(final Role role) {
101         ComponentUtil.getLdapManager().insert(role);
102 
103         roleBhv.insertOrUpdate(role, op -> {
104             op.setRefreshPolicy(Constants.TRUE);
105         });
106 
107     }
108 
109     /**
110      * Deletes a role.
111      * @param role The role to delete.
112      */
113     public void delete(final Role role) {
114         ComponentUtil.getLdapManager().delete(role);
115 
116         roleBhv.delete(role, op -> {
117             op.setRefreshPolicy(Constants.TRUE);
118         });
119 
120         userBhv.selectCursor(cb -> cb.query().setRoles_Equal(role.getId()), entity -> {
121             entity.setRoles(
122                     stream(entity.getRoles()).get(stream -> stream.filter(s -> !s.equals(role.getId())).toArray(n -> new String[n])));
123             userBhv.insertOrUpdate(entity);
124         });
125     }
126 
127     /**
128      * Sets up the list condition for the role query.
129      * @param cb The role condition bean.
130      * @param rolePager The role pager.
131      */
132     protected void setupListCondition(final RoleCB cb, final RolePager rolePager) {
133         if (rolePager.id != null) {
134             cb.query().docMeta().setId_Equal(rolePager.id);
135         }
136         // TODO Long, Integer, String supported only.
137 
138         // setup condition
139         cb.query().addOrderBy_Name_Asc();
140 
141         // search
142 
143     }
144 
145     /**
146      * Gets a list of available roles.
147      * @return A list of available roles.
148      */
149     public List<Role> getAvailableRoleList() {
150         return roleBhv.selectList(cb -> {
151             cb.query().matchAll();
152             cb.query().addOrderBy_Name_Asc();
153             cb.paging(fessConfig.getPageRoleMaxFetchSizeAsInteger(), 1);
154         });
155     }
156 
157 }