added some utility functions
This commit is contained in:
		@@ -1,5 +1,8 @@
 | 
				
			|||||||
package keycloakclaims
 | 
					package keycloakclaims
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// RoleInRoles returns true if role is present in the claims.
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Deprecated: RoleInRoles will be replaced by HasRole
 | 
				
			||||||
func RoleInRoles(role string, claims *Claims) bool {
 | 
					func RoleInRoles(role string, claims *Claims) bool {
 | 
				
			||||||
	for _, v := range claims.RealmAccess.Roles {
 | 
						for _, v := range claims.RealmAccess.Roles {
 | 
				
			||||||
		if role == v {
 | 
							if role == v {
 | 
				
			||||||
@@ -8,3 +11,25 @@ func RoleInRoles(role string, claims *Claims) bool {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// HasRole returns true if role is present in the claims.
 | 
				
			||||||
 | 
					func HasRole(query string, claims *Claims) bool {
 | 
				
			||||||
 | 
						for _, role := range claims.RealmAccess.Roles {
 | 
				
			||||||
 | 
							if query == role {
 | 
				
			||||||
 | 
								return true
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return false
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// HasAnyRole returns true if any of the given roles is present in the claims.
 | 
				
			||||||
 | 
					func HasAnyRole(query []string, claims *Claims) bool {
 | 
				
			||||||
 | 
						for _, q := range query {
 | 
				
			||||||
 | 
							for _, role := range claims.RealmAccess.Roles {
 | 
				
			||||||
 | 
								if q == role {
 | 
				
			||||||
 | 
									return true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return false
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user