All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ueboot.shiro.controller.permission.PermissionController Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
/*
 * Copyright (c)  2018
 * All rights reserved.
 * 2018-08-08 14:05:30
 */
package com.ueboot.shiro.controller.permission;

import com.ueboot.core.http.response.Response;
import com.ueboot.shiro.controller.permission.vo.PermissionFindReq;
import com.ueboot.shiro.controller.permission.vo.PermissionReq;
import com.ueboot.shiro.controller.permission.vo.PermissionResp;
import com.ueboot.shiro.entity.Permission;
import com.ueboot.shiro.service.permission.PermissionService;
import com.ueboot.shiro.shiro.ShiroEventListener;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created on 2018-08-08 14:05:30
 *
 * @author yangkui
 * @since 2.1.0 by ueboot-generator
 */
@Slf4j
@RestController
@RequestMapping(value = "/ueboot/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    // shiro权限日志记录
    @Resource
    private ShiroEventListener shiroEventListener;


    @RequiresPermissions("ueboot:permission:read")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response> page(@PageableDefault(value = 15, sort = {"id"}, direction = Sort.Direction.DESC)
                                                       Pageable pageable, @RequestBody(required = false) PermissionFindReq req) {
        Page entities = permissionService.findBy(pageable);
        Page body = entities.map(entity -> {
            PermissionResp resp = new PermissionResp();
            BeanUtils.copyProperties(entity, resp);
            return resp;
        });

        return new Response<>(body);
    }

    /**
     * 根据角色获取角色所属的权限资源
     *
     * @param req
     * @return
     */
    @RequiresPermissions("ueboot:permission:read")
    @PostMapping(value = "/findByRoleId")
    public Response> findByUserId(@RequestBody PermissionReq req) {
        List permissions = permissionService.findByRoleId(req.getRoleId());
        List result = new ArrayList<>();
        permissions.forEach((u) -> {
            PermissionResp resp = new PermissionResp();
            BeanUtils.copyProperties(u, resp);
            resp.setRoleId(u.getRole().getId());
            resp.setResourceId(u.getResource().getId());
            result.add(resp);
        });
        return new Response<>(result);
    }

    @RequiresPermissions("ueboot:permission:save")
    @PostMapping(value = "/save")
    public Response save(@RequestBody PermissionReq req) {
        permissionService.saveRolePermission(req.getRoleId(), req.getResourceIds());
        return new Response<>();
    }

    @RequiresPermissions("ueboot:permission:delete")
    @PostMapping(value = "/delete")
    public Response delete(Long[] id) {
        permissionService.delete(id);

        // 删除角色资源日志记录
        String optUserName = (String) SecurityUtils.getSubject().getPrincipal();
        this.shiroEventListener.deleteRolePermissionEvent(optUserName, id);

        return new Response<>();
    }

    @RequiresPermissions("ueboot:permission:read")
    @GetMapping(value = "/{id}")
    public Response get(@PathVariable Long id) {
        Permission entity = permissionService.get(id);
        PermissionResp resp = new PermissionResp();
        BeanUtils.copyProperties(entity, resp);
        return new Response<>(resp);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy