This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathuser.go
126 lines (107 loc) · 2.35 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package models
import (
sc "github.com/elithrar/simple-scrypt"
)
// User is an API user of DigitalRebar Provision
// swagger:model
type User struct {
Validation
Access
Meta
Owned
Bundled
// Name is the name of the user
//
// required: true
Name string `index:",key"`
// Description of user
Description string
// PasswordHash is the scrypt-hashed version of the user's Password.
//
PasswordHash []byte `json:",omitempty" index:",ignore"`
// Token secret - this is used when generating user token's to
// allow for revocation by the grantor or the grantee. Changing this
// will invalidate all existing tokens that have this user as a user
// or a grantor.
Secret string `index:",ignore"`
// Roles is a list of Roles this User has.
//
Roles []string
}
func (u *User) GetMeta() Meta {
return u.Meta
}
func (u *User) SetMeta(d Meta) {
u.Meta = d
}
func (u *User) Validate() {
u.AddError(ValidUserName("Invalid Name", u.Name))
}
func (u *User) Prefix() string {
return "users"
}
func (u *User) Key() string {
return u.Name
}
func (u *User) KeyName() string {
return "Name"
}
// GetDescription returns the object's Description
func (u *User) GetDescription() string {
return u.Description
}
func (u *User) Fill() {
u.Validation.fill(u)
if u.Meta == nil {
u.Meta = Meta{}
}
if u.Roles == nil {
u.Roles = []string{}
}
}
func (u *User) CheckPassword(pass string) bool {
if err := sc.CompareHashAndPassword(u.PasswordHash, []byte(pass)); err == nil {
return true
}
return false
}
func (u *User) ChangePassword(newPass string) error {
ph, err := sc.GenerateFromPassword([]byte(newPass), sc.DefaultParams)
if err != nil {
return err
}
u.PasswordHash = ph
// When a user changes their password, invalidate any previous cached auth tokens.
u.Secret = RandString(16)
return nil
}
func (u *User) Sanitize() Model {
res := Clone(u)
res.(*User).PasswordHash = []byte{}
return res
}
func (u *User) AuthKey() string {
return u.Key()
}
// swagger:model
type UserPassword struct {
Password string
}
func (b *User) SliceOf() interface{} {
s := []*User{}
return &s
}
func (b *User) ToModels(obj interface{}) []Model {
items := obj.(*[]*User)
res := make([]Model, len(*items))
for i, item := range *items {
res[i] = Model(item)
}
return res
}
func (b *User) SetName(n string) {
b.Name = n
}
func (b *User) CanHaveActions() bool {
return true
}