Commit 839129bb141e74de871138cf869d5b62fa19d095

Authored by lizhuojun
1 parent 0eafd332

角色管理页面添加系统资源配置

src/main/java/com/bsth/controller/sys/ResourceController.java
1 package com.bsth.controller.sys; 1 package com.bsth.controller.sys;
2 2
  3 +import java.util.List;
3 import java.util.Map; 4 import java.util.Map;
4 5
  6 +import com.bsth.security.SecurityMetadataSourceService;
5 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMapping;
7 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RequestMethod;
@@ -19,9 +21,22 @@ public class ResourceController extends BaseController<Resource, Integer>{ @@ -19,9 +21,22 @@ public class ResourceController extends BaseController<Resource, Integer>{
19 21
20 @Autowired 22 @Autowired
21 ResourceService resourceService; 23 ResourceService resourceService;
  24 +
  25 + @Autowired
  26 + SecurityMetadataSourceService securityMetadataSourceService;
22 27
23 @RequestMapping(value = "/batch", method = RequestMethod.POST) 28 @RequestMapping(value = "/batch", method = RequestMethod.POST)
24 public Map<String, Object> save(@RequestParam String array){ 29 public Map<String, Object> save(@RequestParam String array){
25 return resourceService.saveList(JSON.parseArray(array, Resource.class)); 30 return resourceService.saveList(JSON.parseArray(array, Resource.class));
26 } 31 }
  32 +
  33 + /***
  34 + * 查询所有资源信息,如果当前角色id拥有该资源就将原Resource实体类中的enable改为true否则为false
  35 + * @param roleId
  36 + * @return
  37 + */
  38 + @RequestMapping(value = "/findResource",method = RequestMethod.GET)
  39 + public List<Resource> findResource(Integer roleId){
  40 + return resourceService.findResource(roleId);
  41 + }
27 } 42 }
src/main/java/com/bsth/controller/sys/RoleController.java
@@ -44,4 +44,17 @@ public class RoleController extends BaseController&lt;Role, Integer&gt;{ @@ -44,4 +44,17 @@ public class RoleController extends BaseController&lt;Role, Integer&gt;{
44 public Map<String, Object> roleInfo(@RequestParam Integer id){ 44 public Map<String, Object> roleInfo(@RequestParam Integer id){
45 return roleService.roleInfo(id); 45 return roleService.roleInfo(id);
46 } 46 }
  47 +
  48 + /**
  49 + *
  50 + * @Title: settRoleModules
  51 + * @Description: TODO(为角色设置资源)
  52 + * @param @param roleId 角色ID
  53 + * @param @param mIds 模块ID字符串(1,2,3,4)
  54 + * @throws
  55 + */
  56 + @RequestMapping(value = "/settResources",method = RequestMethod.POST)
  57 + public Map<String, Object> settResources(@RequestParam Integer roleId, @RequestParam String rIds){
  58 + return roleService.settRoleResources(roleId,rIds);
  59 + }
47 } 60 }
src/main/java/com/bsth/repository/sys/ResourceRepository.java
1 package com.bsth.repository.sys; 1 package com.bsth.repository.sys;
2 2
3 import java.util.List; 3 import java.util.List;
  4 +import java.util.Set;
4 5
  6 +import org.springframework.data.jpa.domain.Specification;
  7 +import org.springframework.data.jpa.repository.Modifying;
  8 +import org.springframework.data.jpa.repository.Query;
  9 +import org.springframework.data.repository.query.Param;
5 import org.springframework.stereotype.Repository; 10 import org.springframework.stereotype.Repository;
6 11
7 import com.bsth.entity.sys.Resource; 12 import com.bsth.entity.sys.Resource;
8 import com.bsth.repository.BaseRepository; 13 import com.bsth.repository.BaseRepository;
  14 +import org.springframework.transaction.annotation.Propagation;
  15 +import org.springframework.transaction.annotation.Transactional;
9 16
10 @Repository 17 @Repository
11 public interface ResourceRepository extends BaseRepository<Resource, Integer> { 18 public interface ResourceRepository extends BaseRepository<Resource, Integer> {
12 19
13 - List<Resource> findByRolesId(Integer roleId); 20 + List<Resource> findByRolesId(Integer roleId);
  21 +
  22 + /***
  23 + * 查询所有资源信息,如果当前角色id拥有该资源就将原Resource实体类中的enable改为true否则为false
  24 + * @param roleId 角色id
  25 + * @return
  26 + */
  27 + @Query(value = "select a.id ,a.`name`,a.create_date,a.descriptions,a.method,a.module,a.update_date,a.url,if(b.resources is null,0,1) enable from bsth_c_sys_resource a\n" +
  28 + "left join \n" +
  29 + "(select resources from bsth_c_sys_resource_roles where roles = ?1) b\n" +
  30 + "on a.id = b.resources",nativeQuery = true)
  31 + List<Resource> findResource(int roleId);
14 } 32 }
src/main/java/com/bsth/service/sys/ResourceService.java
@@ -12,4 +12,6 @@ public interface ResourceService extends BaseService&lt;Resource, Integer&gt; { @@ -12,4 +12,6 @@ public interface ResourceService extends BaseService&lt;Resource, Integer&gt; {
12 12
13 List<Resource> findByRolesId(Integer id); 13 List<Resource> findByRolesId(Integer id);
14 14
  15 + List<Resource> findResource(Integer roleId);
  16 +
15 } 17 }
src/main/java/com/bsth/service/sys/RoleService.java
@@ -10,4 +10,6 @@ public interface RoleService extends BaseService&lt;Role, Integer&gt;{ @@ -10,4 +10,6 @@ public interface RoleService extends BaseService&lt;Role, Integer&gt;{
10 Map<String, Object> settRoleModules(Integer roleId, String mIds); 10 Map<String, Object> settRoleModules(Integer roleId, String mIds);
11 11
12 Map<String, Object> roleInfo(Integer id); 12 Map<String, Object> roleInfo(Integer id);
  13 +
  14 + Map<String, Object> settRoleResources(Integer roleId, String mIds);
13 } 15 }
src/main/java/com/bsth/service/sys/impl/ResourceServiceImpl.java
1 package com.bsth.service.sys.impl; 1 package com.bsth.service.sys.impl;
2 2
  3 +import java.util.ArrayList;
3 import java.util.HashMap; 4 import java.util.HashMap;
4 import java.util.List; 5 import java.util.List;
5 import java.util.Map; 6 import java.util.Map;
6 7
  8 +import com.bsth.entity.sys.SysUser;
  9 +import com.bsth.security.util.SecurityUtils;
7 import org.slf4j.Logger; 10 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory; 11 import org.slf4j.LoggerFactory;
9 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
@@ -40,4 +43,15 @@ public class ResourceServiceImpl extends BaseServiceImpl&lt;Resource, Integer&gt; impl @@ -40,4 +43,15 @@ public class ResourceServiceImpl extends BaseServiceImpl&lt;Resource, Integer&gt; impl
40 public List<Resource> findByRolesId(Integer id) { 43 public List<Resource> findByRolesId(Integer id) {
41 return resourceRepository.findByRolesId(id); 44 return resourceRepository.findByRolesId(id);
42 } 45 }
  46 +
  47 + @Override
  48 + public List<Resource> findResource(Integer roleId) {
  49 + List<Resource> list = new ArrayList<>();
  50 + try{
  51 + list = resourceRepository.findResource(roleId);
  52 + }catch (Exception e){
  53 + logger.error("", e);
  54 + }
  55 + return list;
  56 + }
43 } 57 }
src/main/java/com/bsth/service/sys/impl/RoleServiceImpl.java
1 package com.bsth.service.sys.impl; 1 package com.bsth.service.sys.impl;
2 2
  3 +import java.sql.PreparedStatement;
  4 +import java.sql.SQLException;
