~juju-qa/ubuntu/xenial/juju/2.0-rc2

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/state/user.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
        "time"
17
17
 
18
18
        "github.com/juju/errors"
19
 
        "github.com/juju/juju/core/description"
20
19
        "github.com/juju/utils"
21
20
        "gopkg.in/juju/names.v2"
22
21
        "gopkg.in/mgo.v2"
23
22
        "gopkg.in/mgo.v2/bson"
24
23
        "gopkg.in/mgo.v2/txn"
 
24
 
 
25
        "github.com/juju/juju/permission"
25
26
)
26
27
 
27
28
const userGlobalKeyPrefix = "us"
71
72
        }
72
73
        nameToLower := strings.ToLower(name)
73
74
 
74
 
        dateCreated := nowToTheSecond()
 
75
        dateCreated := st.NowToTheSecond()
75
76
        user := &User{
76
77
                st: st,
77
78
                doc: userDoc{
148
149
        return st.run(buildTxn)
149
150
}
150
151
 
151
 
func createInitialUserOps(controllerUUID string, user names.UserTag, password, salt string) []txn.Op {
 
152
func createInitialUserOps(controllerUUID string, user names.UserTag, password, salt string, dateCreated time.Time) []txn.Op {
152
153
        nameToLower := strings.ToLower(user.Name())
153
 
        dateCreated := nowToTheSecond()
154
154
        doc := userDoc{
155
155
                DocID:        nameToLower,
156
156
                Name:         user.Name(),
172
172
                user.Name(),
173
173
                dateCreated,
174
174
                // first user is controller admin.
175
 
                description.SuperuserAccess)
 
175
                permission.SuperuserAccess)
176
176
 
177
177
        ops = append(ops, controllerUserOps...)
178
178
        return ops
347
347
        return lastLogin.LastLogin.UTC(), nil
348
348
}
349
349
 
350
 
// nowToTheSecond returns the current time in UTC to the nearest second.
351
 
// We use this for a time source that is not more precise than we can
352
 
// handle. When serializing time in and out of mongo, we lose enough
353
 
// precision that it's misleading to store any more than precision to
354
 
// the second.
355
 
// TODO(fwereade): 2016-03-17 lp:1558657
356
 
var nowToTheSecond = func() time.Time { return time.Now().Round(time.Second).UTC() }
 
350
// NowToTheSecond returns the current time in UTC to the nearest second. We use
 
351
// this for a time source that is not more precise than we can handle. When
 
352
// serializing time in and out of mongo, we lose enough precision that it's
 
353
// misleading to store any more than precision to the second.
 
354
func (st *State) NowToTheSecond() time.Time {
 
355
        return st.clock.Now().Round(time.Second).UTC()
 
356
}
357
357
 
358
358
// NeverLoggedInError is used to indicate that a user has never logged in.
359
359
type NeverLoggedInError string
389
389
        lastLogin := userLastLoginDoc{
390
390
                DocID:     u.doc.DocID,
391
391
                ModelUUID: u.st.ModelUUID(),
392
 
                LastLogin: nowToTheSecond(),
 
392
                LastLogin: u.st.NowToTheSecond(),
393
393
        }
394
394
 
395
395
        _, err = lastLoginsW.UpsertId(lastLogin.DocID, lastLogin)