加入权限管理

This commit is contained in:
marvzhang
2020-03-20 17:43:11 +08:00
parent 1e2b580ed4
commit fa62e1a2dc
26 changed files with 437 additions and 70 deletions

View File

@@ -4,6 +4,7 @@ import (
"crawlab/constants"
"crawlab/database"
"crawlab/model"
"crawlab/services"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo/bson"
"net/http"
@@ -18,8 +19,11 @@ func GetProjectList(c *gin.Context) {
query["tags"] = tag
}
// 获取校验
query = services.GetAuthQuery(query, c)
// 获取列表
projects, err := model.GetProjectList(query, 0, "+_id")
projects, err := model.GetProjectList(query, "+_id")
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
@@ -74,6 +78,9 @@ func PutProject(c *gin.Context) {
return
}
// UserId
p.UserId = services.GetCurrentUserId(c)
if err := p.Add(); err != nil {
HandleError(http.StatusInternalServerError, c, err)
return

View File

@@ -9,7 +9,12 @@ import (
)
func GetScheduleList(c *gin.Context) {
results, err := model.GetScheduleList(nil)
query := bson.M{}
// 获取校验
query = services.GetAuthQuery(query, c)
results, err := model.GetScheduleList(query)
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return

View File

@@ -29,13 +29,14 @@ import (
// ======== 爬虫管理 ========
func GetSpiderList(c *gin.Context) {
pageNum, _ := c.GetQuery("page_num")
pageSize, _ := c.GetQuery("page_size")
keyword, _ := c.GetQuery("keyword")
pid, _ := c.GetQuery("project_id")
t, _ := c.GetQuery("type")
sortKey, _ := c.GetQuery("sort_key")
sortDirection, _ := c.GetQuery("sort_direction")
pageNum := c.Query("page_num")
pageSize := c.Query("page_size")
keyword := c.Query("keyword")
pid := c.Query("project_id")
t := c.Query("type")
sortKey := c.Query("sort_key")
sortDirection := c.Query("sort_direction")
ownerType := c.Query("owner_type")
// 筛选-名称
filter := bson.M{
@@ -65,6 +66,21 @@ func GetSpiderList(c *gin.Context) {
filter["project_id"] = bson.ObjectIdHex(pid)
}
// 筛选-用户
if ownerType == constants.OwnerTypeAll {
user := services.GetCurrentUser(c)
if user.Role == constants.RoleNormal {
filter["$or"] = []bson.M{
{"user_id": services.GetCurrentUserId(c)},
{"is_public": true},
}
}
} else if ownerType == constants.OwnerTypeMe {
filter["user_id"] = services.GetCurrentUserId(c)
} else if ownerType == constants.OwnerTypePublic {
filter["is_public"] = true
}
// 排序
sortStr := "-_id"
if sortKey != "" && sortDirection != "" {
@@ -815,7 +831,7 @@ func GetSpiderStats(c *gin.Context) {
overview.AvgWaitDuration = overview.TotalWaitDuration / taskCount
overview.AvgRuntimeDuration = overview.TotalRuntimeDuration / taskCount
items, err := model.GetDailyTaskStats(bson.M{"spider_id": spider.Id})
items, err := model.GetDailyTaskStats(bson.M{"spider_id": spider.Id, "user_id": bson.M{"user_id": services.GetCurrentUserId(c)}})
if err != nil {
log.Errorf(err.Error())
HandleError(http.StatusInternalServerError, c, err)

View File

@@ -3,6 +3,7 @@ package routes
import (
"crawlab/constants"
"crawlab/model"
"crawlab/services"
"github.com/gin-gonic/gin"
"github.com/globalsign/mgo/bson"
"net/http"
@@ -22,7 +23,7 @@ func GetHomeStats(c *gin.Context) {
}
// 任务总数
taskCount, err := model.GetTaskCount(nil)
taskCount, err := model.GetTaskCount(bson.M{"user_id": services.GetCurrentUserId(c)})
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
@@ -36,21 +37,21 @@ func GetHomeStats(c *gin.Context) {
}
// 爬虫总数
spiderCount, err := model.GetSpiderCount()
spiderCount, err := model.GetSpiderCount(bson.M{"user_id": services.GetCurrentUserId(c)})
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
}
// 定时任务数
scheduleCount, err := model.GetScheduleCount()
scheduleCount, err := model.GetScheduleCount(bson.M{"user_id": services.GetCurrentUserId(c)})
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return
}
// 每日任务数
items, err := model.GetDailyTaskStats(bson.M{})
items, err := model.GetDailyTaskStats(bson.M{"user_id": services.GetCurrentUserId(c)})
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return

View File

@@ -47,11 +47,14 @@ func GetTaskList(c *gin.Context) {
if data.SpiderId != "" {
query["spider_id"] = bson.ObjectIdHex(data.SpiderId)
}
//新增根据任务状态获取task列表
// 根据任务状态获取task列表
if data.Status != "" {
query["status"] = data.Status
}
// 获取校验
query = services.GetAuthQuery(query, c)
// 获取任务列表
tasks, err := model.GetTaskList(query, (data.PageNum-1)*data.PageSize, data.PageSize, "-create_ts")
if err != nil {