From b129ea6a8196ed61c4a0912757b7ce73f6173e8d Mon Sep 17 00:00:00 2001 From: Anatoly Prohacky Date: Sun, 31 Jul 2022 00:01:05 +1000 Subject: [PATCH] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20ACL=20=D1=81=20=D0=B1=D0=B8=D1=82=D0=BE=D0=B2=D1=8B?= =?UTF-8?q?=D0=BC=D0=B8=20=D1=81=D1=80=D0=B0=D0=B2=D0=BD=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- acl/aclbit.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 29 +++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 acl/aclbit.go create mode 100644 main.go diff --git a/acl/aclbit.go b/acl/aclbit.go new file mode 100644 index 0000000..2aa4cda --- /dev/null +++ b/acl/aclbit.go @@ -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 +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..1fa66f5 --- /dev/null +++ b/main.go @@ -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)) +}