use RestController

This commit is contained in:
devezhao 2020-11-05 13:09:02 +08:00
parent c3a729622d
commit 88951d27f2
6 changed files with 113 additions and 134 deletions

View file

@ -28,34 +28,34 @@ import javax.servlet.http.HttpServletRequest;
public class BizzPageView extends EntityController {
@GetMapping("User/view/{id}")
public ModelAndView userView(@PathVariable String id, HttpServletRequest request) {
ID record = ID.valueOf(id);
ModelAndView mv = createModelAndView("/admin/bizuser/user-view", "User", getRequestUser(request));
mv.getModel().put("id", record);
public ModelAndView userView(@PathVariable ID id, HttpServletRequest request) {
ModelAndView mv = createModelAndView(
"/admin/bizuser/user-view", "User", getRequestUser(request));
mv.getModel().put("id", id);
return mv;
}
@GetMapping("Department/view/{id}")
public ModelAndView deptView(@PathVariable String id, HttpServletRequest request) {
ID record = ID.valueOf(id);
ModelAndView mv = createModelAndView("/admin/bizuser/dept-view", "Department", getRequestUser(request));
mv.getModel().put("id", record);
public ModelAndView deptView(@PathVariable ID id, HttpServletRequest request) {
ModelAndView mv = createModelAndView(
"/admin/bizuser/dept-view", "Department", getRequestUser(request));
mv.getModel().put("id", id);
return mv;
}
@GetMapping("Role/view/{id}")
public ModelAndView roleView(@PathVariable String id, HttpServletRequest request) {
ID record = ID.valueOf(id);
ModelAndView mv = createModelAndView("/admin/bizuser/role-view", "Role", getRequestUser(request));
mv.getModel().put("id", record);
public ModelAndView roleView(@PathVariable ID id, HttpServletRequest request) {
ModelAndView mv = createModelAndView(
"/admin/bizuser/role-view", "Role", getRequestUser(request));
mv.getModel().put("id", id);
return mv;
}
@GetMapping("Team/view/{id}")
public ModelAndView teamView(@PathVariable String id, HttpServletRequest request) {
ID record = ID.valueOf(id);
ModelAndView mv = createModelAndView("/admin/bizuser/team-view", "Team", getRequestUser(request));
mv.getModel().put("id", record);
public ModelAndView teamView(@PathVariable ID id, HttpServletRequest request) {
ModelAndView mv = createModelAndView(
"/admin/bizuser/team-view", "Team", getRequestUser(request));
mv.getModel().put("id", id);
return mv;
}
}

View file

@ -12,31 +12,32 @@ import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.api.RespBody;
import com.rebuild.core.Application;
import com.rebuild.core.configuration.general.DataListManager;
import com.rebuild.core.privileges.DepartmentService;
import com.rebuild.core.privileges.bizz.Department;
import com.rebuild.web.EntityController;
import org.springframework.stereotype.Controller;
import com.rebuild.web.IdParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author devezhao
* @since 10/08/2018
*/
@Controller
@RestController
@RequestMapping("/admin/bizuser/")
public class DepartmentController extends EntityController {
@GetMapping("departments")
public ModelAndView pageList(HttpServletRequest request) {
ID user = getRequestUser(request);
final ID user = getRequestUser(request);
ModelAndView mv = createModelAndView("/admin/bizuser/dept-list", "Department", user);
JSON config = DataListManager.instance.getFieldsLayout("Department", user);
@ -45,28 +46,22 @@ public class DepartmentController extends EntityController {
}
@PostMapping("dept-delete")
public void deptDelete(HttpServletRequest request, HttpServletResponse response) {
ID dept = getIdParameterNotNull(request, "id");
public RespBody deptDelete(@IdParam ID deptId, HttpServletRequest request) {
ID transfer = getIdParameter(request, "transfer"); // TODO 转移到新部门
Application.getBean(DepartmentService.class).deleteAndTransfer(dept, transfer);
writeSuccess(response);
Application.getBean(DepartmentService.class).deleteAndTransfer(deptId, transfer);
return RespBody.ok();
}
@RequestMapping("dept-tree")
public void deptTreeGet(HttpServletResponse response) {
public JSON deptTreeGet() {
JSONArray dtree = new JSONArray();
for (Department root : Application.getUserStore().getTopDepartments()) {
dtree.add(recursiveDeptTree(root));
}
writeSuccess(response, dtree);
return dtree;
}
/**
* 部门结构
*
* @param parent
*/
private JSONObject recursiveDeptTree(Department parent) {
JSONObject parentJson = new JSONObject();
parentJson.put("id", parent.getIdentity());

View file

@ -12,6 +12,7 @@ import cn.devezhao.persist4j.Entity;
import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.api.RespBody;
import com.rebuild.core.Application;
import com.rebuild.core.metadata.MetadataHelper;
import com.rebuild.core.metadata.MetadataSorter;
@ -19,15 +20,11 @@ import com.rebuild.core.metadata.impl.EasyMeta;
import com.rebuild.core.privileges.RoleService;
import com.rebuild.utils.JSONUtils;
import com.rebuild.web.EntityController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.rebuild.web.IdParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
@ -35,31 +32,29 @@ import java.util.List;
* @author devezhao
* @since 10/08/2018
*/
@Controller
@RestController
@RequestMapping("/admin/bizuser/")
public class RolePrivilegesController extends EntityController {
@GetMapping("role-privileges")
public ModelAndView pageList(HttpServletRequest request) {
ID user = getRequestUser(request);
final ID user = getRequestUser(request);
ModelAndView mv = createModelAndView("/admin/bizuser/role-privileges", "Role", user);
setEntities(mv);
return mv;
}
@GetMapping("role/{id}")
public ModelAndView pagePrivileges(@PathVariable String id, HttpServletRequest request) {
ID user = getRequestUser(request);
ID roleId = ID.valueOf(id);
public ModelAndView pagePrivileges(@PathVariable ID id, HttpServletRequest request) {
final ID user = getRequestUser(request);
ModelAndView mv = createModelAndView("/admin/bizuser/role-privileges", "Role", user);
setEntities(mv);
mv.getModel().put("RoleId", roleId);
mv.getModel().put("RoleId", id);
return mv;
}
/**
* @param mv
*/
private void setEntities(ModelAndView mv) {
List<Object[]> entities = new ArrayList<>();
for (Entity e : MetadataSorter.sortEntities()) {
@ -71,18 +66,16 @@ public class RolePrivilegesController extends EntityController {
}
@GetMapping("role-list")
public void roleList(HttpServletResponse response) {
public JSON roleList() {
Object[][] array = Application.createQuery("select roleId,name,isDisabled from Role").array();
JSON retJson = JSONUtils.toJSONObjectArray(new String[]{"id", "name", "disabled"}, array);
writeSuccess(response, retJson);
return JSONUtils.toJSONObjectArray(
new String[] { "id", "name", "disabled" }, array);
}
@GetMapping("privileges-list")
public void privilegesList(HttpServletRequest request, HttpServletResponse response) {
ID roleId = getIdParameterNotNull(request, "role");
public RespBody privilegesList(@IdParam(name = "role") ID roleId) {
if (RoleService.ADMIN_ROLE.equals(roleId)) {
writeFailure(response, getLang(request, "NotModifyAdminRole"));
return;
return RespBody.errorl("NotModifyAdminRole");
}
Object[][] array = Application.createQuery(
@ -95,24 +88,24 @@ public class RolePrivilegesController extends EntityController {
}
}
JSON retJson = JSONUtils.toJSONObjectArray(new String[]{"name", "definition"}, array);
writeSuccess(response, retJson);
JSON retJson = JSONUtils.toJSONObjectArray(
new String[] { "name", "definition" }, array);
return RespBody.ok(retJson);
}
@PostMapping("privileges-update")
public void privilegesUpdate(HttpServletRequest request, HttpServletResponse response) {
public RespBody privilegesUpdate(@IdParam(name = "role") ID roleId, HttpServletRequest request) {
JSON post = ServletUtils.getRequestJson(request);
ID role = getIdParameterNotNull(request, "role");
Application.getBean(RoleService.class).updatePrivileges(role, (JSONObject) post);
writeSuccess(response);
Application.getBean(RoleService.class).updatePrivileges(roleId, (JSONObject) post);
return RespBody.ok();
}
@PostMapping("role-delete")
public void roleDelete(HttpServletRequest request, HttpServletResponse response) {
ID role = getIdParameterNotNull(request, "id");
public RespBody roleDelete(@IdParam ID roleId, HttpServletRequest request) {
ID transfer = getIdParameter(request, "transfer"); // TODO 转移到新角色
Application.getBean(RoleService.class).deleteAndTransfer(role, transfer);
writeSuccess(response);
Application.getBean(RoleService.class).deleteAndTransfer(roleId, transfer);
return RespBody.ok();
}
}

View file

@ -12,20 +12,21 @@ import cn.devezhao.commons.web.ServletUtils;
import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.rebuild.api.RespBody;
import com.rebuild.core.Application;
import com.rebuild.core.configuration.general.DataListManager;
import com.rebuild.core.privileges.TeamService;
import com.rebuild.core.privileges.UserHelper;
import com.rebuild.core.privileges.bizz.User;
import com.rebuild.web.EntityController;
import org.springframework.stereotype.Controller;
import com.rebuild.web.IdParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collections;
@ -36,55 +37,49 @@ import java.util.Set;
* @author devezhao
* @since 2019/11/13
*/
@Controller
@RestController
@RequestMapping("/admin/bizuser/")
public class TeamController extends EntityController {
@GetMapping("teams")
public ModelAndView pageList(HttpServletRequest request) {
final ID user = getRequestUser(request);
ModelAndView mv = createModelAndView("/admin/bizuser/team-list", "Team", user);
JSON config = DataListManager.instance.getFieldsLayout("Team", user);
mv.getModel().put("DataListConfig", JSON.toJSONString(config));
return mv;
}
@GetMapping("team-members")
public void getMembers(HttpServletRequest request, HttpServletResponse response) {
ID teamId = getIdParameterNotNull(request, "team");
public List<Object[]> getMembers(@IdParam(name = "team") ID teamId) {
Team team = Application.getUserStore().getTeam(teamId);
List<Object[]> members = new ArrayList<>();
for (Principal p : team.getMembers()) {
User user = (User) p;
members.add(new Object[]{
members.add(new Object[] {
user.getId(), user.getFullName(),
user.getOwningDept() != null ? user.getOwningDept().getName() : null
});
}
writeSuccess(response, members);
return members;
}
@PostMapping("team-members-add")
public void addMembers(HttpServletRequest request, HttpServletResponse response) {
final ID teamId = getIdParameterNotNull(request, "team");
public RespBody addMembers(@IdParam(name = "team") ID teamId, HttpServletRequest request) {
JSON usersDef = ServletUtils.getRequestJson(request);
Set<ID> users = UserHelper.parseUsers((JSONArray) usersDef, null);
if (!users.isEmpty()) {
Application.getBean(TeamService.class).createMembers(teamId, users);
}
writeSuccess(response);
return RespBody.ok();
}
@PostMapping("team-members-del")
public void deleteMembers(HttpServletRequest request, HttpServletResponse response) {
ID teamId = getIdParameterNotNull(request, "team");
ID userId = getIdParameterNotNull(request, "user");
public RespBody deleteMembers(@IdParam(name = "team") ID teamId, @IdParam(name = "user") ID userId) {
Application.getBean(TeamService.class).deleteMembers(teamId, Collections.singletonList(userId));
writeSuccess(response);
return RespBody.ok();
}
}

View file

@ -13,6 +13,7 @@ import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.engine.ID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rebuild.api.RespBody;
import com.rebuild.core.Application;
import com.rebuild.core.configuration.general.DataListManager;
import com.rebuild.core.metadata.EntityHelper;
@ -24,14 +25,14 @@ import com.rebuild.core.support.RebuildConfiguration;
import com.rebuild.core.support.integration.SMSender;
import com.rebuild.utils.JSONUtils;
import com.rebuild.web.EntityController;
import org.springframework.stereotype.Controller;
import com.rebuild.web.IdParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.HashSet;
@ -42,7 +43,7 @@ import java.util.Set;
* @author devezhao
* @since 10/08/2018
*/
@Controller
@RestController
@RequestMapping("/admin/bizuser/")
public class UserController extends EntityController {
@ -57,19 +58,16 @@ public class UserController extends EntityController {
}
@RequestMapping("check-user-status")
public void checkUserStatus(HttpServletRequest request, HttpServletResponse response) {
final ID user = getIdParameterNotNull(request, "id");
if (!Application.getUserStore().existsUser(user)) {
writeFailure(response);
return;
public RespBody checkUserStatus(@IdParam ID userId) {
if (!Application.getUserStore().existsUser(userId)) {
return RespBody.error();
}
User checkedUser = Application.getUserStore().getUser(user);
User checkedUser = Application.getUserStore().getUser(userId);
Map<String, Object> ret = new HashMap<>();
ret.put("active", checkedUser.isActive());
ret.put("system", "system".equals(checkedUser.getName()) || "admin".equals(checkedUser.getName()));
ret.put("disabled", checkedUser.isDisabled());
if (checkedUser.getOwningRole() != null) {
@ -77,7 +75,7 @@ public class UserController extends EntityController {
ret.put("roleDisabled", checkedUser.getOwningRole().isDisabled());
// 附加角色
ret.put("roleAppends", UserHelper.getRoleAppends(user));
ret.put("roleAppends", UserHelper.getRoleAppends(userId));
}
if (checkedUser.getOwningDept() != null) {
@ -85,34 +83,37 @@ public class UserController extends EntityController {
ret.put("deptDisabled", checkedUser.getOwningDept().isDisabled());
}
writeSuccess(response, ret);
return RespBody.ok(ret);
}
@PostMapping("enable-user")
public void enableUser(HttpServletRequest request, HttpServletResponse response) {
public RespBody enableUser(@IdParam(name = "user") ID userId, HttpServletRequest request) {
JSONObject data = (JSONObject) ServletUtils.getRequestJson(request);
ID user = ID.valueOf(data.getString("user"));
User u = Application.getUserStore().getUser(user);
User enUser = Application.getUserStore().getUser(userId);
// 当前是从未激活状态
final boolean beforeUnEnabled = u.isDisabled() && (u.getOwningDept() == null || u.getOwningRole() == null);
final boolean beforeUnEnabled = enUser.isDisabled()
&& (enUser.getOwningDept() == null || enUser.getOwningRole() == null);
ID deptNew = null;
ID roleNew = null;
ID[] roleAppends = null;
if (data.containsKey("dept")) {
deptNew = ID.valueOf(data.getString("dept"));
if (u.getOwningDept() != null && u.getOwningDept().getIdentity().equals(deptNew)) {
if (enUser.getOwningDept() != null && enUser.getOwningDept().getIdentity().equals(deptNew)) {
deptNew = null;
}
}
if (data.containsKey("role")) {
roleNew = ID.valueOf(data.getString("role"));
if (u.getOwningRole() != null && u.getOwningRole().getIdentity().equals(roleNew)) {
if (enUser.getOwningRole() != null && enUser.getOwningRole().getIdentity().equals(roleNew)) {
roleNew = null;
}
}
if (data.containsKey("roleAppends")) {
String appends = data.getString("roleAppends");
Set<ID> set = new HashSet<>();
@ -122,8 +123,8 @@ public class UserController extends EntityController {
if (roleNew != null) {
set.remove(deptNew);
} else if (u.getOwningRole() != null) {
set.remove(u.getOwningRole().getIdentity());
} else if (enUser.getOwningRole() != null) {
set.remove(enUser.getOwningRole().getIdentity());
}
if (!set.isEmpty()) {
@ -137,83 +138,79 @@ public class UserController extends EntityController {
}
Application.getBean(UserService.class)
.updateEnableUser(user, deptNew, roleNew, roleAppends, enableNew);
.updateEnableUser(userId, deptNew, roleNew, roleAppends, enableNew);
// 是否需要发送激活通知
u = Application.getUserStore().getUser(user);
if (beforeUnEnabled && u.isActive() && SMSender.availableMail() && u.getEmail() != null) {
enUser = Application.getUserStore().getUser(userId);
if (beforeUnEnabled && enUser.isActive() && SMSender.availableMail() && enUser.getEmail() != null) {
Object did = Application.createQuery(
"select logId from LoginLog where user = ?")
.setParameter(1, u.getId())
.setParameter(1, enUser.getId())
.unique();
if (did == null) {
String homeUrl = RebuildConfiguration.getHomeUrl();
String subject = getLang(request, "YourAccountActive");
String content = String.format(getLang(request, "NewUserAccountActive"), u.getFullName(), homeUrl, homeUrl);
String content = String.format(getLang(request, "NewUserAccountActive"), enUser.getFullName(), homeUrl, homeUrl);
SMSender.sendMailAsync(u.getEmail(), subject, content);
SMSender.sendMailAsync(enUser.getEmail(), subject, content);
}
}
// 禁用后马上使之登录失效
if (!u.isActive()) {
HttpSession s = Application.getSessionStore().getSession(u.getId());
if (!enUser.isActive()) {
HttpSession s = Application.getSessionStore().getSession(enUser.getId());
if (s != null) {
LOG.warn("Force destroy user session : " + u.getId());
LOG.warn("Force destroy user session : " + enUser.getId());
s.invalidate();
}
}
writeSuccess(response);
return RespBody.ok();
}
@RequestMapping("delete-checks")
public void deleteChecks(HttpServletRequest request, HttpServletResponse response) {
// 用户/部门/角色
final ID bizz = getIdParameterNotNull(request, "id");
public JSON deleteChecks(@IdParam ID bizzId) {
int hasMember = 0;
int hasChild = 0;
if (bizz.getEntityCode() == EntityHelper.Department) {
Department dept = Application.getUserStore().getDepartment(bizz);
if (bizzId.getEntityCode() == EntityHelper.Department) {
Department dept = Application.getUserStore().getDepartment(bizzId);
hasMember = dept.getMembers().size();
hasChild = dept.getChildren().size();
} else if (bizz.getEntityCode() == EntityHelper.Role) {
hasMember = UserHelper.getMembers(bizz).length;
hasMember += UserHelper.getRoleMembers(bizz).size();
} else if (bizzId.getEntityCode() == EntityHelper.Role) {
hasMember = UserHelper.getMembers(bizzId).length;
hasMember += UserHelper.getRoleMembers(bizzId).size();
} else if (bizz.getEntityCode() == EntityHelper.User) {
} else if (bizzId.getEntityCode() == EntityHelper.User) {
// NOTE 仅检查是否登陆过严谨些还应该检查是否有其他业务数据
Object[] hasLogin = Application.createQueryNoFilter(
"select count(logId) from LoginLog where user = ?")
.setParameter(1, bizz)
.setParameter(1, bizzId)
.unique();
hasMember = ObjectUtils.toInt(hasLogin[0]);
}
JSONObject ret = JSONUtils.toJSONObject(
new String[]{"hasMember", "hasChild"},
new Object[]{hasMember, hasChild});
writeSuccess(response, ret);
return JSONUtils.toJSONObject(
new String[] { "hasMember", "hasChild"},
new Object[] { hasMember, hasChild });
}
@PostMapping("user-delete")
public void userDelete(HttpServletRequest request, HttpServletResponse response) {
ID user = getIdParameterNotNull(request, "id");
Application.getBean(UserService.class).delete(user);
writeSuccess(response);
public RespBody userDelete(@IdParam ID userId) {
Application.getBean(UserService.class).delete(userId);
return RespBody.ok();
}
@PostMapping("user-resetpwd")
public void userResetpwd(HttpServletRequest request, HttpServletResponse response) {
ID user = getIdParameterNotNull(request, "id");
public RespBody userResetpwd(@IdParam ID userId, HttpServletRequest request) {
String newp = getParameterNotNull(request, "newp");
Record record = EntityHelper.forUpdate(user, user);
Record record = EntityHelper.forUpdate(userId, userId);
record.setString("password", newp);
Application.getBean(UserService.class).update(record);
writeSuccess(response);
return RespBody.ok();
}
}

View file

@ -33,7 +33,6 @@ import com.rebuild.web.EntityController;
import com.rebuild.web.EntityParam;
import com.rebuild.web.IdParam;
import com.rebuild.web.InvalidParameterException;
import com.rebuild.web.commons.MetadataGetting;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;