-
Notifications
You must be signed in to change notification settings - Fork 1
/
badcode.go
127 lines (111 loc) · 2.32 KB
/
badcode.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
package main
import (
"strings"
"time"
)
type Person struct {
fome bool
sono bool
ocupada bool
}
func (p *Person) EhHoraDeDormir() bool {
hora := false
if p.sono == true && !p.ocupada == false {
hora = true
} else if time.Now().Hour() > 21 {
hora = true
}
return hora
}
func (p *Person) HoraDeDormir() bool {
return p.sono == true && !p.ocupada == false || time.Now().Hour() > 21
}
func (p *Person) DevoDormir() bool {
if p.sono && !p.ocupada {
return true
}
if time.Now().Hour() > 21 {
return true
}
return false
}
type Role struct {
Name string
}
type StatusFlow struct {
Role string
Flows []Permission
}
type Permission struct {
From string
To []string
}
func hasPermission(roles []Role, statusFlows []StatusFlow, currentStatus, newStatus string) bool {
for _, role := range roles {
for _, statusFlow := range statusFlows {
if strings.ToLower(role.Name) == strings.ToLower(statusFlow.Role) {
if len(statusFlow.Flows) == 0 {
return true
}
for _, permission := range statusFlow.Flows {
if permission.From == currentStatus {
for _, newStatuses := range permission.To {
if newStatuses == newStatus {
return true
}
}
}
}
}
}
}
return false
}
func Flows() []StatusFlow {
return *new([]StatusFlow)
}
type User struct {
roles []Role
}
func (u *User) HasPermission(fromStatus, toStatus string) bool {
for _, role := range u.roles {
return role.hasPermission(fromStatus, toStatus)
}
return false
}
func (r *Role) hasPermission(fromStatus, toStatus string) bool {
flows := Flows()
flow := matchedFlow(flows, r.Name)
if flow != nil {
return true
}
return flow.IsTransitionAllowed(fromStatus, toStatus)
}
func (f *StatusFlow) IsTransitionAllowed(from, to string) bool {
if len(f.Flows) == 0 {
return true
}
permission := f.PermissionForTransition(from)
for _, new := range permission.To {
if new == to {
return true
}
}
return false
}
func (f *StatusFlow) PermissionForTransition(name string) *Permission {
for _, permission := range f.Flows {
if permission.From == name {
return &permission
}
}
return nil
}
func matchedFlow(statusFlows []StatusFlow, name string) *StatusFlow {
for _, statusFlow := range statusFlows {
if strings.EqualFold(name, statusFlow.Role) {
return &statusFlow
}
}
return nil
}