This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
role_scope.go
125 lines (104 loc) · 4.11 KB
/
role_scope.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
package repository
import (
"context"
"time"
resourcetype "github.com/fabric8-services/fabric8-auth/authorization/resourcetype/repository"
"github.com/fabric8-services/fabric8-auth/gormsupport"
"github.com/fabric8-services/fabric8-auth/log"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
errs "github.com/pkg/errors"
"github.com/satori/go.uuid"
)
// RoleScope defines the association between a resource type's scope(s) and a resource type's role.
type RoleScope struct {
gormsupport.Lifecycle
// The associated scope
ResourceTypeScope resourcetype.ResourceTypeScope `gorm:"ForeignKey:ResourceTypeScopeID"`
// The foreign key value for ResourceTypeScopeID
ResourceTypeScopeID uuid.UUID `gorm:"primary_key;column:scope_id" sql:"type:uuid"`
// The associated role
Role Role `gorm:"ForeignKey:RoleID"`
// The foreign key value for RoleID
RoleID uuid.UUID `gorm:"primary_key;column:role_id" sql:"type:uuid"`
}
// TableName overrides the table name settings in Gorm to force a specific table name
// in the database.
func (m RoleScope) TableName() string {
return "role_scope"
}
// GetLastModified returns the last modification time
func (m RoleScope) GetLastModified() time.Time {
return m.UpdatedAt
}
// GormRoleScopeRepository is the implementation of the storage interface for RoleScope.
type GormRoleScopeRepository struct {
db *gorm.DB
}
// NewRoleScopeRepository creates a new storage type.
func NewRoleScopeRepository(db *gorm.DB) RoleScopeRepository {
return &GormRoleScopeRepository{db: db}
}
// RoleScopeRepository represents the storage interface.
type RoleScopeRepository interface {
LoadByScope(ctx context.Context, ID uuid.UUID) ([]RoleScope, error)
LoadByRole(ctx context.Context, ID uuid.UUID) ([]RoleScope, error)
Create(ctx context.Context, roleScope *RoleScope) error
Query(funcs ...func(*gorm.DB) *gorm.DB) ([]RoleScope, error)
}
// TableName overrides the table name settings in Gorm to force a specific table name
// in the database.
func (m *GormRoleScopeRepository) TableName() string {
return "role_scope"
}
// Create creates a new RoleScope
func (m *GormRoleScopeRepository) Create(ctx context.Context, roleScope *RoleScope) error {
defer goa.MeasureSince([]string{"goa", "db", "role_scope", "create"}, time.Now())
err := m.db.Create(roleScope).Error
if err != nil {
log.Error(ctx, map[string]interface{}{
"resource_type_scope_id": roleScope.ResourceTypeScope.ResourceTypeScopeID,
"role_id": roleScope.Role.RoleID,
"err": err,
}, "unable to create the role scope")
return errs.WithStack(err)
}
log.Debug(ctx, map[string]interface{}{
"resource_scope_id": roleScope.ResourceTypeScope.ResourceTypeScopeID,
"role_id": roleScope.Role.RoleID,
}, "Role Scope created!")
return nil
}
//LoadByScope loads a 'role & scope assocation' by the scope ID
func (m *GormRoleScopeRepository) LoadByScope(ctx context.Context, ID uuid.UUID) ([]RoleScope, error) {
return m.Query(RoleScopeFilterByScope(ID))
}
//LoadByRole loads a 'role & scope assocation' by the role ID
func (m *GormRoleScopeRepository) LoadByRole(ctx context.Context, ID uuid.UUID) ([]RoleScope, error) {
return m.Query(RoleScopeFilterByRole(ID))
}
// Query expose an open ended Query model
func (m *GormRoleScopeRepository) Query(funcs ...func(*gorm.DB) *gorm.DB) ([]RoleScope, error) {
defer goa.MeasureSince([]string{"goa", "db", "role_scope", "query"}, time.Now())
var roleScopes []RoleScope
err := m.db.Scopes(funcs...).Table(m.TableName()).Find(&roleScopes).Error
if err != nil && err != gorm.ErrRecordNotFound {
return nil, errs.WithStack(err)
}
log.Debug(nil, map[string]interface{}{
"role_scopes": roleScopes,
}, "Role Scope query executed successfully!")
return roleScopes, nil
}
// RoleScopeFilterByScope is a gorm filter by 'scope_id'
func RoleScopeFilterByScope(id uuid.UUID) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("scope_id = ?", id)
}
}
// RoleScopeFilterByRole is a gorm filter by 'role'
func RoleScopeFilterByRole(id uuid.UUID) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
return db.Where("role_id = ?", id)
}
}