/** * 根据ID获取用户 * * @param id * @return 取不到返回null */ public static User get(String id) { User user = (User) CacheUtils.get(USER_CACHE, USER_CACHE_ID_ + id); if (user == null) { user = userDao.selectOne(id); if (user == null) { return null; } user.setRoleList(roleDao.findList(new Role(user))); CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user); CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user); } return user; }
/** * 获取当前用户角色列表 * * @return */ public static List<Role> getRoleList() { @SuppressWarnings("unchecked") List<Role> roleList = (List<Role>) getCache(CACHE_ROLE_LIST); if (roleList == null) { User user = getUser(); if (user.isAdmin()) { roleList = roleDao.findAllList(); } else { Role role = new Role(); roleList = roleDao.findList(role); } putCache(CACHE_ROLE_LIST, roleList); } return roleList; }
/** * 获取当前用户授权菜单 * * @return */ public static List<Menu> getMenuList() { @SuppressWarnings("unchecked") List<Menu> menuList = (List<Menu>) getCache(CACHE_MENU_LIST); if (menuList == null) { User user = getUser(); if (user.isAdmin()) { menuList = menuDao.findAllList(); } else { Menu m = new Menu(); m.setUserId(user.getId()); menuList = menuDao.findByUserId(m); } putCache(CACHE_MENU_LIST, menuList); } return menuList; }
/** * 根据登录名获取用户 * * @param loginName * @return 取不到返回null */ public static User getByLoginName(String loginName) { User user = (User) CacheUtils.get(USER_CACHE, USER_CACHE_LOGIN_NAME_ + loginName); if (user == null) { User queryUser = new User(); queryUser.setLoginName(loginName); ; user = userDao.getByLoginName(queryUser); if (user == null) { return null; } user.setRoleList(roleDao.findList(new Role(user))); CacheUtils.put(USER_CACHE, USER_CACHE_ID_ + user.getId(), user); CacheUtils.put(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user); } return user; }
/** * 清除指定用户缓存 * * @param user */ public static void clearCache(User user) { CacheUtils.remove(USER_CACHE, USER_CACHE_ID_ + user.getId()); CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getLoginName()); CacheUtils.remove(USER_CACHE, USER_CACHE_LOGIN_NAME_ + user.getOldLoginName()); }