forked from matthewhartstonge/storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
212 lines (166 loc) · 6.38 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package storage
import (
// Standard Library Imports
"context"
"fmt"
"strings"
// External Imports
"github.com/ory/fosite"
// Internal Imports
"github.com/matthewhartstonge/storage/utils"
)
// User provides the specific types for storing, editing, deleting and
// retrieving a User record in mongo.
type User struct {
//// User Meta
// ID is the uniquely assigned uuid that references the user
ID string `bson:"id" json:"id" xml:"id"`
// createTime is when the resource was created in seconds from the epoch.
CreateTime int64 `bson:"createTime" json:"createTime" xml:"createTime"`
// updateTime is the last time the resource was modified in seconds from
// the epoch.
UpdateTime int64 `bson:"updateTime" json:"updateTime" xml:"updateTime"`
// AllowedTenantAccess contains the Tenant IDs that the user has been given
// rights to access.
// This helps in multi-tenanted situations where a user can be given
// explicit cross-tenant access.
AllowedTenantAccess []string `bson:"allowedTenantAccess" json:"allowedTenantAccess,omitempty" xml:"allowedTenantAccess,omitempty"`
// AllowedPersonAccess contains a list of Person IDs that the user is
// allowed access to.
// This helps in multi-tenanted situations where a user can be given
// explicit access to other people accounts, for example, parents to
// children records.
AllowedPersonAccess []string `bson:"allowedPersonAccess" json:"allowedPersonAccess,omitempty" xml:"allowedPersonAccess,omitempty"`
// Scopes contains the permissions that the user is entitled to request.
Scopes []string `bson:"scopes" json:"scopes" xml:"scopes"`
// Roles contains roles that a user has been granted.
Roles []string `bson:"roles" json:"roles" xml:"roles"`
// PersonID is a uniquely assigned id that references a person within the
// system.
// This enables applications where an external person data store is present.
// This helps in multi-tenanted situations where the person is unique, but
// the underlying user accounts can exist per tenant.
PersonID string `bson:"personId" json:"personId" xml:"personId"`
// Disabled specifies whether the user has been disallowed from signing in
Disabled bool `bson:"disabled" json:"disabled" xml:"disabled"`
//// User Content
// Username is used to authenticate a user
Username string `bson:"username" json:"username" xml:"username"`
// Password of the user - will be a hash based on your fosite selected
// hasher.
// If using this model directly in an API, be sure to clear the password
// out when marshaling to json/xml.
Password string `bson:"password,omitempty" json:"password,omitempty" xml:"password,omitempty"`
// FirstName stores the user's Last Name
FirstName string `bson:"firstName" json:"firstName" xml:"firstName"`
// LastName stores the user's Last Name
LastName string `bson:"lastName" json:"lastName" xml:"lastName"`
// ProfileURI is a pointer to where their profile picture lives
ProfileURI string `bson:"profileUri" json:"profileUri,omitempty" xml:"profileUri,omitempty"`
}
// FullName concatenates the User's First Name and Last Name for templating
// purposes
func (u User) FullName() string {
return strings.TrimSpace(fmt.Sprintf("%s %s", u.FirstName, u.LastName))
}
// SetPassword takes a cleartext secret, hashes it with a hasher and sets it as
// the user's password
func (u *User) SetPassword(cleartext string, hasher fosite.Hasher) (err error) {
h, err := hasher.Hash(context.TODO(), []byte(cleartext))
if err != nil {
return err
}
u.Password = string(h)
return nil
}
// GetHashedSecret returns the Users's Hashed Secret as a byte array
func (u *User) GetHashedSecret() []byte {
return []byte(u.Password)
}
// Authenticate compares a cleartext string against the user's
func (u User) Authenticate(cleartext string, hasher fosite.Hasher) error {
return hasher.Compare(context.TODO(), u.GetHashedSecret(), []byte(cleartext))
}
// EnableTenantAccess enables user access to one or many tenants.
func (u *User) EnableTenantAccess(tenantIDs ...string) {
u.AllowedTenantAccess = utils.AppendToStringSet(u.AllowedTenantAccess, tenantIDs...)
}
// DisableTenantAccess disables user access to one or many tenants.
func (u *User) DisableTenantAccess(tenantIDs ...string) {
u.AllowedTenantAccess = utils.RemoveFromStringSet(u.AllowedTenantAccess, tenantIDs...)
}
// EnablePeopleAccess enables user access to the provided people
func (u *User) EnablePeopleAccess(personIDs ...string) {
u.AllowedPersonAccess = utils.AppendToStringSet(u.AllowedPersonAccess, personIDs...)
}
// DisablePeopleAccess disables user access to the provided people.
func (u *User) DisablePeopleAccess(personIDs ...string) {
u.AllowedPersonAccess = utils.RemoveFromStringSet(u.AllowedPersonAccess, personIDs...)
}
// EnableScopeAccess enables user access to one or many scopes.
func (u *User) EnableScopeAccess(scopes ...string) {
u.Scopes = utils.AppendToStringSet(u.Scopes, scopes...)
}
// DisableScopeAccess disables user access to one or many scopes.
func (u *User) DisableScopeAccess(scopes ...string) {
u.Scopes = utils.RemoveFromStringSet(u.Scopes, scopes...)
}
// EnableRoles adds one or many roles to a user.
func (u *User) EnableRoles(roles ...string) {
u.Roles = utils.AppendToStringSet(u.Roles, roles...)
}
// DisableRoles removes one or many roles from a user.
func (u *User) DisableRoles(roles ...string) {
u.Roles = utils.RemoveFromStringSet(u.Roles, roles...)
}
// Equal enables checking equality as having a byte array in a struct stops
// allowing direct equality checks.
func (u User) Equal(x User) bool {
if u.ID != x.ID {
return false
}
if u.CreateTime != x.CreateTime {
return false
}
if u.UpdateTime != x.UpdateTime {
return false
}
if !stringArrayEquals(u.AllowedTenantAccess, x.AllowedTenantAccess) {
return false
}
if !stringArrayEquals(u.AllowedPersonAccess, x.AllowedPersonAccess) {
return false
}
if !stringArrayEquals(u.Scopes, x.Scopes) {
return false
}
if !stringArrayEquals(u.Roles, x.Roles) {
return false
}
if u.PersonID != x.PersonID {
return false
}
if u.Disabled != x.Disabled {
return false
}
if u.Username != x.Username {
return false
}
if u.Password != x.Password {
return false
}
if u.FirstName != x.FirstName {
return false
}
if u.LastName != x.LastName {
return false
}
if u.ProfileURI != x.ProfileURI {
return false
}
return true
}
// IsEmpty returns true if the current user holds no data.
func (u User) IsEmpty() bool {
return u.Equal(User{})
}