Commit bf194930 by fengshuonan

更新shiroUser的初始化过程

parent 0fb05506
...@@ -17,6 +17,7 @@ package cn.stylefeng.guns.core.shiro; ...@@ -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.Const;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory; 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 cn.stylefeng.roses.core.util.ToolUtil;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.crypto.hash.Md5Hash;
...@@ -279,4 +280,25 @@ public class ShiroKit { ...@@ -279,4 +280,25 @@ public class ShiroKit {
return false; 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 @@ ...@@ -15,6 +15,9 @@
*/ */
package cn.stylefeng.guns.core.shiro; package cn.stylefeng.guns.core.shiro;
import cn.stylefeng.guns.core.common.node.MenuNode;
import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
...@@ -24,73 +27,60 @@ import java.util.List; ...@@ -24,73 +27,60 @@ import java.util.List;
* @author fengshuonan * @author fengshuonan
* @date 2016年12月5日 上午10:26:43 * @date 2016年12月5日 上午10:26:43
*/ */
@Data
public class ShiroUser implements Serializable { public class ShiroUser implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public Integer id; // 主键ID /**
public String account; // 账号 * 用户主键ID
public String name; // 姓名 */
public Integer deptId; // 部门id private Integer 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;
}
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; ...@@ -18,11 +18,13 @@ package cn.stylefeng.guns.core.shiro.service.impl;
import cn.hutool.core.convert.Convert; import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory; import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.core.common.constant.state.ManagerStatus; 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.ShiroUser;
import cn.stylefeng.guns.core.shiro.service.UserAuthService; import cn.stylefeng.guns.core.shiro.service.UserAuthService;
import cn.stylefeng.guns.modular.system.dao.MenuMapper; import cn.stylefeng.guns.modular.system.dao.MenuMapper;
import cn.stylefeng.guns.modular.system.dao.UserMapper; import cn.stylefeng.guns.modular.system.dao.UserMapper;
import cn.stylefeng.guns.modular.system.model.User; import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.core.util.SpringContextHolder; import cn.stylefeng.roses.core.util.SpringContextHolder;
import org.apache.shiro.authc.CredentialsException; import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException; import org.apache.shiro.authc.LockedAccountException;
...@@ -48,6 +50,9 @@ public class UserAuthServiceServiceImpl implements UserAuthService { ...@@ -48,6 +50,9 @@ public class UserAuthServiceServiceImpl implements UserAuthService {
@Autowired @Autowired
private MenuMapper menuMapper; private MenuMapper menuMapper;
@Autowired
private IUserService userService;
public static UserAuthService me() { public static UserAuthService me() {
return SpringContextHolder.getBean(UserAuthService.class); return SpringContextHolder.getBean(UserAuthService.class);
} }
...@@ -70,17 +75,15 @@ public class UserAuthServiceServiceImpl implements UserAuthService { ...@@ -70,17 +75,15 @@ public class UserAuthServiceServiceImpl implements UserAuthService {
@Override @Override
public ShiroUser shiroUser(User user) { public ShiroUser shiroUser(User user) {
ShiroUser shiroUser = new ShiroUser();
shiroUser.setId(user.getId()); ShiroUser shiroUser = ShiroKit.createShiroUser(user);
shiroUser.setAccount(user.getAccount());
shiroUser.setDeptId(user.getDeptid());
shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));
shiroUser.setName(user.getName());
//用户角色数组
Integer[] roleArray = Convert.toIntArray(user.getRoleid()); 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) { for (int roleId : roleArray) {
roleList.add(roleId); roleList.add(roleId);
roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId)); roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
...@@ -88,6 +91,9 @@ public class UserAuthServiceServiceImpl implements UserAuthService { ...@@ -88,6 +91,9 @@ public class UserAuthServiceServiceImpl implements UserAuthService {
shiroUser.setRoleList(roleList); shiroUser.setRoleList(roleList);
shiroUser.setRoleNames(roleNameList); shiroUser.setRoleNames(roleNameList);
//获取用户拥有的菜单
shiroUser.setMenus(userService.getUserMenuNodes(roleList));
return shiroUser; return shiroUser;
} }
......
...@@ -17,6 +17,7 @@ package cn.stylefeng.guns.modular.system.controller; ...@@ -17,6 +17,7 @@ package cn.stylefeng.guns.modular.system.controller;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.core.common.node.MenuNode; 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.core.util.ApiMenuFilter;
import cn.stylefeng.guns.modular.system.service.IMenuService; import cn.stylefeng.guns.modular.system.service.IMenuService;
import cn.stylefeng.guns.modular.system.service.INoticeService; import cn.stylefeng.guns.modular.system.service.INoticeService;
...@@ -62,7 +63,7 @@ public class DashboardController extends BaseController { ...@@ -62,7 +63,7 @@ public class DashboardController extends BaseController {
//获取用户头像 //获取用户头像
model.addAttribute("name", "stylefeng"); 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"); model.addAttribute("email", "sn93@qq.com");
return "/dashboard.html"; return "/dashboard.html";
......
...@@ -126,9 +126,6 @@ public class LoginController extends BaseController { ...@@ -126,9 +126,6 @@ public class LoginController extends BaseController {
currentUser.login(token); currentUser.login(token);
ShiroUser shiroUser = ShiroKit.getUser(); ShiroUser shiroUser = ShiroKit.getUser();
super.getSession().setAttribute("shiroUser", shiroUser);
super.getSession().setAttribute("username", shiroUser.getAccount());
LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp())); LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));
ShiroKit.getSession().setAttribute("sessionFlag", true); ShiroKit.getSession().setAttribute("sessionFlag", true);
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package cn.stylefeng.guns.modular.system.service; 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.guns.modular.system.model.User;
import cn.stylefeng.roses.core.datascope.DataScope; import cn.stylefeng.roses.core.datascope.DataScope;
import com.baomidou.mybatisplus.service.IService; import com.baomidou.mybatisplus.service.IService;
...@@ -57,4 +58,9 @@ public interface IUserService extends IService<User> { ...@@ -57,4 +58,9 @@ public interface IUserService extends IService<User> {
*/ */
User getByAccount(String account); User getByAccount(String account);
/**
* 获取用户菜单列表
*/
List<MenuNode> getUserMenuNodes(List<Integer> roleList);
} }
...@@ -15,13 +15,18 @@ ...@@ -15,13 +15,18 @@
*/ */
package cn.stylefeng.guns.modular.system.service.impl; 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.dao.UserMapper;
import cn.stylefeng.guns.modular.system.model.User; 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.guns.modular.system.service.IUserService;
import cn.stylefeng.roses.core.datascope.DataScope; import cn.stylefeng.roses.core.datascope.DataScope;
import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -36,6 +41,9 @@ import java.util.Map; ...@@ -36,6 +41,9 @@ import java.util.Map;
@Service @Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Autowired
private IMenuService menuService;
@Override @Override
public int setStatus(Integer userId, int status) { public int setStatus(Integer userId, int status) {
return this.baseMapper.setStatus(userId, status); return this.baseMapper.setStatus(userId, status);
...@@ -60,4 +68,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU ...@@ -60,4 +68,16 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
public User getByAccount(String account) { public User getByAccount(String account) {
return this.baseMapper.getByAccount(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