/
acl.go
62 lines (45 loc) · 1.36 KB
/
acl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package services
import (
"github.com/gelleson/packup/internal/core/constants"
"github.com/gelleson/packup/internal/core/models"
"github.com/gelleson/packup/pkg/database"
"github.com/pkg/errors"
)
type groupService interface {
Exist(id uint) bool
}
type AclService struct {
db *database.Database
groupService groupService
}
func NewAclService(db *database.Database, groupService groupService) *AclService {
return &AclService{db: db, groupService: groupService}
}
func (a AclService) Create(r models.Rule) (models.Rule, error) {
if !a.groupService.Exist(r.GroupID) {
return models.Rule{}, errors.New("group doesn't exist")
}
rule := models.Rule{
Resource: r.Resource,
Operation: r.Operation,
GroupID: r.GroupID,
}
if tx := a.db.Conn().Create(&rule); tx.Error != nil {
return models.Rule{}, tx.Error
}
return rule, nil
}
func (a AclService) Can(groupId uint, operation models.Operation, resource models.Resource) bool {
rule := models.Rule{}
if tx := a.db.Conn().Where("group_id = ? and operation = ? and resource = ?", groupId, operation, resource).First(&rule); tx.Error != nil {
return false
}
return rule.ID != 0
}
func (a AclService) HasDefaultRules() bool {
rule := models.Rule{}
if tx := a.db.Conn().Where("group_id = ? ", constants.DefaultGroupId).First(&rule); tx.Error != nil {
return false
}
return rule.ID != 0
}