github.com/KiraCore/sekai@v0.3.43/x/gov/keeper/util.go (about)

     1  package keeper
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"github.com/KiraCore/sekai/x/gov/types"
     7  	sdk "github.com/cosmos/cosmos-sdk/types"
     8  )
     9  
    10  // CheckIfAllowedPermission
    11  func CheckIfAllowedPermission(ctx sdk.Context, keeper Keeper, addr sdk.AccAddress, permValue types.PermValue) bool {
    12  	actor, found := keeper.GetNetworkActorByAddress(ctx, addr)
    13  	if !found {
    14  		return false
    15  	}
    16  
    17  	// Get All Roles for actor
    18  	roles := getRolePermissions(ctx, keeper, actor)
    19  
    20  	permMap := map[uint32]bool{}
    21  
    22  	// Add whitelist perms into list
    23  	// First roles
    24  	for _, rolePerm := range roles {
    25  		for _, rp := range rolePerm.Whitelist {
    26  			permMap[rp] = true
    27  		}
    28  	}
    29  
    30  	// Second individual roles
    31  	for _, ap := range actor.Permissions.Whitelist {
    32  		permMap[ap] = true
    33  	}
    34  
    35  	// Remove All Blacklisted perms
    36  	for _, rolePerm := range roles {
    37  		for _, rp := range rolePerm.Blacklist {
    38  			permMap[rp] = false
    39  		}
    40  	}
    41  
    42  	// Remove personal Permissions
    43  	for _, pp := range actor.Permissions.Blacklist {
    44  		permMap[pp] = false
    45  	}
    46  
    47  	isAllowed, ok := permMap[uint32(permValue)]
    48  	if !ok {
    49  		return false
    50  	}
    51  
    52  	return isAllowed
    53  }
    54  
    55  func getRolePermissions(ctx sdk.Context, keeper Keeper, actor types.NetworkActor) map[uint64]*types.Permissions {
    56  	roles := map[uint64]*types.Permissions{}
    57  	for _, role := range actor.Roles {
    58  		rolePerms, found := keeper.GetPermissionsForRole(ctx, role)
    59  		if found {
    60  			roles[role] = &rolePerms
    61  		}
    62  	}
    63  
    64  	return roles
    65  }
    66  
    67  // ProposalIDToBytes returns the byte representation of the proposalID
    68  func ProposalIDToBytes(proposalID uint64) []byte {
    69  	proposalIDBz := sdk.Uint64ToBigEndian(proposalID)
    70  	return proposalIDBz
    71  }
    72  
    73  // BytesToProposalID returns proposalID in uint64 format from a byte array
    74  func BytesToProposalID(bz []byte) uint64 {
    75  	return sdk.BigEndianToUint64(bz)
    76  }
    77  
    78  func BoolToInt(v bool) uint64 {
    79  	if v {
    80  		return 1
    81  	}
    82  	return 0
    83  }
    84  
    85  func IntToBool(v uint64) bool {
    86  	if v != 0 {
    87  		return true
    88  	}
    89  	return false
    90  }
    91  
    92  func ValidateRoleSidKey(key string) bool {
    93  	regex := regexp.MustCompile(`^[a-zA-Z][_0-9a-zA-Z]*$`)
    94  	return regex.MatchString(key)
    95  }