forked from sensu/sensu-go
/
user.go
76 lines (63 loc) · 1.7 KB
/
user.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package v2
import (
"errors"
fmt "fmt"
"net/url"
"path"
"strconv"
"strings"
)
const (
// UsersResource is the name of this resource type
UsersResource = "users"
)
// GetObjectMeta is a dummy implementation to meet the Resource interface.
func (u *User) GetObjectMeta() ObjectMeta {
return ObjectMeta{}
}
// StorePrefix returns the path prefix to this resource in the store
func (u *User) StorePrefix() string {
return UsersResource
}
// URIPath is the URI path component to a user.
func (u *User) URIPath() string {
return path.Join(URLPrefix, UsersResource, url.PathEscape(u.Username))
}
// Validate returns an error if the entity is invalid.
func (u *User) Validate() error {
if err := ValidateNameStrict(u.Username); err != nil {
return fmt.Errorf("username %s", err)
}
return nil
}
// ValidatePassword returns an error if the entity is invalid.
func (u *User) ValidatePassword() error {
if u.Password == "" {
return errors.New("password can't be empty")
}
if len(u.Password) < 8 {
return errors.New("password length must be at least 8 characters")
}
return nil
}
// FixtureUser returns a testing fixture for an Entity object.
func FixtureUser(username string) *User {
return &User{
Username: username,
Password: "P@ssw0rd!",
Groups: []string{"default"},
}
}
// UserFields returns a set of fields that represent that resource
func UserFields(r Resource) map[string]string {
resource := r.(*User)
return map[string]string{
"user.username": resource.Username,
"user.disabled": strconv.FormatBool(resource.Disabled),
"user.groups": strings.Join(resource.Groups, ","),
}
}
// SetNamespace sets the namespace of the resource.
func (u *User) SetNamespace(namespace string) {
return
}