-
Notifications
You must be signed in to change notification settings - Fork 0
/
envuser.go
205 lines (178 loc) · 5.87 KB
/
envuser.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"fmt"
"time"
"github.com/juju/errors"
"github.com/juju/names"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkg.in/mgo.v2/txn"
)
// EnvironmentUser represents a user access to an environment
// whereas the user could represent a remote user or a user
// across multiple environments the environment user always represents
// a single user for a single environment.
// There should be no more than one EnvironmentUser per user
type EnvironmentUser struct {
st *State
doc envUserDoc
}
type envUserDoc struct {
ID string `bson:"_id"`
EnvUUID string `bson:"env-uuid"`
UserName string `bson:"user"`
DisplayName string `bson:"displayname"`
CreatedBy string `bson:"createdby"`
DateCreated time.Time `bson:"datecreated"`
LastConnection *time.Time `bson:"lastconnection"`
}
// ID returns the ID of the environment user.
func (e *EnvironmentUser) ID() string {
return e.doc.ID
}
// EnvironmentTag returns the environment tag of the environment user.
func (e *EnvironmentUser) EnvironmentTag() names.EnvironTag {
return names.NewEnvironTag(e.doc.EnvUUID)
}
// UserTag returns the tag for the environment user.
func (e *EnvironmentUser) UserTag() names.UserTag {
return names.NewUserTag(e.doc.UserName)
}
// UserName returns the user name of the environment user.
func (e *EnvironmentUser) UserName() string {
return e.doc.UserName
}
// DisplayName returns the display name of the environment user.
func (e *EnvironmentUser) DisplayName() string {
return e.doc.DisplayName
}
// CreatedBy returns the user who created the environment user.
func (e *EnvironmentUser) CreatedBy() string {
return e.doc.CreatedBy
}
// DateCreated returns the date the environment user.
func (e *EnvironmentUser) DateCreated() time.Time {
return e.doc.DateCreated
}
// LastConnection returns the last connection time of the environment user.
func (e *EnvironmentUser) LastConnection() *time.Time {
return e.doc.LastConnection
}
// UpdateLastConnection updates the last connection time of the environment user.
func (e *EnvironmentUser) UpdateLastConnection() error {
timestamp := nowToTheSecond()
ops := []txn.Op{{
C: envUsersC,
Id: e.ID(),
Assert: txn.DocExists,
Update: bson.D{{"$set", bson.D{{"lastconnection", timestamp}}}},
}}
if err := e.st.runTransaction(ops); err != nil {
return errors.Annotatef(err, "cannot update last connection timestamp for envuser %q", e.ID())
}
e.doc.LastConnection = ×tamp
return nil
}
// EnvironmentUser returns the environment user.
func (st *State) EnvironmentUser(user names.UserTag) (*EnvironmentUser, error) {
envUser := &EnvironmentUser{st: st}
envUsers, closer := st.getCollection(envUsersC)
defer closer()
err := envUsers.FindId(user.Username()).One(&envUser.doc)
if err == mgo.ErrNotFound {
return nil, errors.NotFoundf("environment user %q", user.Username())
}
return envUser, nil
}
// AddEnvironmentUser adds a new user to the database.
func (st *State) AddEnvironmentUser(user, createdBy names.UserTag) (*EnvironmentUser, error) {
var displayName string
// Ensure local user exists in state before adding them as an environment user.
if user.IsLocal() {
localUser, err := st.User(user)
if err != nil {
return nil, errors.Annotate(err, fmt.Sprintf("user %q does not exist locally", user.Name()))
}
displayName = localUser.DisplayName()
}
// Ensure local createdBy user exists.
if createdBy.IsLocal() {
if _, err := st.User(createdBy); err != nil {
return nil, errors.Annotate(err, fmt.Sprintf("createdBy user %q does not exist locally", createdBy.Name()))
}
}
envuuid := st.EnvironUUID()
op, doc := createEnvUserOpAndDoc(envuuid, user, createdBy, displayName)
err := st.runTransaction([]txn.Op{op})
if err == txn.ErrAborted {
err = errors.New("env user already exists")
}
if err != nil {
return nil, errors.Trace(err)
}
return &EnvironmentUser{st: st, doc: *doc}, nil
}
func createEnvUserOpAndDoc(envuuid string, user, createdBy names.UserTag, displayName string) (txn.Op, *envUserDoc) {
username := user.Username()
creatorname := createdBy.Username()
doc := &envUserDoc{
ID: username,
EnvUUID: envuuid,
UserName: username,
DisplayName: displayName,
CreatedBy: creatorname,
DateCreated: nowToTheSecond(),
}
op := txn.Op{
C: envUsersC,
Id: username,
Assert: txn.DocMissing,
Insert: doc,
}
return op, doc
}
// RemoveEnvironmentUser adds a new user to the database.
func (st *State) RemoveEnvironmentUser(user names.UserTag) error {
ops := []txn.Op{{
C: envUsersC,
Id: user.Username(),
Assert: txn.DocExists,
Remove: true,
}}
err := st.runTransaction(ops)
if err == txn.ErrAborted {
err = errors.NewNotFound(err, fmt.Sprintf("env user %q does not exist", user.Username()))
}
if err != nil {
return errors.Trace(err)
}
return nil
}
// EnvironmentsForUser returns a list of enviroments that the user
// is able to access.
func (st *State) EnvironmentsForUser(user names.UserTag) ([]*Environment, error) {
// Since there are no groups at this stage, the simplest way to get all
// the environments that a particular user can see is to look through the
// environment user collection. A raw collection is required to support
// queries across multiple environments.
envUsers, userCloser := st.getRawCollection(envUsersC)
defer userCloser()
// TODO: consider adding an index to the envUsers collection on the username.
var userSlice []envUserDoc
err := envUsers.Find(bson.D{{"user", user.Username()}}).All(&userSlice)
if err != nil {
return nil, err
}
var result []*Environment
for _, doc := range userSlice {
envTag := names.NewEnvironTag(doc.EnvUUID)
env, err := st.GetEnvironment(envTag)
if err != nil {
return nil, errors.Trace(err)
}
result = append(result, env)
}
return result, nil
}