|
| 1 | +package keycloak |
| 2 | + |
| 3 | +import "strings" |
| 4 | + |
| 5 | +// UserProfileRepresentation struct |
| 6 | +type UserProfileRepresentation struct { |
| 7 | + Attributes []ProfileAttrbRepresentation `json:"attributes"` |
| 8 | + Groups []ProfileGroupRepresentation `json:"groups"` |
| 9 | +} |
| 10 | + |
| 11 | +// ProfileAttrbRepresentation struct |
| 12 | +type ProfileAttrbRepresentation struct { |
| 13 | + Name *string `json:"name,omitempty"` |
| 14 | + DisplayName *string `json:"displayName,omitempty"` |
| 15 | + Group *string `json:"group,omitempty"` |
| 16 | + Required *ProfileAttrbRequiredRepresentation `json:"required,omitempty"` |
| 17 | + Permissions *ProfileAttrbPermissionsRepresentation `json:"permissions,omitempty"` |
| 18 | + Validations ProfileAttrbValidationRepresentation `json:"validations,omitempty"` |
| 19 | + Selector *ProfileAttrbSelectorRepresentation `json:"selector,omitempty"` |
| 20 | + Annotations map[string]string `json:"annotations,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +// ProfileAttrbValidationRepresentation struct |
| 24 | +// Known keys: |
| 25 | +// - email: empty |
| 26 | +// - length: min, max (int/string), trim-disabled (boolean as string) |
| 27 | +// - integer: min, max (integer as string) |
| 28 | +// - double: min, max (double as string) |
| 29 | +// - options: options (array of allowed values) |
| 30 | +// - pattern: pattern (regex as string), error-message (string) |
| 31 | +// - local-date: empty |
| 32 | +// - uri: empty |
| 33 | +// - username-prohibited-characters: error-message (string) |
| 34 | +// - person-name-prohibited-characters: error-message (string) |
| 35 | +type ProfileAttrbValidationRepresentation map[string]ProfileAttrValidatorRepresentation |
| 36 | + |
| 37 | +type ProfileAttrValidatorRepresentation map[string]interface{} |
| 38 | + |
| 39 | +// ProfileAttrbRequiredRepresentation struct |
| 40 | +type ProfileAttrbRequiredRepresentation struct { |
| 41 | + Roles []string `json:"roles,omitempty"` |
| 42 | + Scopes []string `json:"scopes,omitempty"` |
| 43 | +} |
| 44 | + |
| 45 | +// ProfileAttrbPermissionsRepresentation struct |
| 46 | +type ProfileAttrbPermissionsRepresentation struct { |
| 47 | + View []string `json:"view,omitempty"` |
| 48 | + Edit []string `json:"edit,omitempty"` |
| 49 | +} |
| 50 | + |
| 51 | +// ProfileAttrbSelectorRepresentation struct |
| 52 | +type ProfileAttrbSelectorRepresentation struct { |
| 53 | + Scopes []string `json:"scopes,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// ProfileGroupRepresentation struct |
| 57 | +type ProfileGroupRepresentation struct { |
| 58 | + Name *string `json:"name,omitempty"` |
| 59 | + DisplayHeader *string `displayHeader:"name,omitempty"` |
| 60 | + DisplayDescription *string `displayDescription:"name,omitempty"` |
| 61 | + Annotations map[string]string `annotations:"name,omitempty"` |
| 62 | +} |
| 63 | + |
| 64 | +// IsAnnotationTrue checks if an annotation is true |
| 65 | +func (attrb *ProfileAttrbRepresentation) IsAnnotationTrue(key string) bool { |
| 66 | + return attrb.AnnotationEqualsIgnoreCase(key, "true") |
| 67 | +} |
| 68 | + |
| 69 | +// IsAnnotationFalse checks if an annotation is false |
| 70 | +func (attrb *ProfileAttrbRepresentation) IsAnnotationFalse(key string) bool { |
| 71 | + return attrb.AnnotationEqualsIgnoreCase(key, "false") |
| 72 | +} |
| 73 | + |
| 74 | +// AnnotationEqualsIgnoreCase checks if an annotation |
| 75 | +func (attrb *ProfileAttrbRepresentation) AnnotationEqualsIgnoreCase(key string, value string) bool { |
| 76 | + return attrb.AnnotationMatches(key, func(attrbValue string) bool { |
| 77 | + return strings.EqualFold(value, attrbValue) |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +// AnnotationMatches checks if an annotation |
| 82 | +func (attrb *ProfileAttrbRepresentation) AnnotationMatches(key string, matcher func(value string) bool) bool { |
| 83 | + if attrb.Annotations != nil { |
| 84 | + if attrbValue, ok := attrb.Annotations[key]; ok { |
| 85 | + return matcher(attrbValue) |
| 86 | + } |
| 87 | + } |
| 88 | + return false |
| 89 | +} |
0 commit comments