Создание ACL с битовыми сравнениями
This commit is contained in:
parent
40a7851e45
commit
b129ea6a81
101
acl/aclbit.go
Normal file
101
acl/aclbit.go
Normal file
@ -0,0 +1,101 @@
|
||||
package acl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
NullAttr string = ""
|
||||
AllAttr string = "*"
|
||||
)
|
||||
|
||||
type AclBit struct {
|
||||
Acl uint64
|
||||
Atr string
|
||||
}
|
||||
|
||||
// Initialize acl bits and atributes
|
||||
func NewAclBit(def uint64, atr string) *AclBit {
|
||||
acl := new(AclBit)
|
||||
|
||||
acl.Acl = def
|
||||
acl.Atr = atr
|
||||
|
||||
return acl
|
||||
}
|
||||
|
||||
// Set acl bits and atributes
|
||||
func (a *AclBit) Set(acl uint64, atr string) {
|
||||
a.Acl = acl
|
||||
a.Atr = atr
|
||||
}
|
||||
|
||||
// Set array acl bits and atributes
|
||||
func (a *AclBit) SetArray(acl []AclBit) {
|
||||
var endacl uint64
|
||||
var endatr string
|
||||
lenacl := len(acl)
|
||||
for _, a0 := range acl {
|
||||
endacl = endacl | a0.Acl
|
||||
if a0.Atr != NullAttr {
|
||||
endatr = endatr + a0.Atr
|
||||
if lenacl > 1 {
|
||||
endatr = endatr + ","
|
||||
}
|
||||
}
|
||||
lenacl--
|
||||
}
|
||||
a.Acl = endacl
|
||||
a.Atr = endatr
|
||||
}
|
||||
|
||||
// set acl bits
|
||||
func (a *AclBit) SetAcl(acl uint64) {
|
||||
a.Acl = acl
|
||||
}
|
||||
|
||||
// set acl atributes
|
||||
func (a *AclBit) SetAtr(atr string) {
|
||||
a.Atr = atr
|
||||
}
|
||||
|
||||
// get current acl
|
||||
func (a *AclBit) Get() *AclBit {
|
||||
return a
|
||||
}
|
||||
|
||||
// set acl bit in true
|
||||
func (a *AclBit) SetBitTrue(n int) {
|
||||
a.Acl = a.Acl | (1 << n)
|
||||
}
|
||||
|
||||
// set acl bit in false
|
||||
func (a *AclBit) SetBitFalse(n int) {
|
||||
a.Acl = a.Acl &^ (1 << n)
|
||||
}
|
||||
|
||||
// get acl bit return true or false
|
||||
func (a *AclBit) GetBit(n int) bool {
|
||||
var msk uint64 = 1 << n
|
||||
if (a.Acl & msk) == msk {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// converting acl bits in string
|
||||
func (a *AclBit) StringAcl() string {
|
||||
return strconv.FormatUint(uint64(a.Acl), 2)
|
||||
}
|
||||
|
||||
// converting string bits to uint64
|
||||
func Uint64(str string) (uint64, error) {
|
||||
u64, err := strconv.ParseUint(str, 2, 64)
|
||||
if err != nil {
|
||||
return 0, errors.New("string not bit formats")
|
||||
}
|
||||
return u64, nil
|
||||
}
|
29
main.go
Normal file
29
main.go
Normal file
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gitstore.ru/tolikproh/policy/acl"
|
||||
)
|
||||
|
||||
func main() {
|
||||
acl1 := acl.NewAclBit(1, acl.AllAttr)
|
||||
acl2 := acl.NewAclBit(2, acl.NullAttr)
|
||||
acl3 := acl.NewAclBit(1, "finance")
|
||||
acl4 := acl.NewAclBit(8, "nofinance")
|
||||
a := make([]acl.AclBit, 0)
|
||||
a = append(a, *acl1, *acl2, *acl3, *acl4)
|
||||
|
||||
// randoms := 0
|
||||
// for i := 0; i <= 16; i++ {
|
||||
// fmt.Println(randoms, ":", acl1.StringAcl())
|
||||
// randoms = rand.Intn(64)
|
||||
// acl1.SetBitTrue(randoms)
|
||||
// }
|
||||
fmt.Println(a)
|
||||
aclend := acl.NewAclBit(0, acl.NullAttr)
|
||||
aclend.SetArray(a)
|
||||
fmt.Println(aclend.StringAcl())
|
||||
fmt.Println(aclend.Atr)
|
||||
fmt.Println(aclend.GetBit(2))
|
||||
}
|
Loading…
Reference in New Issue
Block a user