业务权限优化

This commit is contained in:
易焱 2019-08-02 01:08:21 +08:00
parent 8740a34a4a
commit 5535ae1ce0
16 changed files with 160 additions and 58 deletions

View File

@ -6,10 +6,12 @@ import com.cwhelp.common.utils.EntityBeanUtil;
import com.cwhelp.common.utils.ResultVoUtil;
import com.cwhelp.common.utils.StatusUtil;
import com.cwhelp.common.vo.ResultVo;
import com.cwhelp.component.shiro.ShiroUtil;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.service.BssDeptService;
import com.cwhelp.modules.business.service.BssPlatformService;
import com.cwhelp.modules.system.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
@ -46,9 +48,15 @@ public class BssDeptController {
// 创建匹配器进行动态查询匹配
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("name", match -> match.contains())
.withMatcher("bssPlatform.name",match -> match.contains());
.withMatcher("name", match -> match.contains());
User user = ShiroUtil.getSubject();
if (1 != user.getBssPlatform().getId()) {
matcher.withMatcher("bssPlatform.id",match -> match.contains());
BssPlatform bssPlatform = new BssPlatform();
bssPlatform.setId(user.getBssPlatform().getId());
bssDept.setBssPlatform(bssPlatform);
}
// 获取数据列表
Example<BssDept> example = Example.of(bssDept, matcher);
Page<BssDept> list = bssDeptService.getPageList(example);

View File

@ -7,8 +7,14 @@ import com.cwhelp.common.utils.EntityBeanUtil;
import com.cwhelp.common.utils.ResultVoUtil;
import com.cwhelp.common.utils.StatusUtil;
import com.cwhelp.common.vo.ResultVo;
import com.cwhelp.component.shiro.ShiroUtil;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssEmployee;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.service.BssDeptService;
import com.cwhelp.modules.business.service.BssEmployeeService;
import com.cwhelp.modules.system.domain.User;
import com.cwhelp.modules.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
@ -32,6 +38,12 @@ public class BssEmployeeController {
@Autowired
private BssEmployeeService bssEmployeeService;
@Autowired
private BssDeptService bssDeptService;
@Autowired
private UserService userService;
/**
* 列表页面
*/
@ -39,13 +51,12 @@ public class BssEmployeeController {
@RequiresPermissions("bss:employee:index")
public String index(Model model, BssEmployee bssEmployee) {
// 创建匹配器进行动态查询匹配
ExampleMatcher matcher = ExampleMatcher.matching()
.withMatcher("name", match -> match.contains());
// 获取数据列表
Example<BssEmployee> example = Example.of(bssEmployee, matcher);
Page<BssEmployee> list = bssEmployeeService.getPageList(example);
User user = ShiroUtil.getSubject();
List<BssDept> bssDepts = null;
if (1 != user.getBssPlatform().getId()) {
bssDepts = bssDeptService.findBssDeptByBssPlatformAndId(user.getBssPlatform());
}
Page<BssEmployee> list = bssEmployeeService.getPageList(bssDepts,bssEmployee);
// 封装数据
model.addAttribute("list", list.getContent());
@ -58,7 +69,11 @@ public class BssEmployeeController {
*/
@GetMapping("/add")
@RequiresPermissions("bss:employee:add")
public String toAdd() {
public String toAdd(Model model) {
User user = ShiroUtil.getSubject();
BssPlatform bssPlatform = userService.getById(user.getId()).getBssPlatform();
List<BssDept> bssDepts = getBssDepts(user, bssPlatform);
model.addAttribute("bssDepts",bssDepts);
return "/business/employee/add";
}
@ -68,10 +83,30 @@ public class BssEmployeeController {
@GetMapping("/edit/{id}")
@RequiresPermissions("bss:employee:edit")
public String toEdit(@PathVariable("id") BssEmployee bssEmployee, Model model) {
User user = ShiroUtil.getSubject();
BssPlatform bssPlatform = userService.getById(user.getId()).getBssPlatform();
List<BssDept> bssDepts = getBssDepts(user, bssPlatform);
model.addAttribute("bssDepts",bssDepts);
model.addAttribute("bssEmployee", bssEmployee);
return "/business/employee/add";
}
/**
* 获取当前用户下的部门
* @param user
* @param bssPlatform
* @return
*/
private List<BssDept> getBssDepts(User user, BssPlatform bssPlatform) {
List<BssDept> bssDepts = null;
if (1 != user.getBssPlatform().getId()) {
bssDepts = bssDeptService.findBssDeptByBssPlatformAndId(bssPlatform);
} else {
bssDepts = bssDeptService.findAll();
}
return bssDepts;
}
/**
* 保存添加/修改的数据
* @param valid 验证对象

View File

@ -7,9 +7,11 @@ import com.cwhelp.common.utils.EntityBeanUtil;
import com.cwhelp.common.utils.ResultVoUtil;
import com.cwhelp.common.utils.StatusUtil;
import com.cwhelp.common.vo.ResultVo;
import com.cwhelp.component.shiro.ShiroUtil;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.service.BssPlatformService;
import com.cwhelp.modules.system.domain.Dept;
import com.cwhelp.modules.system.domain.User;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
@ -48,7 +50,12 @@ public class BssPlatformController {
.withMatcher("name", match -> match.contains())
.withMatcher("contact", match -> match.contains())
.withMatcher("email", match -> match.contains());
User user = ShiroUtil.getSubject();
// 判定当前用户的 所属平台 1为系统平台
if (1 != user.getBssPlatform().getId()) {
matcher.withMatcher("id",match -> match.contains());
bssPlatform.setId(user.getBssPlatform().getId());
}
// 获取数据列表
Example<BssPlatform> example = Example.of(bssPlatform, matcher);
Page<BssPlatform> list = bssPlatformService.getPageList(example);

View File

@ -1,5 +1,6 @@
package com.cwhelp.admin.business.validator;
import com.cwhelp.modules.business.domain.BssDept;
import lombok.Data;
import javax.validation.constraints.Email;
@ -17,20 +18,18 @@ public class BssEmployeeValid implements Serializable {
@NotEmpty(message = "名称不能为空")
private String name;
@NotNull(message = "部门不能为空")
private Long dept_id;
private BssDept bssDept;
@NotEmpty(message = "职位不能为空")
private String position;
@NotEmpty(message = "手机号码不能为空")
@Pattern(regexp = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$", message = "手机号码格式不正确")
private String phone_num;
private String phoneNum;
@NotEmpty(message = "身份证号码不能为空")
@Pattern(regexp = "(^(\\d{14}|\\d{17})(\\d|[xX])$)?", message = "身份证号码错误")
private String card;
@NotEmpty(message = "邮箱不能为空")
@Email(message = "邮箱格式不正确")
private String email;
@NotEmpty(message = "员工状态不能为空")
private String employee_status;
@NotEmpty(message = "学历不能为空")
private String education;
}

View File

@ -15,7 +15,11 @@
<div class="layui-form-item">
<label class="layui-form-label required">部门</label>
<div class="layui-input-inline">
<input class="layui-input" type="text" name="dept_id" placeholder="请输入部门" th:value="${bssEmployee?.dept_id}">
<div class="layui-input-inline">
<select name="bssDept" mo-selected="${dept?.bssPlatform?.id}" mo-empty="" lay-verify="bssDept">
<option th:each="bssDept,userStat:${bssDepts}" th:value="${bssDept.id}" th:text="${bssDept.name}" th:selected="${bssDept.id == bssEmployee?.bssDept?.id}"></option>
</select>
</div>
</div>
</div>
<div class="layui-form-item">
@ -27,7 +31,7 @@
<div class="layui-form-item">
<label class="layui-form-label required">手机号码</label>
<div class="layui-input-inline">
<input class="layui-input" type="text" name="phone_num" placeholder="请输入手机号码" th:value="${bssEmployee?.phone_num}">
<input class="layui-input" type="text" name="phoneNum" placeholder="请输入手机号码" th:value="${bssEmployee?.phoneNum}">
</div>
</div>
<div class="layui-form-item">
@ -42,12 +46,6 @@
<input class="layui-input" type="text" name="email" placeholder="请输入邮箱" th:value="${bssEmployee?.email}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label required">员工状态</label>
<div class="layui-input-inline">
<select name="employeeStatus" mo:dict="EMPLOYEE_TYPE" mo-selected="${bssEmployee?.employeeStatus}" mo-empty="" lay-verify="employeeStatus"></select>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label required">学历</label>
<div class="layui-input-inline">

View File

@ -32,12 +32,8 @@
<tr>
<th>邮箱</th>
<td th:text="${bssEmployee.email}"></td>
<th>员工状态</th>
<td th:text="${#dicts.keyValue('EMPLOYEE_TYPE', bssEmployee.employeeStatus)}"></td>
</tr>
<tr>
<th>学历</th>
<td th:text="${#dicts.keyValue('EDUCATION', bssEmployee.education)}" colspan="3"></td>
<td th:text="${#dicts.keyValue('EDUCATION', bssEmployee.education)}"></td>
</tr>
<tr>
<th>创建者</th>

View File

@ -44,6 +44,7 @@
<dd><a class="ajax-status" th:href="@{/bss/employee/status/ok}">启用</a></dd>
<dd><a class="ajax-status" th:href="@{/bss/employee/status/freezed}">冻结</a></dd>
<dd><a class="ajax-status" th:href="@{/bss/employee/status/delete}">删除</a></dd>
<dd><a class="ajax-status" th:href="@{/bss/employee/role}">授权</a></dd>
</dl>
</div>
</div>
@ -62,7 +63,6 @@
<th>手机号码</th>
<th>身份证号码</th>
<th>邮箱</th>
<th>员工状态</th>
<th>学历</th>
<th>创建时间</th>
<th>更新时间</th>
@ -80,8 +80,7 @@
<td th:text="${item.phoneNum}">手机号码</td>
<td th:text="${item.card}">身份证号码</td>
<td th:text="${item.email}">邮箱</td>
<td th:text="${#dicts.keyValue('EMPLOYEE_TYPE', item.type)}">员工状态</td>
<td th:text="${#dicts.keyValue('EDUCATION', item.type)}">学历</td>
<td th:text="${#dicts.keyValue('EDUCATION', item.education)}">学历</td>
<td th:text="${#dates.format(item.createDate, 'yyyy-MM-dd HH:mm:ss')}">创建时间</td>
<td th:text="${#dates.format(item.updateDate, 'yyyy-MM-dd HH:mm:ss')}">更新时间</td>
<td th:text="${#dicts.dataStatus(item.status)}">数据状态</td>
@ -89,6 +88,7 @@
<a class="open-popup" data-title="编辑员工" th:attr="data-url=@{'/bss/employee/edit/'+${item.id}}" data-size="auto" href="#">编辑</a>
<a class="open-popup" data-title="详细信息" th:attr="data-url=@{'/bss/employee/detail/'+${item.id}}" data-size="800,600" href="#">详细</a>
<a class="ajax-get" data-msg="您是否确认删除" th:href="@{/bss/employee/status/delete(ids=${item.id})}">删除</a>
<a class="open-popup" th:href="@{/bss/employee/role}">授权</a>
</td>
</tr>
</tbody>

View File

@ -49,9 +49,6 @@ public class BssEmployee implements Serializable {
private String card;
// 邮箱
private String email;
// 员工状态
@Column(name = "employee_status")
private String employeeStatus;
// 学历
private String education;
// 备注

View File

@ -72,6 +72,7 @@ public class BssPlatform implements Serializable {
@JoinColumn(name="update_by")
@JsonIgnore
private User updateBy;
// 数据状态
private Byte status = StatusEnum.OK.getCode();
}

View File

@ -1,12 +1,23 @@
package com.cwhelp.modules.business.repository;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.system.repository.BaseRepository;
import java.util.List;
/**
* @author yan.y
* @date 2019/07/29
*/
public interface BssDeptRepository extends BaseRepository<BssDept, Long> {
/**
* 根据平台ID查询业务部门
* @param bssplatform
* @return
*/
List<BssDept> findByBssPlatformEquals(BssPlatform bssplatform);
}

View File

@ -2,11 +2,12 @@ package com.cwhelp.modules.business.repository;
import com.cwhelp.modules.business.domain.BssEmployee;
import com.cwhelp.modules.system.repository.BaseRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author yan.y
* @date 2019/07/31
*/
public interface BssEmployeeRepository extends BaseRepository<BssEmployee, Long> {
public interface BssEmployeeRepository extends BaseRepository<BssEmployee, Long>, JpaSpecificationExecutor<BssEmployee> {
}

View File

@ -2,6 +2,7 @@ package com.cwhelp.modules.business.service;
import com.cwhelp.common.enums.StatusEnum;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssPlatform;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
@ -38,4 +39,8 @@ public interface BssDeptService {
*/
@Transactional
Boolean updateStatus(StatusEnum statusEnum, List<Long> idList);
List<BssDept> findBssDeptByBssPlatformAndId(BssPlatform bssplatform);
List<BssDept> findAll();
}

View File

@ -1,6 +1,7 @@
package com.cwhelp.modules.business.service;
import com.cwhelp.common.enums.StatusEnum;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssEmployee;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
@ -22,6 +23,14 @@ public interface BssEmployeeService {
*/
Page<BssEmployee> getPageList(Example<BssEmployee> example);
/**
* 复杂查询sql
* @param depts
* @param bssEmployee
* @return
*/
Page<BssEmployee> getPageList(List<BssDept> depts,BssEmployee bssEmployee);
/**
* 根据ID查询数据
* @param id 主键ID

View File

@ -3,6 +3,7 @@ package com.cwhelp.modules.business.service.impl;
import com.cwhelp.common.data.PageSort;
import com.cwhelp.common.enums.StatusEnum;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.repository.BssDeptRepository;
import com.cwhelp.modules.business.service.BssDeptService;
import org.springframework.beans.factory.annotation.Autowired;
@ -63,4 +64,14 @@ public class BssDeptServiceImpl implements BssDeptService {
public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
return bssDeptRepository.updateStatus(statusEnum.getCode(), idList) > 0;
}
@Override
public List<BssDept> findBssDeptByBssPlatformAndId(BssPlatform bssplatform) {
return bssDeptRepository.findByBssPlatformEquals(bssplatform);
}
@Override
public List<BssDept> findAll() {
return bssDeptRepository.findAll();
}
}

View File

@ -2,16 +2,23 @@ package com.cwhelp.modules.business.service.impl;
import com.cwhelp.common.data.PageSort;
import com.cwhelp.common.enums.StatusEnum;
import com.cwhelp.modules.business.domain.BssDept;
import com.cwhelp.modules.business.domain.BssEmployee;
import com.cwhelp.modules.business.domain.BssPlatform;
import com.cwhelp.modules.business.repository.BssEmployeeRepository;
import com.cwhelp.modules.business.service.BssEmployeeService;
import com.cwhelp.modules.system.domain.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
/**
@ -46,6 +53,45 @@ public class BssEmployeeServiceImpl implements BssEmployeeService {
return bssEmployeeRepository.findAll(example, page);
}
@Override
public Page<BssEmployee> getPageList(List<BssDept> depts, BssEmployee bssEmployee) {
// 创建分页对象
PageRequest page = PageSort.pageRequest(Sort.Direction.ASC);
// 使用Specification复杂查询
return bssEmployeeRepository.findAll(new Specification<BssEmployee>(){
@Override
public Predicate toPredicate(Root<BssEmployee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> preList = new ArrayList<>();
if(null != depts && depts.size() > 0){
List<Long> deptIn = new ArrayList<>();
depts.forEach(item -> deptIn.add(item.getId()));
Join<BssEmployee, BssDept> join = root.join("bssDept", JoinType.INNER);
CriteriaBuilder.In<Long> in = cb.in(join.get("id").as(Long.class));
deptIn.forEach(in::value);
preList.add(in);
}
// 名称
if(bssEmployee.getName() != null){
preList.add(cb.like(root.get("name").as(String.class), "%" + bssEmployee.getName() + "%"));
}
// 号码
if(bssEmployee.getPhoneNum() != null){
preList.add(cb.like(root.get("phoneNum").as(String.class), "%" + bssEmployee.getPhoneNum() + "%"));
}
// 数据状态
if(bssEmployee.getStatus() != null){
preList.add(cb.equal(root.get("status").as(Byte.class), bssEmployee.getStatus()));
}
Predicate[] pres = new Predicate[preList.size()];
return query.where(preList.toArray(pres)).getRestriction();
}
}, page);
}
/**
* 保存数据
* @param bssEmployee 实体对象

View File

@ -82,15 +82,6 @@ public class UserServiceImpl implements UserService {
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> preList = new ArrayList<>();
if(user.getId() != null){
preList.add(cb.equal(root.get("id").as(Long.class), user.getId()));
}
if(user.getUsername() != null){
preList.add(cb.equal(root.get("username").as(String.class), user.getUsername()));
}
if(user.getNickname() != null){
preList.add(cb.like(root.get("nickname").as(String.class), "%"+ user.getNickname() + "%"));
}
if(user.getDept() != null){
// 联级查询部门
Dept dept = user.getDept();
@ -105,20 +96,7 @@ public class UserServiceImpl implements UserService {
preList.add(in);
}
// 1 为财务帮平台超级账号
if (1 == user.getBssPlatform().getId()) {
// 查询所有平台的账号
BssPlatform bssPlatform = user.getBssPlatform();
List<Long> bssPlatformIn = new ArrayList<>();
bssPlatformIn.add(bssPlatform.getId());
List<BssPlatform> bssPlatforms = bssPlatformRepository.findAll();
bssPlatforms.forEach(item -> bssPlatformIn.add(item.getId()));
Join<User, BssPlatform> join = root.join("bssPlatform", JoinType.INNER);
CriteriaBuilder.In<Long> in = cb.in(join.get("id").as(Long.class));
bssPlatformIn.forEach(in::value);
preList.add(in);
} else {
// 查询所有平台的账号
if (1 != user.getBssPlatform().getId()) {
BssPlatform bssPlatform = user.getBssPlatform();
Join<User, BssPlatform> join = root.join("bssPlatform", JoinType.INNER);
Predicate eq = cb.equal(join.get("id").as(Long.class),bssPlatform.getId());