forked from target/goalert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.go
44 lines (37 loc) · 877 Bytes
/
role.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
package permission
import (
"database/sql/driver"
"fmt"
)
// Role represents a users access level
type Role string
// Available roles
const (
RoleUser Role = "user"
RoleAdmin Role = "admin"
RoleUnknown Role = "unknown"
)
// Scan handles reading a Role from the DB format
func (r *Role) Scan(value interface{}) error {
switch t := value.(type) {
case []byte:
*r = Role(t)
case string:
*r = Role(t)
default:
return fmt.Errorf("could not process unknown type for role %T", t)
}
if *r != RoleAdmin && *r != RoleUser && *r != RoleUnknown {
return fmt.Errorf("unknown value for role %v", *r)
}
return nil
}
// Value converts the Role to the DB representation
func (r Role) Value() (driver.Value, error) {
switch r {
case RoleUser, RoleAdmin, RoleUnknown:
return string(r), nil
default:
return nil, fmt.Errorf("invalid role value: %v", r)
}
}