CRM--permission权限管理

在CRM项目中,实现了一套完整的权限管理模块。包括复制配置文件,创建多个包如cn.wolfcode.config, cn.wolfcode.controller等,并在main路径下设置web.xml和views包。同时,定义了MyFreemarkerConfig类,PermissionMapper接口,IPermissionService接口及其实现类PermissionServiceImpl,还有PermissionMapper.xml文件和对应的PermissionController,以及视图文件permission/list.ftl。" 119948576,9512050,双线性插值法在图像缩放中的应用解析,"['图像处理', '线性代数', '双线性插值']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(CRM项目)

复制配置文件
创建包cn.wolfcode.config
cn.wolfcode.controller
cn.wolfcode.service
cn.wolfcode.service.impl
cn.wolfcode.qo
cn.wolfcode.util
在main路径下创建webapp/WEB-INF/web.xml和views包

cn.wolfcode.config里写MyFreemarkerConfig
MyFreemarkerConfig类

public class MyFreemarkerConfig extends FreeMarkerConfigurer {
    @Override
    public void afterPropertiesSet() throws IOException, TemplateException {
        super.afterPropertiesSet();
        Configuration configuration = this.getConfiguration();
        configuration.setSharedVariable("shiro",new ShiroTags());
    }
}

PermissionMapper接口

public interface PermissionMapper {
    int deleteByPrimaryKey(Long id);

    int insert(Permission record);

    Permission selectByPrimaryKey(Long id);

    List<Permission> selectAll();

    int updateByPrimaryKey(Permission record);

    int count();

    List<Permission> list(QueryObject qo);

    List<String> selectExpressionByCurrentuserId(Long id);

    List<String> selectAllExpression();
}

IPermissionService类

public interface IPermissionService {
    int deleteByPrimaryKey(Long id);

    int insert(Permission record);

    Permission selectByPrimaryKey(Long id);

    List<Permission> selectAll();

    int updateByPrimaryKey(Permission record);

    PageInfo<Permission> list(QueryObject qo);


    List<String> selectExpressionByCurrentuserId(Long id);

    List<String> selectAllExpression();
}

PermissionServiceImpl类

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper mapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Permission record) {
        return mapper.insert(record);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(Permission record) {
        return 0;
    }

    @Override
    public PageInfo<Permission> list(QueryObject qo) {
        //查询一页的Permission数据
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> list = mapper.list(qo);

        return new PageInfo<Permission>(list);
    }

    @Override
    public List<String> selectExpressionByCurrentuserId(Long id) {
        return mapper.selectExpressionByCurrentuserId(id);
    }

    @Override
    public List<String> selectAllExpression() {
        return mapper.selectAllExpression();
    }
}

PermissionMapper.xml

<select id="selectAll" resultMap="BaseResultMap" >
    select id, name, expression
    from permission
  </select>
  <select id="count" resultType="java.lang.Integer">
    select count(*) from permission
  </select>
  <select id="list" resultType="cn.wolfcode.domain.Permission">
    select * from permission
  </select>
  <select id="selectExpressionByCurrentuserId" resultType="java.lang.String">
    select p.expression
    from employee_role er left join role_permission rp on er.role_id = rp.role_id
    left join permission p on rp.permission_id = p.id
    where er.employee_id = #{id}
  </select>
  <select id="selectAllExpression" resultType="java.lang.String">
    select expression from permission
  </select>

PermissionController

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @Autowired
    private ApplicationContext applicationContext;

    @RequestMapping("/list")
    public String list(QueryObject qo, Model model){

        PageInfo<Permission> pr = permissionService.list(qo);
        model.addAttribute("pageResult", pr);
        return "permission/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id)
    {
        //删除权限表中的记录
        permissionService.deleteByPrimaryKey(id);
        //删除角色——权限表中,有该权限的记录
        //通过权限id到角色——权限表中,删除相关记录
        rolePermissionService.deleteByPermissionId(id);
        return "redirect:/permission/list.do";
    }


    @RequestMapping("/reload")
    @ResponseBody
    public JsonResult reload(){

        try {
            //获取所有的权限表达式
            List<String> list = permissionService.selectAllExpression();

            Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Controller.class);
            Collection<Object> controllerCglibProxys = beans.values();
            for (Object controllerCglibProxy : controllerCglibProxys) {
                if (!AopUtils.isCglibProxy(controllerCglibProxy)) {
                    continue;
                }
                Class clzz = controllerCglibProxy.getClass().getSuperclass();
                Method[] methods = clzz.getDeclaredMethods();
                for (Method method : methods) {
                    RequiresPermissions requiresPermissions = method.getAnnotation(RequiresPermissions.class);
                    if (requiresPermissions!=null){
                        String[] expandname = requiresPermissions.value();
                        String expression = expandname[0];
                        String name = expandname[1];
                        //判断数据库中是否包含当前expression
                        if (!list.contains(expression)){
                            Permission permission = new Permission();
                            permission.setName(name);
                            permission.setExpression(expression);
                            permissionService.insert(permission);
                        }
                    }
                }
            }
            return new JsonResult(true,"重新加载成功");
        }catch (Exception e){
            return new JsonResult(false,"重新加载失败");
        }



    }

permission/list.ftl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>权限管理</title>
    <#include "../common/link.ftl">
    <script>
        function permissionreset(){
            //window.location.href = '/permission/reload.do';

            $.post('/permission/reload.do',function (obj){
                if (obj.success){
                    $.messager.alert("加载成功");
                    window.setTimeout(function (){
                        window.location.reload();
                    },1000)
                }else {
                    $.messager.popup(obj.msg);
                }
            });
        }
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="permission">
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>权限管理</h1>
        </section>
        <section class="content">
            <div class="box" >
                <!--高级查询--->
                <form class="form-inline" id="searchForm" action="/permission/list.do" method="post">
                    <input type="hidden" name="currentPage" id="currentPage" value="1">
                    <a href="javascript:permissionreset();" class="btn btn-success btn_reload" style="margin: 10px;">
                        <span class="glyphicon glyphicon-repeat"></span>  重新加载
                    </a>
                </form>

                <table class="table table-striped table-hover" >
                    <thead>
                    <tr>
                        <th>编号</th>
                        <th>权限名称</th>
                        <th>权限表达式</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <#list (pageResult.list)! as permission>
                        <tr>
                            <td>${(permission_index+1)!}</td>
                            <td>${(permission.name)!}</td>
                            <td>${(permission.expression)!}</td>
                            <td>
                                <a href="/permission/delete.do?id=${(permission.id)!}" class="btn btn-danger btn-xs btn_delete" >
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>
                            </td>
                        </tr>
                    </#list>
                </table>
                <!--分页-->
                <#include "../common/page.ftl">
            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值