yangys
2024-04-04 ed4a5236bab800094be4a8378f5098eebe3de6ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.qianwen.smartman.modules.system.service.impl;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
 
import javax.validation.constraints.NotEmpty;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.constant.BladeConstant;
import com.qianwen.core.tool.node.ForestNodeMerger;
import com.qianwen.core.tool.utils.CollectionUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.modules.system.entity.Role;
import com.qianwen.smartman.modules.system.entity.RoleMenu;
import com.qianwen.smartman.modules.system.entity.RoleScope;
import com.qianwen.smartman.modules.system.mapper.RoleMapper;
import com.qianwen.smartman.modules.system.service.IRoleMenuService;
import com.qianwen.smartman.modules.system.service.IRoleScopeService;
import com.qianwen.smartman.modules.system.service.IRoleService;
import com.qianwen.smartman.modules.system.vo.RoleVO;
import com.qianwen.smartman.modules.system.wrapper.RoleWrapper;
 
@Service
@Validated
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/system/service/impl/RoleServiceImpl.class */
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    private final IRoleMenuService roleMenuService;
    private final IRoleScopeService roleScopeService;
 
    public RoleServiceImpl(final IRoleMenuService roleMenuService, final IRoleScopeService roleScopeService) {
        this.roleMenuService = roleMenuService;
        this.roleScopeService = roleScopeService;
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public IPage<RoleVO> selectRolePage(IPage<RoleVO> page, RoleVO role) {
        return page.setRecords(((RoleMapper) this.baseMapper).selectRolePage(page, role));
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public List<RoleVO> tree(String tenantId) {
        String userRole = AuthUtil.getUserRole();
        String excludeRole = null;
        if (!CollectionUtil.contains(Func.toStrArray(userRole), "admin") && !CollectionUtil.contains(Func.toStrArray(userRole), "administrator")) {
            excludeRole = "admin";
        }
        return ForestNodeMerger.merge(((RoleMapper) this.baseMapper).tree(tenantId, excludeRole));
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    @Transactional(rollbackFor = {Exception.class})
    public boolean grant(@NotEmpty List<Long> roleIds, List<Long> menuIds, List<Long> dataScopeIds, List<Long> apiScopeIds) {
        grantRoleMenu(roleIds, menuIds);
        grantDataScope(roleIds, dataScopeIds);
        grantApiScope(roleIds, apiScopeIds);
        return true;
    }
 
    private boolean grantRoleMenu(List<Long> roleIds, List<Long> menuIds) {
        Long administratorCount = this.baseMapper.selectCount(Wrappers.<Role>query().lambda().eq(Role::getRoleAlias, "administrator").in(Role::getId, roleIds));
        /*
        Long administratorCount = ((RoleMapper) this.baseMapper).selectCount((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> {
            return v0.getRoleAlias();
        }, "administrator")).in((v0) -> {
            return v0.getId();
        }, roleIds));*/
        if (!AuthUtil.isAdministrator() && administratorCount.longValue() > 0) {
            throw new ServiceException("无权配置超管角色!");
        }
        Long adminCount = this.baseMapper.selectCount(Wrappers.<Role>query().lambda().eq(Role::getRoleAlias, "admin").in(Role::getId, roleIds));
        /*
        Long adminCount = ((RoleMapper) this.baseMapper).selectCount((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> {
            return v0.getRoleAlias();
        }, "admin")).in((v0) -> {
            return v0.getId();
        }, roleIds));*/
        if (!AuthUtil.isAdmin() && adminCount.longValue() > 0) {
            throw new ServiceException("无权配置管理员角色!");
        }
        this.roleMenuService.remove(Wrappers.<RoleMenu>update().lambda().in(RoleMenu::getRoleId, roleIds));
        /*
        this.roleMenuService.remove((Wrapper) Wrappers.update().lambda().in((v0) -> {
            return v0.getRoleId();
        }, roleIds));*/
        List<RoleMenu> roleMenus = new ArrayList<>();
        roleIds.forEach(roleId -> {
            menuIds.forEach(menuId -> {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenus.add(roleMenu);
            });
        });
        this.roleMenuService.saveBatch(roleMenus);
        recursionRoleMenu(roleIds, menuIds);
        return true;
    }
 
    private void recursionRoleMenu(List<Long> roleIds, List<Long> menuIds) {
        //Role::getParentId RoleMenu::getRoleId  RoleMenu::getMenuId
        roleIds.forEach(roleId -> {
            this.baseMapper.selectList( Wrappers.<Role>query().lambda().eq(Role::getParentId, roleId)).forEach(role -> {
                    List<RoleMenu> roleMenuList = this.roleMenuService.list(Wrappers.<RoleMenu>query().lambda().eq(RoleMenu::getRoleId, role.getId()));
                    List<Long> collectRoleMenuIds = roleMenuList.stream().map((v0) -> {
                        return v0.getMenuId();
                    }).filter(menuId -> {
                        return !menuIds.contains(menuId);
                    }).collect(Collectors.toList());
                    
                    if (collectRoleMenuIds.size() > 0) {
                        this.roleMenuService.remove(Wrappers.<RoleMenu>update().lambda().eq(RoleMenu::getRoleId, role.getId()).in(RoleMenu::getMenuId, collectRoleMenuIds));
                        recursionRoleMenu(Collections.singletonList(role.getId()), menuIds);
                    }
            });
        });
    }
 
    private boolean grantDataScope(List<Long> roleIds, List<Long> dataScopeIds) {
        this.roleScopeService.remove(Wrappers.<RoleScope>update().lambda()
                .eq(RoleScope::getScopeCategory, CommonConstant.DATA_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds));
        /*
        this.roleScopeService.remove((Wrapper) ((LambdaUpdateWrapper) Wrappers.update().lambda().eq((v0) -> {
            return v0.getScopeCategory();
        }, CommonConstant.DATA_SCOPE_CATEGORY)).in((v0) -> {
            return v0.getRoleId();
        }, roleIds));*/
        List<RoleScope> roleDataScopes = new ArrayList<>();
        roleIds.forEach(roleId -> {
            dataScopeIds.forEach(scopeId -> {
                RoleScope roleScope = new RoleScope();
                roleScope.setScopeCategory(CommonConstant.DATA_SCOPE_CATEGORY);
                roleScope.setRoleId(roleId);
                roleScope.setScopeId(scopeId);
                roleDataScopes.add(roleScope);
            });
        });
        this.roleScopeService.saveBatch(roleDataScopes);
        return true;
    }
 
    private boolean grantApiScope(List<Long> roleIds, List<Long> apiScopeIds) {
        this.roleScopeService.remove(Wrappers.<RoleScope>update().lambda()
                .eq(RoleScope::getScopeCategory, CommonConstant.API_SCOPE_CATEGORY).in(RoleScope::getRoleId, roleIds));
        /*
        this.roleScopeService.remove((Wrapper) ((LambdaUpdateWrapper) Wrappers.update().lambda().eq((v0) -> {
            return v0.getScopeCategory();
        }, CommonConstant.API_SCOPE_CATEGORY)).in((v0) -> {
            return v0.getRoleId();
        }, roleIds));*/
        List<RoleScope> roleApiScopes = new ArrayList<>();
        roleIds.forEach(roleId -> {
            apiScopeIds.forEach(scopeId -> {
                RoleScope roleScope = new RoleScope();
                roleScope.setScopeCategory(CommonConstant.API_SCOPE_CATEGORY);
                roleScope.setScopeId(scopeId);
                roleScope.setRoleId(roleId);
                roleApiScopes.add(roleScope);
            });
        });
        this.roleScopeService.saveBatch(roleApiScopes);
        return true;
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public String getRoleIds(String tenantId, String roleNames) {
        List<Role> roleList = this.baseMapper.selectList(Wrappers.<Role>query().lambda().eq(Role::getTenantId, tenantId).in(Role::getRoleName, Func.toStrList(roleNames)));
        
        /*
        List<Role> roleList = ((RoleMapper) this.baseMapper).selectList((Wrapper) ((LambdaQueryWrapper) Wrappers.query().lambda().eq((v0) -> {
            return v0.getTenantId();
        }, tenantId)).in((v0) -> {
            return v0.getRoleName();
        }, Func.toStrList(roleNames)));*/
        if (roleList != null && roleList.size() > 0) {
            return (String) roleList.stream().map(role -> {
                return Func.toStr(role.getId());
            }).distinct().collect(Collectors.joining(","));
        }
        return null;
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public List<String> getRoleNames(String roleIds) {
        return ((RoleMapper) this.baseMapper).getRoleNames(Func.toLongArray(roleIds));
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public List<String> getRoleAliases(String roleIds) {
        return ((RoleMapper) this.baseMapper).getRoleAliases(Func.toLongArray(roleIds));
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public Role submit(Role role) {
        if (!AuthUtil.isAdministrator() && Func.toStr(role.getRoleAlias()).equals("administrator")) {
            throw new ServiceException("无权限创建超管角色!");
        }
        if (Func.isEmpty(role.getParentId())) {
            role.setTenantId(AuthUtil.getTenantId());
            role.setParentId(BladeConstant.TOP_PARENT_ID);
        }
        if (role.getParentId().longValue() > 0) {
            Role parent = (Role) getById(role.getParentId());
            if (Func.toLong(role.getParentId()) == Func.toLong(role.getId())) {
                throw new ServiceException("父节点不可选择自身!");
            }
            role.setTenantId(parent.getTenantId());
        }
        role.setIsDeleted(0);
        saveOrUpdate(role);
        return role;
    }
 
    @Override // org.springblade.modules.system.service.IRoleService
    public List<RoleVO> search(String roleName, Long parentId) {
        LambdaQueryWrapper<Role> lambda = Wrappers.<Role>query().lambda();
        
        //Wrapper lambda = Wrappers.query().lambda();
        if (Func.isNotEmpty(roleName)) {
            lambda.like((v0) -> {
                return v0.getRoleName();
            }, roleName);
        }
        if (Func.isNotEmpty(parentId) && parentId.longValue() > BladeConstant.TOP_PARENT_ID.longValue()) {
            lambda.eq((v0) -> {
                return v0.getParentId();
            }, parentId);
        }
        List<Role> roleList = ((RoleMapper) this.baseMapper).selectList(lambda);
        return RoleWrapper.build().listNodeVO(roleList);
    }
}