-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.go
144 lines (121 loc) · 3.85 KB
/
entity.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"log"
"time"
"github.com/nats-io/go-nats"
"github.com/r3labs/natsdb"
)
// Entity : the database mapped entity
type Entity struct {
ID uint `json:"id" gorm:"primary_key"`
UserID string `json:"user_id" gorm:"unique_index:idx_uniq"`
ResourceID string `json:"resource_id" gorm:"unique_index:idx_uniq"`
ResourceType string `json:"resource_type" gorm:"unique_index:idx_uniq"`
Role string `json:"role"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `json:"-" sql:"index"`
}
// TableName : set Entity's table name to be authorization
func (Entity) TableName() string {
return "authorizations"
}
// Find : based on the defined fields for the current entity
// will perform a search on the database
func (e *Entity) Find() (list []interface{}) {
entities := []Entity{}
if e.Role == "" && e.ResourceID == "" && e.UserID == "" {
db.Find(&entities)
} else if e.Role == "" && e.ResourceID == "" && e.UserID != "" {
db.Where("user_id = ?", e.UserID).Find(&entities)
} else if e.UserID == "" {
db.Where("resource_id = ? AND resource_type = ?", e.ResourceID, e.ResourceType).Find(&entities)
} else if e.ResourceID == "" {
db.Where("user_id = ? AND resource_type = ?", e.UserID, e.ResourceType).Find(&entities)
} else if e.Role == "" {
db.Where("user_id = ? AND resource_id = ? AND resource_type = ?", e.UserID, e.ResourceID, e.ResourceType).Find(&entities)
} else {
db.Where("user_id = ? AND resource_id = ? AND resource_type = ? AND role = ?", e.UserID, e.ResourceID, e.ResourceType, e.Role).Find(&entities)
}
list = make([]interface{}, len(entities))
for i, s := range entities {
list[i] = s
}
return list
}
// MapInput : maps the input []byte on the current entity
func (e *Entity) MapInput(body []byte) {
if err := json.Unmarshal(body, &e); err != nil {
log.Println("Invalid input " + err.Error())
}
}
// HasID : determines if the current entity has an id or not
func (e *Entity) HasID() bool {
if e.ID == 0 {
return false
}
return true
}
// LoadFromInput : Will load from a []byte input the database stored entity
func (e *Entity) LoadFromInput(msg []byte) bool {
e.MapInput(msg)
var stored Entity
if e.ID > 0 {
db.Where("id = ?", e.ID).First(&stored)
} else {
db.Where("user_id = ? AND resource_id = ? AND resource_type = ? AND role = ?", e.UserID, e.ResourceID, e.ResourceType, e.Role).First(&stored)
}
if &stored == nil {
return false
}
if ok := stored.HasID(); !ok {
return false
}
e.ID = stored.ID
e.UserID = stored.UserID
e.ResourceID = stored.ResourceID
e.ResourceType = stored.ResourceType
e.Role = stored.Role
e.CreatedAt = stored.CreatedAt
e.UpdatedAt = stored.UpdatedAt
return true
}
// LoadFromInputOrFail : Will try to load from the input an existing entity,
// or will call the handler to Fail the nats message
func (e *Entity) LoadFromInputOrFail(msg *nats.Msg, h *natsdb.Handler) bool {
stored := &Entity{}
ok := stored.LoadFromInput(msg.Data)
if !ok {
h.Fail(msg)
}
*e = *stored
return ok
}
// Update : It will update the current entity with the input []byte
func (e *Entity) Update(body []byte) error {
e.MapInput(body)
stored := Entity{}
if e.ID > 0 {
db.Where("id = ?", e.ID).First(&stored)
} else {
db.Where("user_id = ? AND resource_id = ? AND resource_type = ?", e.UserID, e.ResourceID, e.ResourceType).First(&stored)
}
stored.Role = e.Role
db.Save(&stored)
e = &stored
return nil
}
// Delete : Will delete from database the current Entity
func (e *Entity) Delete() error {
db.Unscoped().Delete(&e)
return nil
}
// Save : Persists current entity on database
func (e *Entity) Save() error {
db.Save(&e)
return nil
}