Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev 0.3 refactor share module #1875

Merged
merged 22 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f45eab4
feature: common share factor
RichardShan Apr 22, 2020
1bef144
refactor: share widget
RichardShan Apr 23, 2020
b71f2f4
refactor: post share
RichardShan Apr 23, 2020
58c9f07
refactor: Add Aspect AOP for davinci `share` module
RichardShan Apr 26, 2020
c164083
refactor: refresh share token and data token
RichardShan Apr 26, 2020
0e9e32c
refactor: add preflight and get share permissions api
RichardShan Apr 26, 2020
d019542
refactor: share aop add download permission check
RichardShan Apr 29, 2020
dd7b096
fix: spelling mistakes
RichardShan Jul 27, 2020
4c0c117
fix: import objects
RichardShan Jul 28, 2020
36b1d28
refactor: share
RichardShan Jul 28, 2020
cad3e28
feature: common share factor
RichardShan Apr 22, 2020
0d62104
refactor: share widget
RichardShan Apr 23, 2020
f915e8a
refactor: post share
RichardShan Apr 23, 2020
220a240
refactor: Add Aspect AOP for davinci `share` module
RichardShan Apr 26, 2020
6afe245
refactor: refresh share token and data token
RichardShan Apr 26, 2020
ab8c527
refactor: add preflight and get share permissions api
RichardShan Apr 26, 2020
fe0ccd8
refactor: share aop add download permission check
RichardShan Apr 29, 2020
61d1352
fix: spelling mistakes
RichardShan Jul 27, 2020
21be397
fix: import objects
RichardShan Jul 28, 2020
eca33be
Merge branch 'dev-0.3' of github.com:RichardShan/davinci into dev-0.3
RichardShan Aug 6, 2020
f3e9081
fix: auth share get permission error
RichardShan Aug 7, 2020
e7ddfe2
fix: auth share get parse Display
RichardShan Aug 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: add preflight and get share permissions api
  • Loading branch information
RichardShan committed Aug 6, 2020
commit ab8c5278e9561e64bc2b9f44c9067529c1f5a53b
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

package edp.davinci.controller;