3 import java.text.SimpleDateFormat; 5 import java.text.SimpleDateFormat;
4 -import java.util.ArrayList;  
5 -import java.util.HashMap;  
6 -import java.util.Iterator;  
7 -import java.util.List;  
8 -import java.util.Map;  
9 -import java.util.Set; 6 +import java.util.*;
10 7
  8 +import com.bsth.data.BasicData;
  9 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  10 +import com.bsth.entity.sys.Resource;
  11 +import com.bsth.repository.sys.ResourceRepository;
  12 +import com.bsth.security.SecurityMetadataSourceService;
11 import org.slf4j.Logger; 13 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory; 14 import org.slf4j.LoggerFactory;
13 import org.springframework.beans.factory.annotation.Autowired; 15 import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.jdbc.core.BatchPreparedStatementSetter;
  17 +import org.springframework.jdbc.core.JdbcTemplate;
  18 +import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  19 +import org.springframework.orm.jpa.JpaTransactionManager;
14 import org.springframework.stereotype.Service; 20 import org.springframework.stereotype.Service;
15 21
16 import com.bsth.common.ResponseCode; 22 import com.bsth.common.ResponseCode;
@@ -21,6 +27,11 @@ import com.bsth.repository.sys.ModuleRepository; @@ -21,6 +27,11 @@ import com.bsth.repository.sys.ModuleRepository;
21 import com.bsth.repository.sys.RoleRepository; 27 import com.bsth.repository.sys.RoleRepository;
22 import com.bsth.service.impl.BaseServiceImpl; 28 import com.bsth.service.impl.BaseServiceImpl;
23 import com.bsth.service.sys.RoleService; 29 import com.bsth.service.sys.RoleService;
  30 +import org.springframework.transaction.TransactionDefinition;
  31 +import org.springframework.transaction.TransactionStatus;
  32 +import org.springframework.transaction.annotation.Propagation;
  33 +import org.springframework.transaction.annotation.Transactional;
  34 +import org.springframework.transaction.support.DefaultTransactionDefinition;
24 35
25 @Service 36 @Service
26 public class RoleServiceImpl extends BaseServiceImpl<Role, Integer> implements 37 public class RoleServiceImpl extends BaseServiceImpl<Role, Integer> implements
@@ -33,6 +44,15 @@ public class RoleServiceImpl extends BaseServiceImpl&lt;Role, Integer&gt; implements @@ -33,6 +44,15 @@ public class RoleServiceImpl extends BaseServiceImpl&lt;Role, Integer&gt; implements
33 44
34 @Autowired 45 @Autowired
35 ModuleRepository moduleRepository; 46 ModuleRepository moduleRepository;
  47 +
  48 + @Autowired
  49 + ResourceRepository resourceRepository;
  50 +
  51 + @Autowired
  52 + SecurityMetadataSourceService securityMetadataSourceService;
  53 +
  54 + @Autowired
  55 + JdbcTemplate jdbcTemplate;
36 56
37 SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm"); 57 SimpleDateFormat sdfMinute = new SimpleDateFormat("yyyy-MM-dd HH:mm");
38 58
@@ -103,4 +123,39 @@ public class RoleServiceImpl extends BaseServiceImpl&lt;Role, Integer&gt; implements @@ -103,4 +123,39 @@ public class RoleServiceImpl extends BaseServiceImpl&lt;Role, Integer&gt; implements
103 map.put("userNames", userNames); 123 map.put("userNames", userNames);
104 return map; 124 return map;
105 } 125 }
106 -} 126 +
  127 + @Override
  128 + public Map<String, Object> settRoleResources(Integer roleId, String rIds){
  129 + Map<String, Object> map = new HashMap<>();
  130 + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
  131 + DefaultTransactionDefinition def = new DefaultTransactionDefinition();
  132 + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
  133 + TransactionStatus status = tran.getTransaction(def);
  134 + String[] ids = rIds.split(",");
  135 + try{
  136 + jdbcTemplate.update("delete from bsth_c_sys_resource_roles where roles = ?", roleId);
  137 + jdbcTemplate.batchUpdate("insert into bsth_c_sys_resource_roles(resources,roles)" +
  138 + " VALUES (?, ?)", new BatchPreparedStatementSetter() {
  139 + @Override
  140 + public void setValues(PreparedStatement ps, int i) throws SQLException {
  141 + ps.setInt(1, Integer.parseInt(ids[i]));
  142 + ps.setInt(2, roleId);
  143 + }
  144 +
  145 + @Override
  146 + public int getBatchSize() {
  147 + return ids.length;
  148 + }
  149 + });
  150 + tran.commit(status);
  151 + //重新加载security资源
  152 + securityMetadataSourceService.loadResourceDefine();
  153 + map.put("status", ResponseCode.SUCCESS);
  154 + }catch (Exception e){
  155 + tran.rollback(status);
  156 + logger.error("【RoleServiceImpl】【settRoleResources】 : ", e);
  157 + map.put("status", ResponseCode.ERROR);
  158 + }
  159 + return map;
  160 + }
  161 +}
