mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-14 22:26:56 +08:00
学员学习权限修改
This commit is contained in:
parent
7c84e185a6
commit
e13ff1ae25
@ -97,8 +97,7 @@ public class HourController {
|
|||||||
public JsonResponse play(
|
public JsonResponse play(
|
||||||
@PathVariable(name = "courseId") Integer courseId,
|
@PathVariable(name = "courseId") Integer courseId,
|
||||||
@PathVariable(name = "id") Integer id) {
|
@PathVariable(name = "id") Integer id) {
|
||||||
Course course = courseCache.findOrFail(courseId);
|
userCanSeeCourseCache.check(FCtx.getId(), courseId, true);
|
||||||
userCanSeeCourseCache.check(FCtx.getUser(), course, true);
|
|
||||||
CourseHour hour = hourService.findOrFail(id, courseId);
|
CourseHour hour = hourService.findOrFail(id, courseId);
|
||||||
Resource resource = resourceService.findOrFail(hour.getRid());
|
Resource resource = resourceService.findOrFail(hour.getRid());
|
||||||
|
|
||||||
@ -121,9 +120,8 @@ public class HourController {
|
|||||||
return JsonResponse.error("duration参数错误");
|
return JsonResponse.error("duration参数错误");
|
||||||
}
|
}
|
||||||
|
|
||||||
Course course = courseCache.findOrFail(courseId);
|
|
||||||
CourseHour hour = hourService.findOrFail(id, courseId);
|
CourseHour hour = hourService.findOrFail(id, courseId);
|
||||||
userCanSeeCourseCache.check(FCtx.getUser(), course, true);
|
userCanSeeCourseCache.check(FCtx.getId(), courseId, true);
|
||||||
|
|
||||||
// 获取锁
|
// 获取锁
|
||||||
String lockKey = String.format("record:%d", FCtx.getId());
|
String lockKey = String.format("record:%d", FCtx.getId());
|
||||||
@ -135,11 +133,7 @@ public class HourController {
|
|||||||
try {
|
try {
|
||||||
boolean isFinished =
|
boolean isFinished =
|
||||||
userCourseHourRecordService.storeOrUpdate(
|
userCourseHourRecordService.storeOrUpdate(
|
||||||
FCtx.getId(),
|
FCtx.getId(), courseId, hour.getId(), duration, hour.getDuration());
|
||||||
course.getId(),
|
|
||||||
hour.getId(),
|
|
||||||
duration,
|
|
||||||
hour.getDuration());
|
|
||||||
if (isFinished) {
|
if (isFinished) {
|
||||||
ctx.publishEvent(
|
ctx.publishEvent(
|
||||||
new UserCourseHourFinishedEvent(
|
new UserCourseHourFinishedEvent(
|
||||||
@ -158,9 +152,7 @@ public class HourController {
|
|||||||
public JsonResponse ping(
|
public JsonResponse ping(
|
||||||
@PathVariable(name = "courseId") Integer courseId,
|
@PathVariable(name = "courseId") Integer courseId,
|
||||||
@PathVariable(name = "id") Integer id) {
|
@PathVariable(name = "id") Integer id) {
|
||||||
Course course = courseCache.findOrFail(courseId);
|
userCanSeeCourseCache.check(FCtx.getId(), courseId, true);
|
||||||
CourseHour hour = hourService.findOrFail(id, courseId);
|
|
||||||
userCanSeeCourseCache.check(FCtx.getUser(), course, true);
|
|
||||||
|
|
||||||
// 获取锁
|
// 获取锁
|
||||||
String lockKey = String.format("ping:%d", FCtx.getId());
|
String lockKey = String.format("ping:%d", FCtx.getId());
|
||||||
@ -183,7 +175,7 @@ public class HourController {
|
|||||||
|
|
||||||
ctx.publishEvent(
|
ctx.publishEvent(
|
||||||
new UserLearnCourseUpdateEvent(
|
new UserLearnCourseUpdateEvent(
|
||||||
this, FCtx.getId(), course.getId(), hour.getId(), lastTime, curTime));
|
this, FCtx.getId(), courseId, id, lastTime, curTime));
|
||||||
} finally {
|
} finally {
|
||||||
// 此处未考虑上面代码执行失败释放锁
|
// 此处未考虑上面代码执行失败释放锁
|
||||||
redisDistributedLock.releaseLock(lockKey);
|
redisDistributedLock.releaseLock(lockKey);
|
||||||
|
@ -19,11 +19,14 @@ import org.apache.commons.collections4.CollectionUtils;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import xyz.playedu.common.domain.User;
|
import xyz.playedu.common.domain.Department;
|
||||||
|
import xyz.playedu.common.service.DepartmentService;
|
||||||
import xyz.playedu.common.service.UserService;
|
import xyz.playedu.common.service.UserService;
|
||||||
import xyz.playedu.course.domain.Course;
|
import xyz.playedu.common.util.StringUtil;
|
||||||
import xyz.playedu.course.service.CourseService;
|
import xyz.playedu.course.service.CourseService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,34 +41,36 @@ public class UserBus {
|
|||||||
|
|
||||||
@Autowired private UserService userService;
|
@Autowired private UserService userService;
|
||||||
|
|
||||||
public boolean canSeeCourse(User user, Course course) {
|
@Autowired private DepartmentService departmentService;
|
||||||
List<Integer> courseDepIds = courseService.getDepIdsByCourseId(course.getId());
|
|
||||||
if (courseDepIds == null || courseDepIds.size() == 0) {
|
public boolean canSeeCourse(Integer userId, Integer courseId) {
|
||||||
// 线上课无所属部门=>任何学员都可以学习
|
List<Integer> courseDepIds = courseService.getDepIdsByCourseId(courseId);
|
||||||
|
if (StringUtil.isEmpty(courseDepIds)) {
|
||||||
|
// 线上课全部部门=>任何学员都可以学习
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
List<Integer> userDepIds = userService.getDepIdsByUserId(user.getId());
|
|
||||||
if (userDepIds == null || userDepIds.size() == 0) {
|
// 获取学员所属部门以及所有父级部门
|
||||||
|
List<Integer> allDepIds = new ArrayList<>();
|
||||||
|
List<Integer> userDepIds = userService.getDepIdsByUserId(userId);
|
||||||
|
if (StringUtil.isNotEmpty(userDepIds)) {
|
||||||
|
List<Department> departmentList = departmentService.chunk(userDepIds);
|
||||||
|
if (StringUtil.isNotEmpty(departmentList)) {
|
||||||
|
for (Department dep : departmentList) {
|
||||||
|
allDepIds.add(dep.getId());
|
||||||
|
if (StringUtil.isNotEmpty(dep.getParentChain())) {
|
||||||
|
allDepIds.addAll(
|
||||||
|
Arrays.stream(dep.getParentChain().split(","))
|
||||||
|
.map(Integer::valueOf)
|
||||||
|
.toList());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtil.isEmpty(allDepIds)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return CollectionUtils.intersection(courseDepIds, userDepIds).size() > 0;
|
return CollectionUtils.intersection(courseDepIds, allDepIds).size() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注意,调用该方法需要考虑到并发写入问题
|
|
||||||
/* public void userLearnDurationRecord(User user, Course course, CourseHour hour) {
|
|
||||||
Long curTime = System.currentTimeMillis();
|
|
||||||
|
|
||||||
// 最近一次学习时间
|
|
||||||
Long lastTime = userLastLearnTimeCache.get(FCtx.getId());
|
|
||||||
// 最大周期为10s+0.5s的网络延迟
|
|
||||||
if (lastTime == null || curTime - lastTime > 10500) {
|
|
||||||
lastTime = curTime - 10000;
|
|
||||||
}
|
|
||||||
|
|
||||||
userLastLearnTimeCache.put(user.getId(), curTime);
|
|
||||||
|
|
||||||
ctx.publishEvent(
|
|
||||||
new UserLearnCourseUpdateEvent(
|
|
||||||
this, user.getId(), course.getId(), hour.getId(), lastTime, curTime));
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
@ -18,11 +18,13 @@ package xyz.playedu.course.caches;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import xyz.playedu.common.domain.User;
|
|
||||||
import xyz.playedu.common.exception.ServiceException;
|
import xyz.playedu.common.exception.ServiceException;
|
||||||
import xyz.playedu.common.util.RedisUtil;
|
import xyz.playedu.common.util.RedisUtil;
|
||||||
|
import xyz.playedu.common.util.StringUtil;
|
||||||
import xyz.playedu.course.bus.UserBus;
|
import xyz.playedu.course.bus.UserBus;
|
||||||
import xyz.playedu.course.domain.Course;
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author 杭州白书科技有限公司
|
* @Author 杭州白书科技有限公司
|
||||||
@ -38,14 +40,15 @@ public class UserCanSeeCourseCache {
|
|||||||
|
|
||||||
private static final int expire = 3600; // s
|
private static final int expire = 3600; // s
|
||||||
|
|
||||||
public boolean check(User user, Course course, boolean isThrow) throws ServiceException {
|
public boolean check(Integer userId, Integer courseId, boolean isThrow)
|
||||||
|
throws ServiceException {
|
||||||
boolean result;
|
boolean result;
|
||||||
if (RedisUtil.exists(key(user, course))) {
|
if (RedisUtil.exists(key(userId, courseId))) {
|
||||||
String cacheResult = (String) RedisUtil.get(key(user, course));
|
String cacheResult = (String) RedisUtil.get(key(userId, courseId));
|
||||||
result = "1".equals(cacheResult);
|
result = "1".equals(cacheResult);
|
||||||
} else {
|
} else {
|
||||||
result = userBus.canSeeCourse(user, course);
|
result = userBus.canSeeCourse(userId, courseId);
|
||||||
put(user, course, result);
|
put(userId, courseId, result);
|
||||||
}
|
}
|
||||||
if (!result && isThrow) {
|
if (!result && isThrow) {
|
||||||
throw new ServiceException("无权限观看");
|
throw new ServiceException("无权限观看");
|
||||||
@ -53,11 +56,21 @@ public class UserCanSeeCourseCache {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(User user, Course course, boolean result) {
|
public void put(Integer userId, Integer courseId, boolean result) {
|
||||||
RedisUtil.set(key(user, course), result ? "1" : "0", expire);
|
RedisUtil.set(key(userId, courseId), result ? "1" : "0", expire);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String key(User user, Course course) {
|
public void destroy(List<Integer> userIds, Integer courseId) {
|
||||||
return String.format(keyTemplate, course.getId(), user.getId());
|
if (StringUtil.isNotEmpty(userIds)) {
|
||||||
|
List<String> keyList = new ArrayList<>();
|
||||||
|
for (Integer userId : userIds) {
|
||||||
|
keyList.add(key(userId, courseId));
|
||||||
|
}
|
||||||
|
RedisUtil.del(keyList.toArray(new String[keyList.size()]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String key(Integer userId, Integer courseId) {
|
||||||
|
return String.format(keyTemplate, courseId, userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user