-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission.go
148 lines (132 loc) · 2.71 KB
/
permission.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package model
import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/bson/primitive"
"github.com/mongodb/mongo-go-driver/mongo"
log "github.com/sirupsen/logrus"
)
// Permission ...
type Permission struct {
Model `bson:",inline"`
Name string `bson:"name"`
Slug string `bson:"slug"`
Description string `bson:"description"`
PermissionModel string `bson:"permission_model"`
}
// IsExist ...
func (p *Permission) IsExist() bool {
if p.ID != primitive.NilObjectID {
return IDExist(p)
}
return IsExist(p, bson.M{
"slug": p.Slug,
})
}
// CreateIfNotExist ...
func (p *Permission) CreateIfNotExist() error {
return CreateIfNotExist(p)
}
// NewPermission ...
func NewPermission() *Permission {
return &Permission{
Model: model(),
}
}
// GetID ...
func (p *Permission) GetID() primitive.ObjectID {
return p.ID
}
// SetID ...
func (p *Permission) SetID(id primitive.ObjectID) {
p.ID = id
}
// Create ...
func (p *Permission) Create() error {
return InsertOne(p)
}
// Update ...
func (p *Permission) Update() error {
return UpdateOne(p)
}
// Delete ...
func (p *Permission) Delete() error {
return DeleteByID(p)
}
// Find ...
func (p *Permission) Find() error {
if p.ID != primitive.NilObjectID {
return FindByID(p)
}
return FindOne(p, bson.M{
"slug": p.Slug,
})
}
// All ...
func (p *Permission) All() ([]*Permission, error) {
var permissions []*Permission
m := bson.M{}
err := Find(p, m, func(cursor mongo.Cursor) error {
log.Println(cursor.DecodeBytes())
var p Permission
err := cursor.Decode(&p)
if err != nil {
return err
}
permissions = append(permissions, &p)
return nil
})
return permissions, err
}
func (p *Permission) _Name() string {
return "permission"
}
// Roles ...
func (p *Permission) Roles() ([]*Role, error) {
var roles []*Role
pu := NewPermissionRole()
err := Find(pu, bson.M{
"role_id": pu.RoleID,
}, func(cursor mongo.Cursor) error {
pu := NewPermissionRole()
err := cursor.Decode(pu)
if err != nil {
return err
}
role, err := pu.Role()
if err != nil {
return err
}
roles = append(roles, role)
return nil
})
return roles, err
}
// Users ...
func (p *Permission) Users() ([]*User, error) {
var users []*User
pu := NewPermissionUser()
err := Find(pu, bson.M{
"user_id": pu.UserID,
}, func(cursor mongo.Cursor) error {
pu := NewPermissionUser()
err := cursor.Decode(pu)
if err != nil {
return err
}
user, err := pu.User()
if err != nil {
return err
}
users = append(users, user)
return nil
})
return users, err
}
// Menu ...
func (p *Permission) Menu() (*Menu, error) {
menu := NewMenu()
err := FindOne(menu, bson.M{
"slug": p.Slug,
})
return menu, err
}