2022-07-30 17:01:05 +03:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NullAttr string = ""
|
|
|
|
AllAttr string = "*"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AclBit struct {
|
2022-08-04 11:49:45 +03:00
|
|
|
Acl int64
|
2022-07-30 17:01:05 +03:00
|
|
|
Atr string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize acl bits and atributes
|
2022-08-04 11:49:45 +03:00
|
|
|
func NewAclBit(def int64, atr string) *AclBit {
|
2022-07-30 17:01:05 +03:00
|
|
|
acl := new(AclBit)
|
|
|
|
|
|
|
|
acl.Acl = def
|
|
|
|
acl.Atr = atr
|
|
|
|
|
|
|
|
return acl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set acl bits and atributes
|
2022-08-04 11:49:45 +03:00
|
|
|
func (a *AclBit) Set(acl int64, atr string) {
|
2022-07-30 17:01:05 +03:00
|
|
|
a.Acl = acl
|
|
|
|
a.Atr = atr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set array acl bits and atributes
|
|
|
|
func (a *AclBit) SetArray(acl []AclBit) {
|
2022-08-04 11:49:45 +03:00
|
|
|
var endacl int64
|
2022-07-30 17:01:05 +03:00
|
|
|
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
|
2022-08-04 11:49:45 +03:00
|
|
|
func (a *AclBit) SetAcl(acl int64) {
|
2022-07-30 17:01:05 +03:00
|
|
|
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
|
2022-08-04 11:49:45 +03:00
|
|
|
func (a *AclBit) SetBitTrue(n int64) {
|
2022-07-30 17:01:05 +03:00
|
|
|
a.Acl = a.Acl | (1 << n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set acl bit in false
|
2022-08-04 11:49:45 +03:00
|
|
|
func (a *AclBit) SetBitFalse(n int64) {
|
2022-07-30 17:01:05 +03:00
|
|
|
a.Acl = a.Acl &^ (1 << n)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get acl bit return true or false
|
2022-08-04 11:49:45 +03:00
|
|
|
func (a *AclBit) GetBit(n int64) bool {
|
|
|
|
var msk int64 = 1 << n
|
2022-07-30 17:01:05 +03:00
|
|
|
if (a.Acl & msk) == msk {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// converting acl bits in string
|
|
|
|
func (a *AclBit) StringAcl() string {
|
2022-08-04 11:49:45 +03:00
|
|
|
return strconv.FormatInt(int64(a.Acl), 2)
|
2022-07-30 17:01:05 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 11:49:45 +03:00
|
|
|
// converting string bits to int64
|
|
|
|
func ConvertToInt(str string) (int64, error) {
|
|
|
|
i64, err := strconv.ParseInt(str, 2, 64)
|
2022-07-30 17:01:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, errors.New("string not bit formats")
|
|
|
|
}
|
2022-08-04 11:49:45 +03:00
|
|
|
return i64, nil
|
2022-07-30 17:01:05 +03:00
|
|
|
}
|