コード例 #1
0
ファイル: user.go プロジェクト: VincentYu/server
// disable user
func (u *User) Active(c *gin.Context) {
	if !isGranted(c, "user.active") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}

	id := paramInt64(c, "id")
	var user models.User
	var data models.User
	if err := c.BindJSON(&data); err != nil {
		c.JSON(400, utils.NewInvalidJsonError())
		return
	}

	if err := models.GetById(id, &user); err != nil {
		c.JSON(400, utils.NewNotFoundError())
		return
	}

	fmt.Println("is active", data.IsActive)
	user.IsActive = data.IsActive
	if err := models.UpdateById(id, &user, "is_active"); err != nil {
		c.JSON(400, utils.NewError("update database failed - %s", err.Error()))
		return
	}

	c.JSON(200, gin.H{
		"success": true,
	})

}
コード例 #2
0
ファイル: user.go プロジェクト: VincentYu/server
func (u *User) Get(c *gin.Context) {
	if !isGranted(c, "user.list") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}
	id := paramInt64(c, "id")

	var user models.User
	if err := models.GetById(id, &user); err != nil {
		c.JSON(400, utils.NewNotFoundError())
		return
	}

	c.JSON(200, user)

}
コード例 #3
0
ファイル: user.go プロジェクト: VincentYu/server
func (u *User) Roles(c *gin.Context) {
	if !isGranted(c, "user.active") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}
	id := paramInt64(c, "id")

	user := models.User{Id: id}
	roles, err := user.Roles()
	if err != nil {
		c.JSON(400, utils.NewError("get user roles failed - %s", err.Error()))
		return
	}

	c.JSON(200, roles)

}
コード例 #4
0
ファイル: file.go プロジェクト: VincentYu/server
func (f *File) Find(c *gin.Context) {
	if !isGranted(c, "file.list") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}

	page := queryInt(c, "page", 1)
	pagesize := queryInt(c, "pagesize", 30)
	userid := queryInt64(c, "user_id", 0)
	ext := c.Query("ext")
	name := c.Query("name")

	engine := models.Engine()
	session := func() *xorm.Session {
		where := engine.Where("")
		if userid > 0 {
			where = where.Where("user_id = ?", userid)
		}

		if ext != "" {
			exts := strings.Split(ext, ",")
			where = where.In("ext", exts)
		}

		if name != "" {
			where = where.Where("name like ?", "%"+name+"%")
		}

		return where
	}

	total, _ := session().Count(&models.File{})
	files := make([]models.File, 0)

	if err := session().Limit(pagesize, pagesize*(page-1)).Desc("created_at").Find(&files); err != nil {
		c.JSON(400, utils.NewError("find files failed - %s", err.Error()))
		return
	}

	result := models.NewQueryResult(page, pagesize, total, files)

	c.JSON(200, result)

}
コード例 #5
0
ファイル: user.go プロジェクト: VincentYu/server
// liist all user
// is_active 1/0
func (u *User) List(c *gin.Context) {
	if !isGranted(c, "user.list") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}

	page := queryInt(c, "page", 1)
	pagesize := queryInt(c, "pagesize", 30)
	isActive := strings.ToLower(c.Query("is_active"))

	engine := models.Engine()
	session := func() *xorm.Session {
		where := engine.Where("nickname like ? and username like ? and email like ? ",
			"%"+c.Query("nickname")+"%", "%"+c.Query("username")+"%", "%"+c.Query("email")+"%")

		if isActive != "" {
			if isActive == "1" || isActive == "true" {
				where = where.Where("is_active = ?", true)
			} else if isActive == "0" || isActive == "false" {
				where = where.Where("is_active = ?", false)
			}
		}

		return where
	}

	total, _ := session().Count(&models.User{})
	users := make([]models.User, 0)

	if err := session().Limit(pagesize, pagesize*(page-1)).Asc("created_at").Find(&users); err != nil {
		c.JSON(400, utils.NewError("find users failed - %s", err.Error()))
		return
	}

	result := models.NewQueryResult(page, pagesize, total, users)

	c.JSON(200, result)

}
コード例 #6
0
ファイル: file.go プロジェクト: VincentYu/server
func (f *File) Upload(c *gin.Context) {
	if !isGranted(c, "file.upload") {
		c.JSON(403, utils.NewNoAccessPermissionError(""))
		return
	}
	cu := currentUser(c)
	file, header, err := c.Request.FormFile("file")
	if err != nil {
		c.JSON(400, utils.NewError("read file content failed - %s", err.Error()))
		return
	}

	ext := filepath.Ext(header.Filename)
	filename := fmt.Sprintf("%v%v%s", cu.Id, time.Now().UnixNano(), ext)
	filefolder := "/" + time.Now().Format("20060102")
	path := f.Config.File.UploadPath + filefolder

	buf, err := ioutil.ReadAll(file)
	if err != nil {
		c.JSON(400, utils.NewError("read file content failed - %s", err.Error()))
		return
	}

	fileModel := models.File{}
	fileModel.Key = filename
	fileModel.Name = header.Filename
	fileModel.Path = filefolder
	fileModel.Size = len(buf)
	fileModel.UserId = cu.Id
	fileModel.Ext = ext

	if err := fileModel.CheckValid(); err != nil {
		c.JSON(400, utils.NewError("upload file failed - %s", err.Error()))
		return
	}

	if _, err := os.Stat(path); err != nil {
		os.MkdirAll(path, os.ModePerm)
	}

	fmt.Println("file path:", path)

	x := models.Engine()
	s := x.NewSession()
	s.Begin()

	if _, err := s.Insert(&fileModel); err != nil {
		c.JSON(400, utils.NewError("write database failed - %s", err.Error()))
		return
	}

	if err := ioutil.WriteFile(path+"/"+filename, buf, os.ModePerm); err != nil {
		s.Rollback()
		c.JSON(400, utils.NewError("write file failed - %s", err.Error()))
		return
	}

	s.Commit()

	c.JSON(200, fileModel)

}