Commit bf194930 by fengshuonan

更新shiroUser的初始化过程

parent 0fb05506
......@@ -17,6 +17,7 @@ package cn.stylefeng.guns.core.shiro;
import cn.stylefeng.guns.core.common.constant.Const;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
......@@ -279,4 +280,25 @@ public class ShiroKit {
return false;
}
/**
* 通过用户表的信息创建一个shiroUser对象
*/
public static ShiroUser createShiroUser(User user) {
ShiroUser shiroUser = new ShiroUser();
if (user == null) {
return shiroUser;
}
shiroUser.setId(user.getId());
shiroUser.setAccount(user.getAccount());
shiroUser.setDeptId(user.getDeptid());
shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));
shiroUser.setName(user.getName());
shiroUser.setEmail(user.getEmail());
shiroUser.setAvatar(user.getAvatar());
return shiroUser;
}
}
......@@ -15,6 +15,9 @@
*/
package cn.stylefeng.guns.core.shiro;
import cn.stylefeng.guns.core.common.node.MenuNode;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
......@@ -24,73 +27,60 @@ import java.util.List;
* @author fengshuonan
* @date 2016年12月5日 上午10:26:43
*/
@Data
public class ShiroUser implements Serializable {
private static final long serialVersionUID = 1L;
public Integer id; // 主键ID
public String account; // 账号
public String name; // 姓名
public Integer deptId; // 部门id
public List<Integer> roleList; // 角色集
public String deptName; // 部门名称
public List<String> roleNames; // 角色名称集
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
/**
* 用户主键ID
*/
private Integer id;
public String getName() {
return name;
}
/**
* 账号
*/
private String account;
public void setName(String name) {
this.name = name;
}
/**
* 姓名
*/
private String name;
public Integer getDeptId() {
return deptId;
}
/**
* 邮箱
*/
private String email;
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
/**
* 头像
*/
private String avatar;
public List<Integer> getRoleList() {
return roleList;
}
/**
* 部门id
*/
private Integer deptId;
public void setRoleList(List<Integer> roleList) {
this.roleList = roleList;
}
/**
* 角色集
*/
private List<Integer> roleList;
public String getDeptName() {
return deptName;
}
/**
* 部门名称
*/
private String deptName;
public void setDeptName(String deptName) {
this.deptName = deptName;
}
/**
* 角色名称集
*/
private List<String> roleNames;
public List<String> getRoleNames() {
return roleNames;
}
/**
* 用户所能看到的菜单列表
*/
private List<MenuNode> menus;
public void setRoleNames(List<String> roleNames) {
this.roleNames = roleNames;
}
}
......@@ -18,11 +18,13 @@ package cn.stylefeng.guns.core.shiro.service.impl;
import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.constant.state.ManagerStatus;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.core.shiro.ShiroUser;
import cn.stylefeng.guns.core.shiro.service.UserAuthService;
import cn.stylefeng.guns.modular.system.dao.MenuMapper;
import cn.stylefeng.guns.modular.system.dao.UserMapper;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
......@@ -48,6 +50,9 @@ public class UserAuthServiceServiceImpl implements UserAuthService {
@Autowired
private MenuMapper menuMapper;
@Autowired
private IUserService userService;
public static UserAuthService me() {
return SpringContextHolder.getBean(UserAuthService.class);
}
......@@ -70,17 +75,15 @@ public class UserAuthServiceServiceImpl implements UserAuthService {
@Override
public ShiroUser shiroUser(User user) {
ShiroUser shiroUser = new ShiroUser();
shiroUser.setId(user.getId());
shiroUser.setAccount(user.getAccount());
shiroUser.setDeptId(user.getDeptid());
shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));
shiroUser.setName(user.getName());
ShiroUser shiroUser = ShiroKit.createShiroUser(user);
//用户角色数组
Integer[] roleArray = Convert.toIntArray(user.getRoleid());
List<Integer> roleList = new ArrayList<Integer>();
List<String> roleNameList = new ArrayList<String>();
//获取用户角色列表
List<Integer> roleList = new ArrayList<>();
List<String> roleNameList = new ArrayList<>();
for (int roleId : roleArray) {
roleList.add(roleId);
roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
......@@ -88,6 +91,9 @@ public class UserAuthServiceServiceImpl implements UserAuthService {
shiroUser.setRoleList(roleList);
shiroUser.setRoleNames(roleNameList);
//获取用户拥有的菜单
shiroUser.setMenus(userService.getUserMenuNodes(roleList));
return shiroUser;
}
......
......@@ -17,6 +17,7 @@ package cn.stylefeng.guns.modular.system.controller;
import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.core.common.node.MenuNode;
import cn.stylefeng.guns.core.listener.ConfigListener;
import cn.stylefeng.guns.core.util.ApiMenuFilter;
import cn.stylefeng.guns.modular.system.service.IMenuService;
import cn.stylefeng.guns.modular.system.service.INoticeService;
......@@ -62,7 +63,7 @@ public class DashboardController extends BaseController {
//获取用户头像
model.addAttribute("name", "stylefeng");
model.addAttribute("avatar", "/assets/images/users/1.jpg");
model.addAttribute("avatar", ConfigListener.getConf().get("contextPath") + "/assets/images/users/1.jpg");
model.addAttribute("email", "sn93@qq.com");
return "/dashboard.html";
......
......@@ -126,9 +126,6 @@ public class LoginController extends BaseController {
currentUser.login(token);
ShiroUser shiroUser = ShiroKit.getUser();
super.getSession().setAttribute("shiroUser", shiroUser);
super.getSession().setAttribute("username", shiroUser.getAccount());
LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));
ShiroKit.getSession().setAttribute("sessionFlag", true);
......
......@@ -15,6 +15,7 @@
*/
package cn.stylefeng.guns.modular.system.service;
import cn.stylefeng.guns.core.common.node.MenuNode;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.roses.core.datascope.DataScope;
import com.baomidou.mybatisplus.service.IService;
......@@ -57,4 +58,9 @@ public interface IUserService extends IService<User> {
*/
User getByAccount(String account);
/**
* 获取用户菜单列表
*/
List<MenuNode> getUserMenuNodes(List<Integer> roleList);
}
......@@ -15,13 +15,18 @@
*/
package cn.stylefeng.guns.modular.system.service.impl;
import cn.stylefeng.guns.core.common.node.MenuNode;
import cn.stylefeng.guns.core.util.ApiMenuFilter;
import cn.stylefeng.guns.modular.system.dao.UserMapper;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.service.IMenuService;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.core.datascope.DataScope;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
......@@ -36,6 +41,9 @@ import java.util.Map;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private IMenuService menuService;
@Override
public int setStatus(Integer userId, int status) {
return this.baseMapper.setStatus(userId, status);
......@@ -60,4 +68,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
public User getByAccount(String account) {
return this.baseMapper.getByAccount(account);
}
@Override
public List<MenuNode> getUserMenuNodes(List<Integer> roleList) {
if (roleList == null || roleList.size() == 0) {
return new ArrayList<>();
} else {
List<MenuNode> menus = menuService.getMenusByRoleIds(roleList);
List<MenuNode> titles = MenuNode.buildTitle(menus);
return ApiMenuFilter.build(titles);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment