...

Source file src/golang.conradwood.net/go-easyops/auth/permissions.go

Documentation: golang.conradwood.net/go-easyops/auth

     1  package auth
     2  
     3  import (
     4  	"context"
     5  	apb "golang.conradwood.net/apis/auth"
     6  	"strings"
     7  )
     8  
     9  func IsRoot(ctx context.Context) bool {
    10  	return IsRootUser(GetUser(ctx))
    11  }
    12  func IsRootUser(user *apb.User) bool {
    13  	return IsInGroupByUser(user, "1")
    14  }
    15  
    16  // return true if service in context is one of the serviceids. serviceids comma delimited
    17  func IsService(ctx context.Context, serviceids string) bool {
    18  	svc := GetService(ctx)
    19  	if svc == nil {
    20  		return false
    21  	}
    22  	for _, g := range strings.Split(serviceids, ",") {
    23  		g = strings.Trim(g, " ")
    24  		if svc.ID == g {
    25  			return true
    26  		}
    27  	}
    28  	return false
    29  }
    30  
    31  // return true if user is in any of the groups (comma delimited list of ids)
    32  func IsInGroupsByUser(user *apb.User, groupids string) bool {
    33  	for _, g := range strings.Split(groupids, ",") {
    34  		g = strings.Trim(g, " ")
    35  		if IsInGroupByUser(user, g) {
    36  			return true
    37  		}
    38  	}
    39  	return false
    40  }
    41  
    42  /*
    43  * return true if user is in this group
    44   */
    45  func IsInGroupByUser(user *apb.User, groupid string) bool {
    46  	if user == nil || groupid == "" || user.Groups == nil {
    47  		return false
    48  	}
    49  	for _, g := range user.Groups {
    50  		if g.ID == groupid {
    51  			return true
    52  		}
    53  	}
    54  
    55  	return false
    56  }
    57  
    58  // return true if user (from context) is part of group specified by groupid
    59  func IsInGroup(ctx context.Context, groupid string) bool {
    60  	u := GetUser(ctx)
    61  	return IsInGroupByUser(u, groupid)
    62  }
    63  
    64  // return true if user (from context) is part of at least one of the groups specified by groupids. groupids is a comma delimited list of groupids
    65  func IsInGroups(ctx context.Context, groupids string) bool {
    66  	u := GetUser(ctx)
    67  	return IsInGroupsByUser(u, groupids)
    68  }
    69  

View as plain text