42 lines
798 B
Go
42 lines
798 B
Go
package auth
|
|
|
|
type Context interface {
|
|
Permissions() map[string]struct{}
|
|
PermitAll(permissions ...string) bool
|
|
PermitAny(permissions ...string) bool
|
|
}
|
|
|
|
// region DeviceContext
|
|
|
|
type DeviceContext struct {
|
|
ID uint
|
|
IpAddress string
|
|
Permissions map[string]struct{}
|
|
}
|
|
|
|
func (c DeviceContext) PermitAny(permissions ...string) bool {
|
|
if _, exist := c.Permissions["*"]; exist {
|
|
return true
|
|
}
|
|
for _, permission := range permissions {
|
|
if _, ok := c.Permissions[permission]; ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c DeviceContext) PermitAll(permissions ...string) bool {
|
|
if _, exist := c.Permissions["*"]; exist {
|
|
return true
|
|
}
|
|
for _, permission := range permissions {
|
|
if _, ok := c.Permissions[permission]; !ok {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// endregion
|