就如前一节讲的,其它没有新知识点的内容就不再重复了。描述下业务。
业务
- 权限:权限是一个树结构的数据,每个权限对应业务一个功能。通过关联到用户后,控制用户可以或者不可以使用某功能;
- 角色:另外一个维度(前面讲了组织的维度)对用户分组的单位。对角色关联权限。相应角色的用户就关联了权限;
- 用户组织关联和用户角色关联:记录用户分组关系的数据。
数据模型
权限
package com.biboheart.huip.user.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name = "bh_user_authority")
public class Authority implements Serializable {
private static final long serialVersionUID = 8644054050270458500L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private Integer pid; // 父权限ID
private String sn; // 权限编号
private String name; // 权限名称
}
角色
package com.biboheart.huip.user.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name = "bh_user_user")
public class Role implements Serializable {
private static final long serialVersionUID = -395701688680020217L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String sn; // 角色编号
private String name; // 角色名称
private String aids; // 权限ID,用"()"分隔起来的权限ID
private Long createTime; // 创建时间
private Long updateTime; // 最后修改时间
}
用户组织
package com.biboheart.huip.user.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name = "bh_user_user_org")
public class UserOrg implements Serializable {
private static final long serialVersionUID = 2439488455909891027L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long uid; // 用户ID
private Integer oid; // 组织ID
private Integer otid; // 组织的类型ID
private String otname; // 组织类型名称
private String oname; // 组织名称
private String opath; // 组织路径,如:XX集团>XX医院>XX科室
}
用户角色
package com.biboheart.huip.user.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.Data;
@Data
@Entity
@Table(name = "bh_user_user_role")
public class UserRole implements Serializable {
private static final long serialVersionUID = -8598191284860231216L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long uid; // 用户ID
private Integer rid; // 角色ID
private String rname; // 角色名称
}
关键的文件目录
文件目录
特别说明
为了方便使用,用户服务增加一个接口,取当前用户
@Override
public User current() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if(null == authentication) {
return null;
}
String username = null;
if (!(authentication instanceof AnonymousAuthenticationToken)) {
username = authentication.getName();
}
if(CheckUtils.isEmpty(username)) {
return null;
}
Account account = accountRepository.findByUsername(username);
if (null == account || CheckUtils.isEmpty(account.getUid())) {
return null;
}
User user = userRepository.findById(account.getUid()).get();
return user;
}
组织列表增加对用户所在组织的判断
@RequestMapping(value = "/userapi/user/org/list", method = {RequestMethod.POST, RequestMethod.GET})
public BhResponseResult<?> list(String ids, String pids, String otids, String otsns, Integer descendant, Integer parents, String match) {
List<Integer> inIdList = PrimaryTransverter.idsStr2List(ids);
List<Integer> inOtidList = PrimaryTransverter.idsStr2List(otids);
if(!CheckUtils.isEmpty(otsns)) {
String[] otSnArr = otsns.split(",");
for(String otSn : otSnArr) {
OrgType ot = orgTypeService.load(null, otSn);
if(null == ot) {
continue;
}
if(null == inOtidList) {
inOtidList = new ArrayList<>();
}
if(!inOtidList.contains(ot.getId())) {
inOtidList.add(ot.getId());
}
}
}
User user = userService.current();
if (null != user) {
List<UserOrg> uos = userOrgService.list(user.getId());
List<Integer> userAllowOidList = null;
if (!CheckUtils.isEmpty(uos)) {
List<Integer> userOidList = new ArrayList<>();
for (UserOrg uo : uos) {
userOidList.add(uo.getOid());
}
userAllowOidList = orgService.listId(null, userOidList, null, 1, null);
}
if (CheckUtils.isEmpty(userAllowOidList)) {
inIdList = new ArrayList<>();
inIdList.add(0);
} else {
if (CheckUtils.isEmpty(inIdList)) {
inIdList = userAllowOidList;
} else {
// 取交集
inIdList = ListUtils.intersectionList(inIdList, userAllowOidList);
}
}
}
List<Integer> inPidList = PrimaryTransverter.idsStr2List(pids);
List<Org> orgs = orgService.list(inIdList, inPidList, inOtidList, descendant, parents, match);
return new BhResponseResult<>(0, "success", orgs);
}
总结
到此,用户系统服务端的基本功能已经完成。接下先做点前端的界面。这一系列就暂停几天。
具体代码请查看git:https://gitee.com/biboheart/huip.git
网友评论