添加角色相关的接口,用户信息添加角色信息

This commit is contained in:
648540858
2021-08-10 15:42:15 +08:00
parent dbc525e8fb
commit 9e8cab609d
29 changed files with 598 additions and 134 deletions

View File

@@ -52,10 +52,10 @@ public class PlatformController {
@GetMapping("/server_config")
public ResponseEntity<JSONObject> serverConfig() {
JSONObject result = new JSONObject();
result.put("deviceIp", sipConfig.getSipIp());
result.put("devicePort", sipConfig.getSipPort());
result.put("username", sipConfig.getSipId());
result.put("password", sipConfig.getSipPassword());
result.put("deviceIp", sipConfig.getIp());
result.put("devicePort", sipConfig.getPort());
result.put("username", sipConfig.getId());
result.put("password", sipConfig.getPassword());
return new ResponseEntity<>(result, HttpStatus.OK);
}

View File

@@ -1,5 +1,7 @@
package com.genersoft.iot.vmp.vmanager.log;
import com.genersoft.iot.vmp.conf.UserSetup;
import com.genersoft.iot.vmp.media.zlm.ZLMRunner;
import com.genersoft.iot.vmp.service.ILogService;
import com.genersoft.iot.vmp.storager.dao.dto.LogDto;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
@@ -8,6 +10,8 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -23,9 +27,14 @@ import java.text.SimpleDateFormat;
@RequestMapping("/api/log")
public class LogController {
private final static Logger logger = LoggerFactory.getLogger(LogController.class);
@Autowired
private ILogService logService;
@Autowired
private UserSetup userSetup;
private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/**
@@ -60,7 +69,9 @@ public class LogController {
if (StringUtils.isEmpty(query)) query = null;
if (StringUtils.isEmpty(startTime)) startTime = null;
if (StringUtils.isEmpty(endTime)) endTime = null;
if (!userSetup.getLogInDatebase()) {
logger.warn("自动记录日志功能已关闭,查询结果可能不完整。");
}
try {
if (startTime != null) format.parse(startTime);

View File

@@ -1,7 +1,11 @@
package com.genersoft.iot.vmp.vmanager.server;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.VManageBootstrap;
import com.genersoft.iot.vmp.common.VersionPo;
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.conf.UserSetup;
import com.genersoft.iot.vmp.conf.VersionInfo;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.service.IMediaServerService;
@@ -9,9 +13,13 @@ import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import gov.nist.javax.sip.SipStackImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.sip.ListeningPoint;
@@ -36,6 +44,15 @@ public class ServerController {
@Autowired
VersionInfo versionInfo;
@Autowired
SipConfig sipConfig;
@Autowired
UserSetup userSetup;
@Value("${server.port}")
private int serverPort;
@ApiOperation("流媒体服务列表")
@GetMapping(value = "/media_server/list")
@@ -113,4 +130,34 @@ public class ServerController {
result.setData(versionInfo.getVersion());
return result;
}
@ApiOperation("配置信息")
@GetMapping(value = "/config")
@ApiImplicitParams({
@ApiImplicitParam(name="type", value = "配置类型sip, base", dataTypeClass = String.class),
})
@ResponseBody
public WVPResult<JSONObject> getVersion(String type){
WVPResult<JSONObject> result = new WVPResult<>();
result.setCode(0);
result.setMsg("success");
JSONObject jsonObject = new JSONObject();
jsonObject.put("server.port", serverPort);
if (StringUtils.isEmpty(type)) {
jsonObject.put("sip", JSON.toJSON(sipConfig));
jsonObject.put("base", JSON.toJSON(userSetup));
}else {
switch (type){
case "sip":
jsonObject.put("sip", sipConfig);
break;
case "base":
jsonObject.put("base", userSetup);
break;
}
}
result.setData(jsonObject);
return result;
}
}

View File

@@ -0,0 +1,101 @@
package com.genersoft.iot.vmp.vmanager.user;
import com.genersoft.iot.vmp.conf.security.SecurityUtils;
import com.genersoft.iot.vmp.service.IRoleService;
import com.genersoft.iot.vmp.service.IUserService;
import com.genersoft.iot.vmp.storager.dao.dto.Role;
import com.genersoft.iot.vmp.storager.dao.dto.User;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.List;
@Api(tags = "角色管理")
@CrossOrigin
@RestController
@RequestMapping("/api/role")
public class RoleController {
@Autowired
private IRoleService roleService;
private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ApiOperation("添加角色")
@ApiImplicitParams({
@ApiImplicitParam(name = "name", required = true, value = "角色名", dataTypeClass = String.class),
@ApiImplicitParam(name = "authority", required = true, value = "权限(自行定义内容,目前未使用)", dataTypeClass = String.class),
})
@PostMapping("/add")
public ResponseEntity<WVPResult<Integer>> add(@RequestParam String name,
@RequestParam(required = false) String authority){
WVPResult<Integer> result = new WVPResult<>();
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
if (currenRoleId != 1) {
// 只用角色id为1才可以删除和添加用户
result.setCode(-1);
result.setMsg("用户无权限");
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
}
Role role = new Role();
role.setName(name);
role.setAuthority(authority);
role.setCreateTime(format.format(System.currentTimeMillis()));
role.setUpdateTime(format.format(System.currentTimeMillis()));
int addResult = roleService.add(role);
result.setCode(addResult > 0 ? 0 : -1);
result.setMsg(addResult > 0 ? "success" : "fail");
result.setData(addResult);
return new ResponseEntity<>(result, HttpStatus.OK);
}
@ApiOperation("删除角色")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", required = true, value = "用户Id", dataTypeClass = Integer.class),
})
@DeleteMapping("/delete")
public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
WVPResult<String> result = new WVPResult<>();
if (currenRoleId != 1) {
// 只用角色id为0才可以删除和添加用户
result.setCode(-1);
result.setMsg("用户无权限");
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
}
int deleteResult = roleService.delete(id);
result.setCode(deleteResult>0? 0 : -1);
result.setMsg(deleteResult>0? "success" : "fail");
return new ResponseEntity<>(result, HttpStatus.OK);
}
@ApiOperation("查询角色")
@ApiImplicitParams({})
@GetMapping("/all")
public ResponseEntity<WVPResult<List<Role>>> all(){
// 获取当前登录用户id
List<Role> allRoles = roleService.getAll();
WVPResult<List<Role>> result = new WVPResult<>();
result.setCode(0);
result.setMsg("success");
result.setData(allRoles);
return new ResponseEntity<>(result, HttpStatus.OK);
}
}

View File

@@ -2,7 +2,9 @@ package com.genersoft.iot.vmp.vmanager.user;
import com.genersoft.iot.vmp.conf.security.SecurityUtils;
import com.genersoft.iot.vmp.conf.security.dto.LoginUser;
import com.genersoft.iot.vmp.service.IRoleService;
import com.genersoft.iot.vmp.service.IUserService;
import com.genersoft.iot.vmp.storager.dao.dto.Role;
import com.genersoft.iot.vmp.storager.dao.dto.User;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import io.swagger.annotations.Api;
@@ -14,6 +16,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.security.sasl.AuthenticationException;
@@ -32,6 +35,9 @@ public class UserController {
@Autowired
private IUserService userService;
@Autowired
private IRoleService roleService;
private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ApiOperation("登录")
@@ -97,21 +103,38 @@ public class UserController {
@PostMapping("/add")
public ResponseEntity<WVPResult<Integer>> add(@RequestParam String username,
@RequestParam String password,
@RequestParam int roleId){
@RequestParam Integer roleId){
WVPResult<Integer> result = new WVPResult<>();
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || roleId == null) {
result.setCode(-1);
result.setMsg("参数不可为空");
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRoleId();
if (currenRoleId != 0) {
// 只用角色id为0才可以删除和添加用户
return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
if (currenRoleId != 1) {
// 只用角色id为1才可以删除和添加用户
result.setCode(-1);
result.setMsg("用户无权限");
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
}
User user = new User();
user.setUsername(username);
user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
user.setRoleId(roleId);
Role role = roleService.getRoleById(roleId);
if (role == null) {
result.setCode(-1);
result.setMsg("roleId is not found");
// 角色不存在
return new ResponseEntity<>(result, HttpStatus.OK);
}
user.setRole(role);
user.setCreateTime(format.format(System.currentTimeMillis()));
user.setUpdateTime(format.format(System.currentTimeMillis()));
int addResult = userService.addUser(user);
WVPResult<Integer> result = new WVPResult<>();
result.setCode(addResult > 0 ? 0 : -1);
result.setMsg(addResult > 0 ? "success" : "fail");
result.setData(addResult);
@@ -125,13 +148,16 @@ public class UserController {
@DeleteMapping("/delete")
public ResponseEntity<WVPResult<String>> delete(@RequestParam Integer id){
// 获取当前登录用户id
int currenRoleId = SecurityUtils.getUserInfo().getRoleId();
if (currenRoleId != 0) {
int currenRoleId = SecurityUtils.getUserInfo().getRole().getId();
WVPResult<String> result = new WVPResult<>();
if (currenRoleId != 1) {
// 只用角色id为0才可以删除和添加用户
return new ResponseEntity<>(null, HttpStatus.FORBIDDEN);
result.setCode(-1);
result.setMsg("用户无权限");
return new ResponseEntity<>(result, HttpStatus.FORBIDDEN);
}
int deleteResult = userService.deleteUser(id);
WVPResult<String> result = new WVPResult<>();
result.setCode(deleteResult>0? 0 : -1);
result.setMsg(deleteResult>0? "success" : "fail");
return new ResponseEntity<>(result, HttpStatus.OK);