Beispiel #1
0
func (l *loginUser) CheckValid() error {
	if strings.Trim(l.UserName, " ") == "" {
		return utils.NewError("username required")
	}

	if strings.Trim(l.Password, " ") == "" {
		return utils.NewError("password required")
	}

	return nil
}
Beispiel #2
0
func (a *Account) Register(c *gin.Context) {
	var user models.User
	if err := c.BindJSON(&user); err != nil {
		c.JSON(400, utils.NewError(err.Error()))
		return
	}

	if err := models.CreateUser(&user); err != nil {
		c.JSON(400, utils.NewError(err.Error()))
		return
	}

	c.JSON(201, user)

}
Beispiel #3
0
// 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,
	})

}
Beispiel #4
0
func (a *Account) Login(c *gin.Context) {
	var param loginUser
	err := c.BindJSON(&param)
	if err != nil {
		c.JSON(400, utils.NewInvalidJsonError())
		return
	}

	if err := param.CheckValid(); err != nil {
		c.JSON(400, err)
		return
	}

	user := models.GetUserByUserName(param.UserName)
	if user == nil {
		c.JSON(400, utils.NewError("user not exist"))
		return
	}

	if !user.IsValidPassword(param.Password) {
		c.JSON(400, utils.NewError("invalid password"))
		return
	}

	login := models.NewLogin(a.Config.TokenSecret, a.Config.TokenExpiredIn)
	token, err := login.GetToken(user)
	if err != nil {
		c.JSON(400, utils.NewError("gen token failed - %d -%s", user.Id, err.Error()))
		return
	}

	user.Token = token
	user.ExpiredIn = a.Config.TokenExpiredIn

	c.JSON(200, user)

}
Beispiel #5
0
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)

}
Beispiel #6
0
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)

}
Beispiel #7
0
// 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)

}
Beispiel #8
0
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)

}