github.com/datachainlab/burrow@v0.25.0/permission/base_permissions.go (about)

     1  package permission
     2  
     3  import "fmt"
     4  
     5  // Gets the permission value.
     6  // ErrValueNotSet is returned if the permission's set bits are not all on,
     7  // and should be caught by caller so the global permission can be fetched
     8  func (bp BasePermissions) Get(ty PermFlag) (bool, error) {
     9  	if ty == 0 {
    10  		return false, ErrInvalidPermission(ty)
    11  	}
    12  	if !bp.IsSet(ty) {
    13  		return false, ErrValueNotSet(ty)
    14  	}
    15  	return bp.Perms&ty == ty, nil
    16  }
    17  
    18  // Set a permission bit. Will set the permission's set bit to true.
    19  func (bp *BasePermissions) Set(ty PermFlag, value bool) error {
    20  	if ty == 0 {
    21  		return ErrInvalidPermission(ty)
    22  	}
    23  	bp.SetBit |= ty
    24  	if value {
    25  		bp.Perms |= ty
    26  	} else {
    27  		bp.Perms &= ^ty
    28  	}
    29  	return nil
    30  }
    31  
    32  // Set the permission's set bits to false
    33  func (bp *BasePermissions) Unset(ty PermFlag) error {
    34  	if ty == 0 {
    35  		return ErrInvalidPermission(ty)
    36  	}
    37  	bp.SetBit &= ^ty
    38  	return nil
    39  }
    40  
    41  // Check if the permission is set
    42  func (bp BasePermissions) IsSet(ty PermFlag) bool {
    43  	if ty == 0 {
    44  		return false
    45  	}
    46  	return bp.SetBit&ty == ty
    47  }
    48  
    49  // Returns the Perms PermFlag masked with SetBit bit field to give the resultant
    50  // permissions enabled by this BasePermissions
    51  func (bp BasePermissions) ResultantPerms() PermFlag {
    52  	return bp.Perms & bp.SetBit
    53  }
    54  
    55  // Returns a BasePermission that matches any permissions set on this BasePermission
    56  // and falls through to any permissions set on the bpFallthrough
    57  func (bp BasePermissions) Compose(bpFallthrough BasePermissions) BasePermissions {
    58  	return BasePermissions{
    59  		// Combine set perm flags from bp with set perm flags in fallthrough NOT set in bp
    60  		Perms:  (bp.Perms & bp.SetBit) | (bpFallthrough.Perms & (^bp.SetBit & bpFallthrough.SetBit)),
    61  		SetBit: bp.SetBit | bpFallthrough.SetBit,
    62  	}
    63  }
    64  
    65  func (bp BasePermissions) String() string {
    66  	return fmt.Sprintf("Base: %b; Set: %b", bp.Perms, bp.SetBit)
    67  }