/
permissions.go
94 lines (80 loc) · 2.27 KB
/
permissions.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
package models
import (
"errors"
"fmt"
"log"
)
const (
ValidateBooks uint64 = 0
ValidateAuthors uint64 = 1
ElevateUsers uint64 = 2
DemoteUsers uint64 = 3
ValidateSingleAuthor uint64 = 4
ValidateSingleBook uint64 = 5
)
type Permission struct {
Type uint64
Ref uint64
}
func TargetedPermission(permissionType uint64, id uint64) *Permission {
if !IsTargeted(permissionType) {
log.Fatal("Gave invalid permission type ", permissionType)
}
return &Permission{permissionType, id}
}
func BroadPermission(permissionType uint64) *Permission {
if !IsValidPermissionType(permissionType) || IsTargeted(permissionType) {
log.Fatal("Gave invalid permission type ", permissionType)
}
return &Permission{permissionType, 0}
}
func IsTargeted(permissionType uint64) bool {
return permissionType > 3 && permissionType < 6
}
func IsValidPermissionType(value uint64) bool {
return value < 6
}
//func Type(permissionType uint64) string {
// switch permissionType {
// case ValidateBooks:
// return "ValidateBooks"
// case ValidateAuthors:
// return "ValidateAuthors"
// case ElevateUsers:
// return "ElevateUsers"
// case DemoteUsers:
// return "DemoteUsers"
// case ValidateSingleAuthor:
// return "ValidateSingleAuthor"
// case ValidateSingleBook:
// return "ValidateSingleBook"
// }
//
// log.Fatal("This should never happen")
// return ""
//}
func BuildPermission(permissionType string, reference uint64) (*Permission, error) {
switch permissionType {
case "ValidateBooks":
return BroadPermission(ValidateBooks), nil
case "ValidateAuthors":
return BroadPermission(ValidateAuthors), nil
case "ElevateUsers":
return BroadPermission(ElevateUsers), nil
case "DemoteUsers":
return BroadPermission(DemoteUsers), nil
case "ValidateSingleAuthor":
return TargetedPermission(ValidateSingleAuthor, reference), nil
case "ValidateSingleBook":
return TargetedPermission(ValidateSingleBook, reference), nil
}
return nil, errors.New(fmt.Sprintf("permissionType %s is invalid", permissionType))
}
func PermissionContains(l, r Permission) bool {
if l.Type == ValidateBooks {
return r.Type == ValidateBooks || r.Type == ValidateSingleBook
} else if l.Type == ValidateAuthors {
return r.Type == ValidateAuthors || r.Type == ValidateSingleAuthor
}
return l == r
}