github.com/greenpau/go-identity@v1.1.6/role.go (about) 1 // Copyright 2020 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package identity 16 17 import ( 18 "github.com/greenpau/go-identity/pkg/errors" 19 "strings" 20 ) 21 22 // Role is the user role or entitlement in a system. 23 type Role struct { 24 Name string `json:"name,omitempty" xml:"name,omitempty" yaml:"name,omitempty"` 25 Organization string `json:"organization,omitempty" xml:"organization,omitempty" yaml:"organization,omitempty"` 26 } 27 28 // NewRole returns an instance of Role. 29 func NewRole(s string) (*Role, error) { 30 s = strings.TrimSpace(s) 31 if s == "" { 32 return nil, errors.ErrRoleEmpty 33 } 34 parts := strings.Split(s, "/") 35 role := &Role{} 36 if len(parts) == 1 { 37 role.Name = s 38 return role, nil 39 } 40 role.Organization = parts[0] 41 role.Name = strings.Join(parts[1:], "/") 42 return role, nil 43 } 44 45 // String returns string representation of Role instance. 46 func (r *Role) String() string { 47 if r.Organization == "" { 48 return r.Name 49 } 50 return r.Organization + "/" + r.Name 51 }