新增超级管理员租户切换功能

This commit is contained in:
thinkgem
2018-09-28 22:25:38 +08:00
parent bc66796ca7
commit 968e5fe357
5 changed files with 97 additions and 12 deletions

View File

@@ -106,13 +106,15 @@ public class OnlineController extends BaseController{
map.put("startTimestamp", DateUtils.formatDateTime(session.getStartTimestamp()));
map.put("lastAccessTime", DateUtils.formatDateTime(session.getLastAccessTime()));
map.put("timeout", TimeUtils.formatDateAgo(session.getTimeout()-(currentTime-session.getLastAccessTime().getTime())));
PrincipalCollection pc = (PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
LoginInfo principal = (pc != null ? (LoginInfo)pc.getPrimaryPrincipal() : null);
if (principal != null){
map.put("userCode", session.getAttribute("userCode"));// principal.getId());
map.put("userName", session.getAttribute("userName"));// principal.getName());
map.put("userType", session.getAttribute("userType"));// ObjectUtils.toString(principal.getParam("userType")));
map.put("deviceType", ObjectUtils.toString(principal.getParam("deviceType")));
Object pc = session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
if (pc != null && pc instanceof PrincipalCollection){
LoginInfo loginInfo = (LoginInfo)((PrincipalCollection)pc).getPrimaryPrincipal();
if (loginInfo != null){
map.put("userCode", session.getAttribute("userCode"));// principal.getId());
map.put("userName", session.getAttribute("userName"));// principal.getName());
map.put("userType", session.getAttribute("userType"));// ObjectUtils.toString(principal.getParam("userType")));
map.put("deviceType", ObjectUtils.toString(loginInfo.getParam("deviceType")));
}
}
map.put("host", session.getHost());
list.add(map);

View File

@@ -4,21 +4,26 @@
package com.jeesite.modules.sys.web.user;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeesite.common.collect.ListUtils;
import com.jeesite.common.collect.MapUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
@@ -216,5 +221,55 @@ public class CorpAdminController extends BaseController {
return renderResult(Global.TRUE, "取消用户'" + user.getUserName() + "'管理员身份成功!");
}
}
/**
* 查询租户数据树格式
* @param pId 父级编码,默认 -1
* @param isShowCode 是否显示编码true or 1显示在左侧2显示在右侧false or null不显示
* @return
*/
@RequiresPermissions("user")
@RequestMapping(value = "treeData")
@ResponseBody
public List<Map<String, Object>> treeData(String pId, String isShowCode) {
List<Map<String, Object>> mapList = ListUtils.newArrayList();
User where = new User();
List<User> list = userService.findCorpList(where);
for (int i = 0; i < list.size(); i++) {
User e = list.get(i);
Map<String, Object> map = MapUtils.newHashMap();
map.put("id", e.getCorpCode_());
map.put("pId", StringUtils.defaultIfBlank(pId, "-1"));
map.put("name", StringUtils.getTreeNodeName(isShowCode, e.getCorpCode_(), e.getCorpName_()));
mapList.add(map);
}
return mapList;
}
/**
* 切换租户
* @param user
* @return
*/
@RequiresPermissions("sys:corpAdmin:edit")
@RequestMapping(value = "switch/{corpCode}")
@ResponseBody
public String switchCorp(@PathVariable String corpCode) {
if (UserUtils.getUser().isSuperAdmin()){
User where = new User();
where.setCorpCode_(corpCode);
List<User> list = userService.findCorpList(where);
if (list.size() > 0){
Session session = UserUtils.getSession();
User user = list.get(0);
session.setAttribute("corpCode", user.getCorpCode_());
session.setAttribute("corpName", user.getCorpName_());
return renderResult(Global.TRUE, "租户切换成功!");
}else{
return renderResult(Global.TRUE, "租户切换失败,没有这个租户!");
}
}
return renderResult(Global.FALSE, "租户切换失败,只有超级管理员才可以操作!");
}
}

View File

@@ -0,0 +1,28 @@
<% if(@Global.getConfigToBoolean('user.useCorpModel', 'false') && user.superAdmin){ %>
<li>
<a href="javascript:" id="switchCorp">
<i class="fa icon-home"></i> 当前租户:(${session.corpCode}) ${session.corpName}
</a>
<div class="hide"><#form:treeselect id="switchCorpSelect" title="${text('租户切换')}" allowClear="false"
url="${ctx}/sys/corpAdmin/treeData?isShowCode=true" callbackFuncName="switchCorpSelectCallback"/>
</div>
<script>
$('#switchCorp').click(function(){
$('#switchCorpSelectButton').click();
});
function switchCorpSelectCallback(id, act, index, layero, nodes){
if (act == 'ok'){
var corpCode = $('#switchCorpSelectCode').val();
if (corpCode != ''){
js.ajaxSubmit("${ctx}/sys/corpAdmin/switch/"+corpCode, function(data){
js.showMessage(data.message);
top.location.reload();
});
}
}
}
</script>
</li>
<% }else if(__info_type == '0'){ %>
<li><a href="http://jeesite.com" target="_blank"><i class="fa fa-diamond"></i> 官方网站</a></li>
<% } %>

View File

@@ -13,7 +13,7 @@ jdbc:
# Mysql 数据库配置
type: mysql
driver: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/jeesite?useSSL=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
url: jdbc:mysql://127.0.0.1:3306/jeesite?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
testSql: SELECT 1