import com.alibaba.druid.util.StringUtils;
import edp.core.annotation.AuthIgnore;
import edp.core.annotation.AuthShare;
import edp.core.annotation.CurrentUser;
import edp.core.utils.FileUtils;
import edp.davinci.common.controller.BaseController;
import edp.davinci.core.common.Constants;
import edp.davinci.core.common.ErrorMsg;
import edp.davinci.core.common.ResultMap;
import edp.davinci.core.enums.DownloadType;
import edp.davinci.core.enums.FileTypeEnum;
Expand Down Expand Up @@ -133,16 +131,10 @@ public ResponseEntity submitShareDownloadTask(@PathVariable(name = "token") Stri
@RequestParam(required = false) String password,
@PathVariable(name = "uuid") String uuid,
@PathVariable(name = "type") String type,
@Valid @RequestBody(required = false) DownloadViewExecuteParam[] params,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
if (StringUtils.isEmpty(token)) {
ResultMap resultMap = new ResultMap().fail().message(ErrorMsg.ERR_INVALID_TOKEN);
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}
@Valid @RequestBody(required = false) DownloadViewExecuteParam[] params) {

List<DownloadViewExecuteParam> downloadViewExecuteParams = Arrays.asList(params);
boolean rst = shareDownloadService.submit(DownloadType.getDownloadType(type), uuid, token, user, downloadViewExecuteParams);
boolean rst = shareDownloadService.submit(DownloadType.getDownloadType(type), uuid, downloadViewExecuteParams);

return ResponseEntity.ok(rst ? new ResultMap().success() : new ResultMap().fail());
}
Expand All @@ -154,15 +146,9 @@ public ResponseEntity getShareDownloadRecordFile(@PathVariable(name = "token") S
@RequestParam(required = false) String password,
@PathVariable(name = "uuid") String uuid,
@PathVariable(name = "id") String id,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request,
HttpServletResponse response) {
if (StringUtils.isEmpty(token)) {
ResultMap resultMap = new ResultMap().fail().message(ErrorMsg.ERR_INVALID_TOKEN);
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

ShareDownloadRecord record = shareDownloadService.downloadById(id, uuid, token, user);
ShareDownloadRecord record = shareDownloadService.downloadById(id, uuid);
FileInputStream is = null;
try {
encodeFileName(request, response, record.getName() + FileTypeEnum.XLSX.getFormat());
Expand All @@ -184,14 +170,9 @@ public ResponseEntity getShareDownloadRecordPage(@PathVariable(name = "token") S
@PathVariable(name = "uuid") String uuid,
@ApiIgnore @CurrentUser User user,
HttpServletRequest request) {
if (StringUtils.isEmpty(token)) {
ResultMap resultMap = new ResultMap().fail().message(ErrorMsg.ERR_INVALID_TOKEN);
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

List<ShareDownloadRecord> records = shareDownloadService.queryDownloadRecordPage(uuid, token, user);
List<ShareDownloadRecord> records = shareDownloadService.queryDownloadRecordPage(uuid);

if (null == user) {
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap(tokenUtils).payloads(records));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(records));
Expand Down
44 changes: 30 additions & 14 deletions server/src/main/java/edp/davinci/controller/ShareController.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package edp.davinci.controller;

import com.alibaba.druid.util.StringUtils;
import edp.core.annotation.AuthIgnore;
import edp.core.annotation.AuthShare;
import edp.core.annotation.CurrentUser;
import edp.core.model.Paginate;
Expand Down Expand Up @@ -68,6 +66,29 @@ public class ShareController extends BaseController {
@Autowired
private ShareService shareService;

@ApiOperation(value = "share preflight")
@AuthShare(type = ShareType.LOGIN)
@GetMapping(value = "/preflight/{token}")
public ResponseEntity preFlight(@PathVariable String token) {
Map<String, Object> result = shareService.checkShareToken();
return ResponseEntity.ok(new ResultMap().success().payload(result));
}

@ApiOperation(value = "get share permission")
@AuthShare(type = ShareType.PERMISSION, operation = ShareOperation.PERMISSION)
@GetMapping(value = "/permissions/{token}")
public ResponseEntity preFlight(@PathVariable(name = "token") String token,
@RequestParam(required = false) String password,
@RequestParam String type,
@ApiIgnore User user,
HttpServletRequest request) {
Map<String, Object> result = shareService.getSharePermissions();
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap().success().payload(result));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(result));
}
}

/**
* share页登录
Expand All @@ -78,23 +99,18 @@ public class ShareController extends BaseController {
* @return
*/
@ApiOperation(value = "share login")
@AuthIgnore
@AuthShare(type = ShareType.LOGIN)
@PostMapping("/login/{token}")
public ResponseEntity shareLogin(@PathVariable String token,
@Valid @RequestBody UserLogin userLogin,
@ApiIgnore BindingResult bindingResult) {

if (StringUtils.isEmpty(token)) {
ResultMap resultMap = new ResultMap().fail().message("Invalid token");
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

if (bindingResult.hasErrors()) {
ResultMap resultMap = new ResultMap().fail().message(bindingResult.getFieldErrors().get(0).getDefaultMessage());
return ResponseEntity.status(resultMap.getCode()).body(resultMap);
}

User user = shareService.shareLogin(token, userLogin);
User user = shareService.shareLogin(userLogin);
return ResponseEntity.ok(new ResultMap().success(tokenUtils.generateToken(user)).payload(new UserLoginResult(user)));
}

Expand All @@ -115,7 +131,7 @@ public ResponseEntity getShareDashboard(@PathVariable String token,
HttpServletRequest request) {
ShareDashboard shareDashboard = shareService.getShareDashboard(user);

if (null == user) {
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap().success().payload(shareDashboard));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareDashboard));
Expand All @@ -139,7 +155,7 @@ public ResponseEntity getShareDisplay(@PathVariable String token,
HttpServletRequest request) {
ShareDisplay shareDisplay = shareService.getShareDisplay(user);

if (null == user) {
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap().success().payload(shareDisplay));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareDisplay));
Expand All @@ -163,7 +179,7 @@ public ResponseEntity getShareWidget(@PathVariable String token,
HttpServletRequest request) {
ShareWidget shareWidget = shareService.getShareWidget(user);

if (null == user) {
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap().success().payload(shareWidget));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareWidget));
Expand All @@ -189,7 +205,7 @@ public ResponseEntity getShareData(@PathVariable String token,
HttpServletRequest request) throws SQLException {

Paginate<Map<String, Object>> shareData = shareService.getShareData(executeParam, user);
if (null == user) {
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap().success().payload(shareData));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payload(shareData));
Expand Down Expand Up @@ -221,7 +237,7 @@ public ResponseEntity getDistinctValue(@PathVariable("token") String token,


List<Map<String, Object>> resultList = shareService.getDistinctValue(viewId, param, user);
if (null == user) {
if (null == user || user.getId() == null) {
return ResponseEntity.ok(new ResultMap().success().payloads(resultList));
} else {
return ResponseEntity.ok(new ResultMap(tokenUtils).successAndRefreshToken(request).payloads(resultList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,26 @@ public interface ShareDownloadService {
*
* @param downloadType
* @param uuid
* @param token
* @param user
* @param downloadViewExecuteParams
* @return
*/
boolean submit(DownloadType downloadType, String uuid, String token, User user, List<DownloadViewExecuteParam> downloadViewExecuteParams);
boolean submit(DownloadType downloadType, String uuid, List<DownloadViewExecuteParam> downloadViewExecuteParams);

/**
* 获取分享下载列表
*
* @param uuid
* @param token
* @param user
* @return
*/
List<ShareDownloadRecord> queryDownloadRecordPage(String uuid, String token, User user);
List<ShareDownloadRecord> queryDownloadRecordPage(String uuid);


/**
* 下载
*
* @param id
* @param uuid
* @param token
* @param user
* @return
*/
ShareDownloadRecord downloadById(String id, String uuid, String token, User user);
ShareDownloadRecord downloadById(String id, String uuid);
}
6 changes: 5 additions & 1 deletion server/src/main/java/edp/davinci/service/ShareService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface ShareService {

String generateShareToken(Long shareEntityId, String username, Long userId) throws ServerException;

User shareLogin(String token, UserLogin userLogin) throws NotFoundException, ServerException, UnAuthorizedExecption;
User shareLogin(UserLogin userLogin) throws NotFoundException, ServerException, UnAuthorizedExecption;

ShareDisplay getShareDisplay(User user) throws NotFoundException, ServerException, ForbiddenExecption, UnAuthorizedExecption;

Expand All @@ -53,6 +53,10 @@ public interface ShareService {

void formatShareParam(Long projectId, ShareEntity entity);

Map<String, Object> checkShareToken() throws ServerException, ForbiddenExecption;

Map<String, Object> getSharePermissions() throws ServerException, ForbiddenExecption;

@Deprecated
ShareInfo getShareInfo(String token, User user) throws ServerException, ForbiddenExecption;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import edp.core.utils.CollectionUtils;
import edp.core.utils.DateUtils;
import edp.core.utils.TokenUtils;
import edp.davinci.core.common.ErrorMsg;
import edp.davinci.core.enums.DownloadType;
import edp.davinci.dao.DashboardMapper;
import edp.davinci.dao.MemDashboardWidgetMapper;
Expand Down Expand Up @@ -79,7 +80,7 @@ protected List<WidgetContext> getWidgetContextListByDashBoardId(List<Long> dashb
return widgetList;
}
for (Long dashboardId : dashboardIds) {
if (dashboardId == null || dashboardId.longValue() <= 0) {
if (dashboardId == null || dashboardId <= 0) {
continue;
}
Dashboard dashboard = dashboardMapper.getById(dashboardId);
Expand All @@ -90,7 +91,7 @@ protected List<WidgetContext> getWidgetContextListByDashBoardId(List<Long> dashb
if (CollectionUtils.isEmpty(mdw)) {
continue;
}
Set<Long> widgetIds = mdw.stream().filter(y -> y != null).map(y -> y.getWidgetId()).collect(Collectors.toSet());
Set<Long> widgetIds = mdw.stream().filter(Objects::nonNull).map(MemDashboardWidget::getWidgetId).collect(Collectors.toSet());
List<Widget> widgets = widgetMapper.getByIds(widgetIds);
if (!CollectionUtils.isEmpty(widgets)) {
// order by mem_dashboard_widget create_time
Expand Down Expand Up @@ -129,7 +130,7 @@ protected List<WidgetContext> getWidgetContextListByFolderDashBoardId(Long id) {
if (CollectionUtils.isEmpty(dashboardList)) {
return widgetList;
}
List<Long> dashboardIds = dashboardList.stream().filter(x -> x != null).map(x -> x.getId()).collect(Collectors.toList());
List<Long> dashboardIds = dashboardList.stream().filter(Objects::nonNull).map(Dashboard::getId).collect(Collectors.toList());
if (CollectionUtils.isEmpty(dashboardIds)) {
return widgetList;
}
Expand Down Expand Up @@ -166,8 +167,7 @@ protected List<WidgetContext> getWidgetContexts(DownloadType downloadType, Long
executeParam = params.stream()
.filter(p -> null != p.getParam() && p.getId().equals(widget.getId())).findFirst()
.get().getParam();
}
catch (Exception e) {
} catch (Exception e) {
// ignore
}
}
Expand All @@ -190,15 +190,15 @@ protected List<WidgetContext> getWidgetContexts(DownloadType downloadType, Long
throw new IllegalArgumentException("unsupported DownloadType=" + downloadType.name());
}
if (CollectionUtils.isEmpty(widgetList)) {
throw new IllegalArgumentException("has no widget to download");
throw new IllegalArgumentException("there is no widget to download");
}
for (WidgetContext context : widgetList) {
ProjectDetail projectDetail = projectService.getProjectDetail(context.getWidget().getProjectId(), user, false);
ProjectPermission projectPermission = projectService.getProjectPermission(projectDetail, user);
//校验权限
if (!projectPermission.getDownloadPermission()) {
log.info("user {} have not permisson to download the widget {}", user.getUsername(), id);
throw new UnAuthorizedExecption("you have not permission to download the widget");
log.info("user {} have not permission to download the widget {}", user.getUsername(), id);
throw new UnAuthorizedExecption(ErrorMsg.ERR_MSG_PERMISSION);
}
context.setIsMaintainer(projectService.isMaintainer(projectDetail, user));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import edp.davinci.core.enums.DownloadTaskStatus;
import edp.davinci.core.enums.DownloadType;
import edp.davinci.dao.ShareDownloadRecordMapper;
import edp.davinci.dto.shareDto.ShareInfo;
import edp.davinci.dto.viewDto.DownloadViewExecuteParam;
import edp.davinci.model.ShareDownloadRecord;
import edp.davinci.model.User;
Expand All @@ -36,6 +35,8 @@
import edp.davinci.service.excel.MsgWrapper;
import edp.davinci.service.excel.WidgetContext;
import edp.davinci.service.excel.WorkBookContext;
import edp.davinci.service.share.ShareFactor;
import edp.davinci.service.share.aspect.ShareAuthAspect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -54,15 +55,14 @@ public class ShareDownloadServiceImpl extends DownloadCommonService implements S
private ShareService shareService;

@Override
public boolean submit(DownloadType downloadType, String uuid, String token, User user, List<DownloadViewExecuteParam> params) {
ShareInfo shareInfo = shareService.getShareInfo(token, user);

public boolean submit(DownloadType downloadType, String uuid, List<DownloadViewExecuteParam> params) {
ShareFactor shareFactor = ShareAuthAspect.SHARE_FACTOR_THREAD_LOCAL.get();
try {
List<WidgetContext> widgetList = getWidgetContexts(downloadType, shareInfo.getShareId(), shareInfo.getShareUser(), params);
List<WidgetContext> widgetList = getWidgetContexts(downloadType, shareFactor.getEntityId(), shareFactor.getUser(), params);

ShareDownloadRecord record = new ShareDownloadRecord();
record.setUuid(uuid);
record.setName(getDownloadFileName(downloadType, shareInfo.getShareId()));
record.setName(getDownloadFileName(downloadType, shareFactor.getEntityId()));
record.setStatus(DownloadTaskStatus.PROCESSING.getStatus());
record.setCreateTime(new Date());
shareDownloadRecordMapper.insertSelective(record);
Expand All @@ -71,7 +71,7 @@ public boolean submit(DownloadType downloadType, String uuid, String token, User
WorkBookContext workBookContext = WorkBookContext.WorkBookContextBuilder.newBuildder()
.withWrapper(wrapper)
.withWidgets(widgetList)
.withUser(shareInfo.getShareUser())
.withUser(shareFactor.getUser())
.withResultLimit(resultLimit)
.withTaskKey("ShareDownload_" + uuid)
.build();
Expand All @@ -88,23 +88,15 @@ public boolean submit(DownloadType downloadType, String uuid, String token, User


@Override
public List<ShareDownloadRecord> queryDownloadRecordPage(String uuid, String token, User user) {
shareService.getShareInfo(token, user);

public List<ShareDownloadRecord> queryDownloadRecordPage(String uuid) {
// ShareAuthAspect.SHARE_FACTOR_THREAD_LOCAL.get();
return shareDownloadRecordMapper.getShareDownloadRecordsByUuid(uuid);
}

@Override
public ShareDownloadRecord downloadById(String id, String uuid, String token, User user) {
public ShareDownloadRecord downloadById(String id, String uuid) {
//share download 只校验token是否正确,不校验权限,走分享人权限
try {
shareService.getShareInfo(token, user);
} catch (ServerException e) {
throw e;
} catch (ForbiddenExecption e) {
log.warn("auth share download: record: [id: {}, uuid: {}] type", id, uuid);
}

// ShareAuthAspect.SHARE_FACTOR_THREAD_LOCAL.get();
ShareDownloadRecord record = shareDownloadRecordMapper.getShareDownloadRecordBy(Long.valueOf(id), uuid);

if (record != null) {
Expand Down
Loading