github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/acl/perm.gno (about)

     1  package acl
     2  
     3  import "regexp"
     4  
     5  type perm struct {
     6  	verbs     []string
     7  	resources []string
     8  }
     9  
    10  func (perm perm) hasPerm(verb, resource string) bool {
    11  	// check verb
    12  	verbOK := false
    13  	for _, pattern := range perm.verbs {
    14  		if match(pattern, verb) {
    15  			verbOK = true
    16  			break
    17  		}
    18  	}
    19  	if !verbOK {
    20  		return false
    21  	}
    22  
    23  	// check resource
    24  	for _, pattern := range perm.resources {
    25  		if match(pattern, resource) {
    26  			return true
    27  		}
    28  	}
    29  	return false
    30  }
    31  
    32  func match(pattern, target string) bool {
    33  	if pattern == ".*" {
    34  		return true
    35  	}
    36  
    37  	if pattern == target {
    38  		return true
    39  	}
    40  
    41  	// regexp handling
    42  	match, _ := regexp.MatchString(pattern, target)
    43  	return match
    44  }