| New file |
| | |
| | | package com.stylefeng.guns.modular.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.core.common.constant.factory.ConstantFactory; |
| | | import com.stylefeng.guns.core.shiro.ShiroUser; |
| | | import com.stylefeng.guns.core.util.Convert; |
| | | import com.stylefeng.guns.core.util.SinataUtil; |
| | | import com.stylefeng.guns.modular.system.dao.RoleMapper; |
| | | import com.stylefeng.guns.modular.system.dao.SysUserCompanyScopeMapper; |
| | | import com.stylefeng.guns.modular.system.model.Role; |
| | | import com.stylefeng.guns.modular.system.model.SysUserCompanyScope; |
| | | import com.stylefeng.guns.modular.system.model.User; |
| | | import com.stylefeng.guns.modular.system.service.ISysUserCompanyScopeService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Service |
| | | public class SysUserCompanyScopeServiceImpl extends ServiceImpl<SysUserCompanyScopeMapper, SysUserCompanyScope> |
| | | implements ISysUserCompanyScopeService { |
| | | |
| | | @Autowired |
| | | private RoleMapper roleMapper; |
| | | |
| | | @Override |
| | | public void ensureDefaultScope(User user) { |
| | | if (user == null || user.getId() == null || user.getRoleType() == null || user.getRoleType() != 2 |
| | | || user.getObjectId() == null) { |
| | | return; |
| | | } |
| | | Integer count = this.baseMapper.countScopesByUserId(user.getId()); |
| | | if (count != null && count > 0) { |
| | | return; |
| | | } |
| | | SysUserCompanyScope scope = buildScope(user.getId(), user.getObjectId(), user.getRoleid(), user.getDeptid(), 1); |
| | | this.baseMapper.insert(scope); |
| | | } |
| | | |
| | | @Override |
| | | public List<Map<String, Object>> selectScopesByUserId(Integer userId) { |
| | | return this.baseMapper.selectScopesByUserId(userId); |
| | | } |
| | | |
| | | @Override |
| | | public SysUserCompanyScope selectActiveScope(Integer userId, Integer companyId) { |
| | | return this.baseMapper.selectActiveScope(userId, companyId); |
| | | } |
| | | |
| | | @Override |
| | | public void applyScopeToShiroUser(ShiroUser shiroUser, SysUserCompanyScope scope) { |
| | | if (shiroUser == null || scope == null) { |
| | | return; |
| | | } |
| | | shiroUser.setObjectId(scope.getCompanyId()); |
| | | shiroUser.setActiveCompanyId(scope.getCompanyId()); |
| | | shiroUser.setDeptId(scope.getDeptid()); |
| | | if (SinataUtil.isEmpty(scope.getRoleid())) { |
| | | shiroUser.setRoleList(new ArrayList<Integer>()); |
| | | shiroUser.setRoleNames(new ArrayList<String>()); |
| | | shiroUser.setAdmin(false); |
| | | return; |
| | | } |
| | | Integer[] roleArray = Convert.toIntArray(scope.getRoleid()); |
| | | List<Integer> roleList = new ArrayList<Integer>(); |
| | | List<String> roleNameList = new ArrayList<String>(); |
| | | for (int roleId : roleArray) { |
| | | roleList.add(roleId); |
| | | roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId)); |
| | | } |
| | | shiroUser.setRoleList(roleList); |
| | | shiroUser.setRoleNames(roleNameList); |
| | | List<Role> roles = roleMapper.selectBatchIds(roleList); |
| | | shiroUser.setAdmin(roles.stream().anyMatch(s -> "administrator".equals(s.getTips()))); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void saveScopes(Integer userId, String scopesJson, Integer fallbackCompanyId, String fallbackRoleid, Integer fallbackDeptid) { |
| | | this.baseMapper.deleteByUserId(userId); |
| | | JSONArray array = parseScopes(scopesJson); |
| | | if (array == null || array.isEmpty()) { |
| | | if (fallbackCompanyId != null) { |
| | | this.baseMapper.insert(buildScope(userId, fallbackCompanyId, fallbackRoleid, fallbackDeptid, 1)); |
| | | } |
| | | return; |
| | | } |
| | | boolean hasDefault = false; |
| | | for (int i = 0; i < array.size(); i++) { |
| | | JSONObject item = array.getJSONObject(i); |
| | | Integer companyId = item.getInteger("companyId"); |
| | | String roleid = item.getString("roleid"); |
| | | Integer deptid = item.getInteger("deptid"); |
| | | if (companyId == null || deptid == null) { |
| | | continue; |
| | | } |
| | | Integer isDefault = item.getInteger("isDefault"); |
| | | if (isDefault != null && isDefault == 1 && !hasDefault) { |
| | | hasDefault = true; |
| | | } else { |
| | | isDefault = 2; |
| | | } |
| | | this.baseMapper.insert(buildScope(userId, companyId, roleid, deptid, isDefault)); |
| | | } |
| | | if (!hasDefault) { |
| | | SysUserCompanyScope first = this.selectOne(new EntityWrapper<SysUserCompanyScope>() |
| | | .eq("userId", userId) |
| | | .eq("status", 1) |
| | | .orderBy("id", true)); |
| | | if (first != null) { |
| | | first.setIsDefault(1); |
| | | first.setUpdateTime(new Date()); |
| | | this.updateById(first); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void deleteByUserId(Integer userId) { |
| | | this.baseMapper.deleteByUserId(userId); |
| | | } |
| | | |
| | | private JSONArray parseScopes(String scopesJson) { |
| | | if (SinataUtil.isEmpty(scopesJson)) { |
| | | return null; |
| | | } |
| | | return JSON.parseArray(scopesJson); |
| | | } |
| | | |
| | | private SysUserCompanyScope buildScope(Integer userId, Integer companyId, String roleid, Integer deptid, Integer isDefault) { |
| | | SysUserCompanyScope scope = new SysUserCompanyScope(); |
| | | scope.setUserId(userId); |
| | | scope.setCompanyId(companyId); |
| | | scope.setRoleid(roleid); |
| | | scope.setDeptid(deptid); |
| | | scope.setIsDefault(isDefault); |
| | | scope.setStatus(1); |
| | | scope.setCreateTime(new Date()); |
| | | scope.setUpdateTime(new Date()); |
| | | return scope; |
| | | } |
| | | } |