107 \ No newline at end of file 162 \ No newline at end of file
src/main/resources/static/assets/js/common.js
@@ -57,7 +57,10 @@ function ajaxComplete(xhr, ts, succ){ @@ -57,7 +57,10 @@ function ajaxComplete(xhr, ts, succ){
57 successHandle(JSON.parse(xhr.responseText), succ); 57 successHandle(JSON.parse(xhr.responseText), succ);
58 } 58 }
59 else if(ts == 'error'){ 59 else if(ts == 'error'){
60 - layer.alert(xhr.responseText, {icon: 2, title: '操作失败'}); 60 + layer.alert(xhr.responseText + '<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span id="goIndex" style="color: #ff1f08;font-weight: bold;font-size: large;">1</span> 秒后回到首页', {icon: 2, title: '操作失败'});
  61 + setTimeout(function(){
  62 + window.location.href = '/';
  63 + },1500)
61 } 64 }
62 } 65 }
63 66
src/main/resources/static/pages/permission/resource/list.html
@@ -165,16 +165,17 @@ $(function(){ @@ -165,16 +165,17 @@ $(function(){
165 var options = '<option value="">请选择...</option>'; 165 var options = '<option value="">请选择...</option>';
166 $.each(treeData, function(i, g){ 166 $.each(treeData, function(i, g){
167 var dArray = g.children; 167 var dArray = g.children;
168 -  
169 - for(var i = 0,d; d = dArray[i++];){  
170 - options += '<optgroup label="'+d.name+'">';  
171 - if(!d.children)  
172 - continue;  
173 -  
174 - $.each(d.children, function(i, m){  
175 - options += '<option value="'+m.id+'">'+m.name+'</option>'  
176 - });  
177 - options += '</optgroup>'; 168 + if(dArray){
  169 + for(var i = 0,d; d = dArray[i++];){
  170 + options += '<optgroup label="'+d.name+'">';
  171 + if(!d.children)
  172 + continue;
  173 +
  174 + $.each(d.children, function(i, m){
  175 + options += '<option value="'+m.id+'">'+m.name+'</option>'
  176 + });
  177 + options += '</optgroup>';
  178 + }
178 } 179 }
179 }); 180 });
180 $('#moduleSelect').html(options)/* .select2() */; 181 $('#moduleSelect').html(options)/* .select2() */;
src/main/resources/static/pages/permission/role/list.html
@@ -67,7 +67,7 @@ @@ -67,7 +67,7 @@
67 style="display: inline-block; margin-right: 5px;"> <i 67 style="display: inline-block; margin-right: 5px;"> <i
68 class="fa fa-meh-o"> </i> 模块配置 68 class="fa fa-meh-o"> </i> 模块配置
69 </a> 69 </a>
70 - <a href="javascript:;" class=" font-blue " 70 + <a href="resourcesSetting.html?no={{role.id}}" class=" font-blue "
71 style="display: inline-block;color: #aaaaaa !important;" > <i class="fa fa-key"> 71 style="display: inline-block;color: #aaaaaa !important;" > <i class="fa fa-key">
72 </i> 系统资源权限 72 </i> 系统资源权限
73 </a> 73 </a>
src/main/resources/static/pages/permission/role/resourcesSetting.html 0 → 100644
  1 +<div class="page-head">
  2 + <div class="page-title">
  3 + <h1>分配资源</h1>
  4 + </div>
  5 +</div>
  6 +
  7 +<ul class="page-breadcrumb breadcrumb">
  8 + <li><a href="/pages/home.html" data-pjax>首页</a> <i
  9 + class="fa fa-circle"></i></li>
  10 + <li><span class="active">权限管理</span> <i class="fa fa-circle"></i></li>
  11 + <li><a href="list.html" data-pjax>角色管理</a> <i class="fa fa-circle"></i></li>
  12 + <li><span class="active">分配资源</span></li>
  13 +</ul>
  14 +<br><br>
  15 +<div class="row">
  16 + <div class="col-lg-4 col-md-5 col-sm-5 col-md-offset-1">
  17 + <!-- BEGIN PORTLET-->
  18 + <div class="portlet light bordered">
  19 + <div class="portlet-title">
  20 + <div class="caption">
  21 + <i class="icon-bar-chart font-green"></i>
  22 + <span class="caption-subject font-green bold uppercase">角色信息</span>
  23 + <span class="caption-helper">更新于 2016-03-29 16:40</span>
  24 + </div>
  25 + </div>
  26 + <div class="portlet-body">
  27 + <div class="mt-element-list">
  28 + <div class="mt-list-container list-simple" style="border: none;">
  29 + </div>
  30 + </div>
  31 + </div>
  32 + </div>
  33 + <!-- END PORTLET-->
  34 + </div>
  35 + <script id="role_detail_temp" type="text/html">
  36 + <ul>
  37 + <li class="mt-list-item" style="border-bottom:none;">
  38 + <div class="list-item-content" >
  39 + <h5 class="uppercase">
  40 + <span><i class="fa fa-code"></i> 角色代码:{{codeName}}</span>
  41 + </h5>
  42 + </div>
  43 + </li>
  44 + <li class="mt-list-item" style="border-bottom:none;">
  45 + <div class="list-item-content">
  46 + <h5 class="uppercase">
  47 + <span><i class="fa fa-user"></i> 角色名称:{{roleName}}</span>
  48 + </h5>
  49 + </div>
  50 + </li>
  51 + <li class="mt-list-item" style="border-bottom:none;">
  52 + <div class="list-item-content">
  53 + <h5 class="uppercase">
  54 + <span><i class="fa fa-clock-o"></i> 创建时间:{{createDate}}</span>
  55 + </h5>
  56 + </div>
  57 + </li>
  58 + <li class="mt-list-item" style="border-bottom:none;">
  59 + <div class="list-item-content">
  60 + <h5 class="uppercase">
  61 + <span><i class="fa fa-check-circle"></i> 状态:
  62 + {{if enable == 1}}
  63 + 可用
  64 + {{else}}
  65 + 禁用
  66 + {{/if}}
  67 + </span>
  68 + </h5>
  69 + </div>
  70 + </li>
  71 + <li class="mt-list-item" style="border-bottom:none;">
  72 + <div class="list-item-content">
  73 + <h5 class="uppercase">
  74 + <span><i class="fa fa-columns"></i> 模块数:{{modules}}</span>
  75 + </h5>
  76 + </div>
  77 + </li>
  78 + <li class="mt-list-item" style="border-bottom:none;">
  79 + <div class="list-item-content">
  80 + <h5 class="uppercase">
  81 + <span><i class="fa fa-users"></i> 用户:{{userNames}}</span>
  82 + </h5>
  83 + </div>
  84 + </li>
  85 + <li class="mt-list-item" style="border-bottom:none;">
  86 + <div class="list-item-content">
  87 + <h5 class="uppercase">
  88 + <span><i class="fa fa-ambulance"></i> 资源数:{{resources}}</span>
  89 + </h5>
  90 + </div>
  91 + </li>
  92 + <li class="mt-list-item" style="border-bottom:none;">
  93 + <div class="list-item-content">
  94 + <h5 class="uppercase">
  95 + <span><i class="fa fa-text-width"></i> 描述:{{descriptions}}</span>
  96 + </h5>
  97 + </div>
  98 + </li>
  99 + </ul>
  100 + </script>
  101 +
  102 + <div style="display: inline-block;">
  103 + <!-- BEGIN PORTLET-->
  104 + <div class="portlet light bordered">
  105 + <div class="portlet-title">
  106 + <div class="caption">
  107 + <i class="icon-bar-chart font-green"></i>
  108 + <span class="caption-subject font-green bold uppercase">分配资源</span>
  109 + </div>
  110 + <div class="actions">
  111 + <button class="btn green btn-circle btn-sm" disabled="disabled" id="saveResourceSett"><i class="fa fa-check"></i> 保存修改</button>
  112 + <a href="list.html" data-pjax class="btn grey btn-circle btn-sm"><i class="fa fa-reply"></i> 返回</a>
  113 + </div>
  114 + </div>
  115 + <div class="portlet-body">
  116 + <div class="form-group last" >
  117 + <div>
  118 + <select multiple="multiple" class="multi-select" id="resourceSettSelect" ></select>
  119 + </div>
  120 + </div>
  121 + </div>
  122 + </div>
  123 + </div>
  124 +</div>
  125 +<script>
  126 + var id = $.url().param('no')
  127 + ,roleObj;
  128 +
  129 + if(!id){
  130 + alert('缺少主键');
  131 + }
  132 + else{
  133 + $get('/role/roleInfo' ,{id:id}, function(obj){
  134 + $('.caption-helper').text(obj.updateDate);
  135 + var htmlStr = template('role_detail_temp', obj);
  136 + $('.mt-list-container').html(htmlStr);
  137 + });
  138 + $get('/role/' + id ,null, function(obj){
  139 + roleObj = obj;
  140 + });
  141 + }
  142 +
  143 + $("#saveResourceSett").on('click',function(){
  144 + if($(this).attr('disabled'))
  145 + return;
  146 +
  147 + var ids = [];
  148 + $.each($('#resourceSettSelect').val(), function(i, rId){
  149 + ids.push(rId);
  150 + });
  151 +
  152 + if(roleObj){
  153 + $post('/role/settResources', {roleId: roleObj.id,rIds: ids.join(',')}, function(){
  154 + layer.msg('修改成功!');
  155 + });
  156 + }
  157 + })
  158 +
  159 + //资源下拉框
  160 + getResourceTreeData(function(treeData){
  161 + var options = '';
  162 + for(var i = 0; i < treeData.length; i++){
  163 + //是否被当前角色持有
  164 + var selected = '';
  165 + if(treeData[i].enable){
  166 + selected = 'selected';
  167 + }
  168 + options += '<option value="'+treeData[i].id+'" '+selected+'>'+treeData[i].name+'</option>'
  169 + }
  170 + //初始化multiSelect
  171 + $('#resourceSettSelect').html(options).multiSelect({
  172 + selectableOptgroup: false,
  173 + selectableHeader: "<div class='multi-custom-header-left'>未分配</div>",
  174 + selectionHeader: "<div class='multi-custom-header-right'>已分配</div>",
  175 + }).on('change',function(){
  176 + if($(this).val() != null)
  177 + $('#saveResourceSett').removeAttr('disabled');
  178 + else
  179 + $('#saveResourceSett').attr('disabled', 'disabled');
  180 + });
  181 + });
  182 +
  183 + function getResourceTreeData(cb){
  184 + $get('/resource/findResource',{roleId:id}, function(arr){
  185 + cb && cb(arr)
  186 + });
  187 + }
  188 +</script>
0 \ No newline at end of file 189 \ No newline at end of file