dnote/pkg/server/permissions/plan.go
Sung Won Cho 5d83a4dff0 wip
2019-11-28 13:47:07 +08:00

40 lines
1,009 B
Go

package permissions
import (
"github.com/dnote/dnote/pkg/server/database"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
// CheckPlanAllowance checks if the given user is within the allowance for the
// current plan.
func CheckPlanAllowance(db *gorm.DB, user database.User) (bool, error) {
if user.Cloud {
return true, nil
}
var bookCount int
if err := db.Model(database.Book{}).Where("user_id = ? AND NOT deleted", user.ID).Count(&bookCount).Error; err != nil {
return false, errors.Wrap(err, "checking plan threshold")
}
if bookCount > 5 {
return false, nil
}
return true, nil
}
// CanCreateBook checks if the given user can create a book
func CanCreateBook(db *gorm.DB, user database.User) (bool, error) {
if user.Cloud {
return true, nil
}
var bookCount int
if err := db.Model(database.Book{}).Where("user_id = ? AND NOT deleted", user.ID).Count(&bookCount).Error; err != nil {
return false, errors.Wrap(err, "checking plan threshold")
}
return bookCount < 5, nil
}