mirror of
https://github.com/dnote/dnote
synced 2026-03-16 23:45:52 +01:00
40 lines
1,009 B
Go
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
|
|
}
|