Selaa lähdekoodia

增加app关联user的权限控制

luoshi 2 vuotta sitten
vanhempi
commit
5fa2937fa0

+ 46 - 12
src/main/java/com/qmth/ops/api/controller/admin/AppController.java

@@ -1,16 +1,18 @@
 package com.qmth.ops.api.controller.admin;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.qmth.ops.api.constants.OpsApiConstants;
-import com.qmth.ops.biz.domain.App;
+import com.qmth.ops.api.security.AdminSession;
+import com.qmth.ops.biz.domain.*;
 import com.qmth.ops.biz.query.AppQuery;
 import com.qmth.ops.biz.service.AppService;
+import com.qmth.ops.biz.service.AppUserService;
+import com.qmth.ops.biz.service.UserService;
 import com.qmth.ops.biz.service.VersionService;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import java.util.List;
 
 @RestController
 @RequestMapping(OpsApiConstants.ADMIN_URI_PREFIX + "/app")
@@ -19,28 +21,60 @@ public class AppController {
     @Resource
     private AppService appService;
 
+    @Resource
+    private AppUserService appUserService;
+
     @Resource
     private VersionService versionService;
 
+    @Resource
+    private UserService userService;
+
     @PostMapping("/query")
-    public AppQuery query(AppQuery query) {
+    public IPage<AppDTO> query(@RequestAttribute AdminSession accessEntity, AppQuery query) {
+        User user = accessEntity.getUser();
+        if (user.getRole() == Role.DEV || user.getRole() == Role.TEST) {
+            query.setUserId(user.getId());
+        }
         return appService.query(query);
     }
 
+    @PostMapping("/list")
+    public List<AppDTO> list(@RequestAttribute AdminSession accessEntity, AppQuery query) {
+        User user = accessEntity.getUser();
+        if (user.getRole() == Role.DEV || user.getRole() == Role.TEST) {
+            query.setUserId(user.getId());
+        }
+        return appService.list(query);
+    }
+
     @PostMapping("/insert")
-    public App insert(App app) {
-        return appService.insert(app);
+    public AppDTO insert(App app) {
+        appService.insert(app);
+        return appService.findDTO(app.getId());
     }
 
     @PostMapping("/update")
-    public App update(App app) {
-        return appService.update(app);
+    public AppDTO update(App app) {
+        appService.update(app);
+        return appService.findDTO(app.getId());
     }
 
     @PostMapping("/version/master")
-    public App updateMasterVersion(@RequestParam Long id, @RequestParam Long versionId) {
+    public AppDTO updateMasterVersion(@RequestParam Long id, @RequestParam Long versionId) {
         appService.setMasterVersion(appService.getById(id), versionService.getById(versionId));
-        return appService.getById(id);
+        return appService.findDTO(id);
+    }
+
+    @PostMapping("/user/bind")
+    public AppUser bindUser(@RequestParam Long id, @RequestParam Long userId) {
+        return appUserService.insert(appService.getById(id), userService.getById(userId));
+    }
+
+    @PostMapping("/user/unbind")
+    public AppUser unbindUser(@RequestParam Long id, @RequestParam Long userId) {
+        appUserService.delete(appService.getById(id), userService.getById(userId));
+        return new AppUser(id, userId);
     }
 }
 

+ 3 - 2
src/main/java/com/qmth/ops/api/controller/admin/UserController.java

@@ -4,6 +4,7 @@ import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.annotation.BOOL;
 import com.qmth.boot.core.exception.ParameterException;
 import com.qmth.ops.api.constants.OpsApiConstants;
+import com.qmth.ops.api.dto.LoginResult;
 import com.qmth.ops.api.security.AdminSession;
 import com.qmth.ops.biz.domain.User;
 import com.qmth.ops.biz.query.UserQuery;
@@ -23,7 +24,7 @@ public class UserController {
 
     @PostMapping("/login")
     @Aac(auth = BOOL.FALSE)
-    public AdminSession login(User request) {
+    public LoginResult login(User request) {
         User user = userService.findByLoginName(request.getLoginName());
         if (user == null) {
             throw new ParameterException("登录名错误");
@@ -31,7 +32,7 @@ public class UserController {
         if (!user.buildPassword(request.getPassword()).equals(user.getPassword())) {
             throw new ParameterException("密码错误");
         }
-        return new AdminSession(user);
+        return new AdminSession(user).getLoginResult();
     }
 
     @PostMapping("/query")

+ 36 - 0
src/main/java/com/qmth/ops/api/dto/LoginResult.java

@@ -0,0 +1,36 @@
+package com.qmth.ops.api.dto;
+
+import com.qmth.ops.biz.domain.Role;
+
+public class LoginResult {
+
+    private Role role;
+
+    private String session;
+
+    private String token;
+
+    public Role getRole() {
+        return role;
+    }
+
+    public void setRole(Role role) {
+        this.role = role;
+    }
+
+    public String getSession() {
+        return session;
+    }
+
+    public void setSession(String session) {
+        this.session = session;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public void setToken(String token) {
+        this.token = token;
+    }
+}

+ 13 - 32
src/main/java/com/qmth/ops/api/security/AdminSession.java

@@ -1,55 +1,36 @@
 package com.qmth.ops.api.security;
 
 import com.qmth.boot.core.security.model.AccessEntity;
-import com.qmth.boot.tools.models.ByteArray;
-import com.qmth.ops.biz.domain.Role;
+import com.qmth.ops.api.dto.LoginResult;
 import com.qmth.ops.biz.domain.User;
 
 public class AdminSession implements AccessEntity {
 
-    private Role role;
-
-    private String session;
-
-    private String token;
+    private User user;
 
     public AdminSession(User user) {
-        this.role = user.getRole();
-        this.session = user.getId().toString();
-        this.token = ByteArray.fromString(user.getLoginName()).toBase64();
-    }
-
-    public Role getRole() {
-        return role;
-    }
-
-    public void setRole(Role role) {
-        this.role = role;
-    }
-
-    public String getSession() {
-        return session;
-    }
-
-    public void setSession(String session) {
-        this.session = session;
+        this.user = user;
     }
 
-    public String getToken() {
-        return token;
+    public LoginResult getLoginResult() {
+        LoginResult result = new LoginResult();
+        result.setRole(user.getRole());
+        result.setSession(getIdentity());
+        result.setToken(getSecret());
+        return result;
     }
 
-    public void setToken(String token) {
-        this.token = token;
+    public User getUser() {
+        return user;
     }
 
     @Override
     public String getIdentity() {
-        return session;
+        return user.getId().toString();
     }
 
     @Override
     public String getSecret() {
-        return token;
+        return user.buildAccessToken();
     }
 }

+ 10 - 0
src/main/java/com/qmth/ops/biz/dao/AppDao.java

@@ -1,8 +1,18 @@
 package com.qmth.ops.biz.dao;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.qmth.ops.biz.domain.App;
+import com.qmth.ops.biz.domain.AppDTO;
+import com.qmth.ops.biz.query.AppQuery;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 public interface AppDao extends BaseMapper<App> {
 
+    List<AppDTO> findByQuery(@Param("query") AppQuery query);
+
+    IPage<AppDTO> findByQuery(Page page, @Param("query") AppQuery query);
 }

+ 8 - 0
src/main/java/com/qmth/ops/biz/dao/AppUserDao.java

@@ -0,0 +1,8 @@
+package com.qmth.ops.biz.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.qmth.ops.biz.domain.AppUser;
+
+public interface AppUserDao extends BaseMapper<AppUser> {
+
+}

+ 74 - 0
src/main/java/com/qmth/ops/biz/domain/AppDTO.java

@@ -0,0 +1,74 @@
+package com.qmth.ops.biz.domain;
+
+public class AppDTO {
+
+    private Long id;
+
+    private String code;
+
+    private String name;
+
+    private Long masterVersionId;
+
+    private String masterVersionName;
+
+    private Long createTime;
+
+    private Long updateTime;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Long getMasterVersionId() {
+        return masterVersionId;
+    }
+
+    public void setMasterVersionId(Long masterVersionId) {
+        this.masterVersionId = masterVersionId;
+    }
+
+    public String getMasterVersionName() {
+        return masterVersionName;
+    }
+
+    public void setMasterVersionName(String masterVersionName) {
+        this.masterVersionName = masterVersionName;
+    }
+
+    public Long getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Long createTime) {
+        this.createTime = createTime;
+    }
+
+    public Long getUpdateTime() {
+        return updateTime;
+    }
+
+    public void setUpdateTime(Long updateTime) {
+        this.updateTime = updateTime;
+    }
+}

+ 40 - 0
src/main/java/com/qmth/ops/biz/domain/AppUser.java

@@ -0,0 +1,40 @@
+package com.qmth.ops.biz.domain;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+
+import java.io.Serializable;
+
+@TableName("app_user")
+public class AppUser implements Serializable {
+
+    private static final long serialVersionUID = -7034544322116255919L;
+
+    private Long appId;
+
+    private Long userId;
+
+    public AppUser() {
+
+    }
+
+    public AppUser(Long appId, Long userId) {
+        this.appId = appId;
+        this.userId = userId;
+    }
+
+    public Long getAppId() {
+        return appId;
+    }
+
+    public void setAppId(Long appId) {
+        this.appId = appId;
+    }
+
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
+    }
+}

+ 4 - 0
src/main/java/com/qmth/ops/biz/domain/User.java

@@ -98,4 +98,8 @@ public class User implements Serializable {
                 .toLowerCase();
     }
 
+    public String buildAccessToken() {
+        return ByteArray.fromString(loginName + createTime).toBase64();
+    }
+
 }

+ 9 - 4
src/main/java/com/qmth/ops/biz/query/AppQuery.java

@@ -1,6 +1,5 @@
 package com.qmth.ops.biz.query;
 
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.qmth.boot.mybatis.query.BaseQuery;
 import com.qmth.ops.biz.domain.App;
 
@@ -14,6 +13,8 @@ public class AppQuery extends BaseQuery<App> {
 
     private String nameStartWith;
 
+    private Long userId;
+
     public Long getId() {
         return id;
     }
@@ -38,8 +39,12 @@ public class AppQuery extends BaseQuery<App> {
         this.nameStartWith = nameStartWith;
     }
 
-    public LambdaQueryWrapper<App> build() {
-        return new LambdaQueryWrapper<App>().eq(id != null, App::getId, id).eq(code != null, App::getCode, code)
-                .likeRight(nameStartWith != null, App::getName, nameStartWith);
+    public Long getUserId() {
+        return userId;
+    }
+
+    public void setUserId(Long userId) {
+        this.userId = userId;
     }
+
 }

+ 17 - 4
src/main/java/com/qmth/ops/biz/service/AppService.java

@@ -2,10 +2,12 @@ package com.qmth.ops.biz.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.boot.core.exception.ParameterException;
 import com.qmth.ops.biz.dao.AppDao;
 import com.qmth.ops.biz.domain.App;
+import com.qmth.ops.biz.domain.AppDTO;
 import com.qmth.ops.biz.domain.Version;
 import com.qmth.ops.biz.query.AppQuery;
 import org.springframework.stereotype.Service;
@@ -13,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.Resource;
 import javax.validation.constraints.NotNull;
+import java.util.List;
 
 @Service
 public class AppService extends ServiceImpl<AppDao, App> {
@@ -29,15 +32,25 @@ public class AppService extends ServiceImpl<AppDao, App> {
     }
 
     @Transactional
-    public App update(App app) {
+    public void update(App app) {
         appDao.update(app, new LambdaUpdateWrapper<App>().set(app.getCode() != null, App::getCode, app.getCode())
                 .set(app.getName() != null, App::getName, app.getName())
                 .set(App::getUpdateTime, System.currentTimeMillis()).eq(App::getId, app.getId()));
-        return appDao.selectById(app.getId());
     }
 
-    public AppQuery query(AppQuery query) {
-        return appDao.selectPage(query, query.build());
+    public AppDTO findDTO(Long id) {
+        AppQuery query = new AppQuery();
+        query.setId(id);
+        List<AppDTO> list = list(query);
+        return list.isEmpty() ? null : list.get(0);
+    }
+
+    public IPage<AppDTO> query(AppQuery query) {
+        return appDao.findByQuery(query, query);
+    }
+
+    public List<AppDTO> list(AppQuery query) {
+        return appDao.findByQuery(query);
     }
 
     public App findByCode(String code) {

+ 44 - 0
src/main/java/com/qmth/ops/biz/service/AppUserService.java

@@ -0,0 +1,44 @@
+package com.qmth.ops.biz.service;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.ops.biz.dao.AppUserDao;
+import com.qmth.ops.biz.domain.App;
+import com.qmth.ops.biz.domain.AppUser;
+import com.qmth.ops.biz.domain.User;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+import javax.validation.constraints.NotNull;
+import java.util.List;
+
+@Service
+public class AppUserService extends ServiceImpl<AppUserDao, AppUser> {
+
+    @Resource
+    private AppUserDao appUserDao;
+
+    public List<AppUser> listByApp(@NotNull App app) {
+        return appUserDao.selectList(new LambdaQueryWrapper<AppUser>().eq(AppUser::getAppId, app.getId()));
+    }
+
+    public boolean exist(@NotNull App app, @NotNull User user) {
+        return appUserDao.selectOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getAppId, app.getId())
+                .eq(AppUser::getUserId, user.getId())) != null;
+    }
+
+    @Transactional
+    public AppUser insert(@NotNull App app, @NotNull User user) {
+        AppUser au = new AppUser(app.getId(), user.getId());
+        appUserDao.insert(au);
+        return au;
+    }
+
+    @Transactional
+    public void delete(@NotNull App app, @NotNull User user) {
+        appUserDao.delete(new LambdaQueryWrapper<AppUser>().eq(AppUser::getAppId, app.getId())
+                .eq(AppUser::getUserId, user.getId()));
+    }
+}
+

+ 24 - 0
src/main/resources/mapper/AppMapper.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.qmth.scancloud.dao.BatchDao">
+
+    <select id="findByQuery" resultType="com.qmth.biz.domain.AppDTO">
+        SELECT a.id, a.code, a.name, a.master_version_id, v.name, a.create_time, a.update_time
+        FROM app a
+        left join version v on a.id=v.app_id and a.master_version_id=v.id
+        <if test="query.userId != null">
+            inner join app_user au on a.id = au.app_id and au.user_id = #{query.userId}
+        </if>
+        WHERE 1=1
+        <if test="query.id != null">
+            and a.id=#{query.id}
+        </if>
+        <if test="query.code != null">
+            and t.code=#{query.code}
+        </if>
+        <if test="query.nameStartWith != null">
+            and t.name like concat(#{query.name},'%')
+        </if>
+    </select>
+
+</mapper>

+ 37 - 27
src/main/resources/script/init.sql

@@ -2,29 +2,39 @@
 CREATE TABLE `app`
 (
     `id`                bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-    `code`              varchar(64)         NOT NULL,
-    `name`              varchar(64)         NOT NULL,
-    `master_version_id` bigint(20) DEFAULT NULL,
+    `code`              varchar(64)         NOT NULL DEFAULT '',
+    `name`              varchar(64)         NOT NULL DEFAULT '',
+    `master_version_id` bigint(20) unsigned          DEFAULT NULL,
     `create_time`       bigint(20)          NOT NULL,
     `update_time`       bigint(20)          NOT NULL,
     PRIMARY KEY (`id`),
     UNIQUE KEY `code` (`code`)
+) ENGINE = InnoDB
+  AUTO_INCREMENT = 2
+  DEFAULT CHARSET = utf8mb4;
+
+-- Create syntax for TABLE 'app_user'
+CREATE TABLE `app_user`
+(
+    `app_id`  bigint(20) unsigned NOT NULL,
+    `user_id` bigint(20) unsigned NOT NULL,
+    PRIMARY KEY (`app_id`, `user_id`)
 ) ENGINE = InnoDB
   DEFAULT CHARSET = utf8mb4;
 
 -- Create syntax for TABLE 'config_item'
 CREATE TABLE `config_item`
 (
-    `app_id`      bigint(20)  NOT NULL,
-    `version_id`  bigint(20)  NOT NULL,
-    `module_id`   bigint(20)  NOT NULL,
-    `env_id`      bigint(20)  NOT NULL,
-    `key`         varchar(64) NOT NULL,
-    `value`       varchar(64) NOT NULL,
-    `comment`     varchar(128) DEFAULT NULL,
-    `mode`        varchar(16) NOT NULL,
-    `create_time` bigint(20)  NOT NULL,
-    `update_time` bigint(20)  NOT NULL,
+    `app_id`      bigint(20) unsigned NOT NULL,
+    `version_id`  bigint(20) unsigned NOT NULL,
+    `module_id`   bigint(20) unsigned NOT NULL,
+    `env_id`      bigint(20)          NOT NULL,
+    `key`         varchar(64)         NOT NULL DEFAULT '',
+    `value`       varchar(64)         NOT NULL DEFAULT '',
+    `comment`     varchar(128)                 DEFAULT NULL,
+    `mode`        varchar(16)         NOT NULL DEFAULT '',
+    `create_time` bigint(20)          NOT NULL,
+    `update_time` bigint(20)          NOT NULL,
     PRIMARY KEY (`app_id`, `version_id`, `module_id`, `env_id`, `key`)
 ) ENGINE = InnoDB
   DEFAULT CHARSET = utf8mb4;
@@ -33,10 +43,10 @@ CREATE TABLE `config_item`
 CREATE TABLE `env`
 (
     `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-    `app_id`      bigint(20)          NOT NULL,
-    `code`        varchar(64)         NOT NULL,
-    `name`        varchar(64)         NOT NULL,
-    `type`        varchar(16)         NOT NULL,
+    `app_id`      bigint(20) unsigned NOT NULL,
+    `code`        varchar(64)         NOT NULL DEFAULT '',
+    `name`        varchar(64)         NOT NULL DEFAULT '',
+    `type`        varchar(16)         NOT NULL DEFAULT '',
     `create_time` bigint(20)          NOT NULL,
     `update_time` bigint(20)          NOT NULL,
     PRIMARY KEY (`id`),
@@ -48,9 +58,9 @@ CREATE TABLE `env`
 CREATE TABLE `module`
 (
     `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-    `app_id`      bigint(20)          NOT NULL,
-    `code`        varchar(64)         NOT NULL,
-    `name`        varchar(64)         NOT NULL,
+    `app_id`      bigint(20) unsigned NOT NULL,
+    `code`        varchar(64)         NOT NULL DEFAULT '',
+    `name`        varchar(64)         NOT NULL DEFAULT '',
     `enable`      tinyint(1)          NOT NULL,
     `create_time` bigint(20)          NOT NULL,
     `update_time` bigint(20)          NOT NULL,
@@ -63,11 +73,11 @@ CREATE TABLE `module`
 CREATE TABLE `user`
 (
     `id`            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-    `login_name`    varchar(64)         NOT NULL,
-    `name`          varchar(64)         NOT NULL,
-    `password`      varchar(64)         NOT NULL,
-    `role`          varchar(16)         NOT NULL,
-    `export_secret` varchar(64)         NOT NULL,
+    `login_name`    varchar(64)         NOT NULL DEFAULT '',
+    `name`          varchar(64)         NOT NULL DEFAULT '',
+    `password`      varchar(64)         NOT NULL DEFAULT '',
+    `role`          varchar(16)         NOT NULL DEFAULT '',
+    `export_secret` varchar(64)         NOT NULL DEFAULT '',
     `create_time`   bigint(20)          NOT NULL,
     `update_time`   bigint(20)          NOT NULL,
     PRIMARY KEY (`id`),
@@ -79,8 +89,8 @@ CREATE TABLE `user`
 CREATE TABLE `version`
 (
     `id`            bigint(20) unsigned NOT NULL AUTO_INCREMENT,
-    `app_id`        bigint(20)          NOT NULL,
-    `name`          varchar(64)         NOT NULL,
+    `app_id`        bigint(20) unsigned NOT NULL,
+    `name`          varchar(64)         NOT NULL DEFAULT '',
     `main_number`   int(11)             NOT NULL,
     `middle_number` int(11)             NOT NULL,
     `sub_number`    int(11)             NOT NULL,