~rogpeppe/juju-core/azure

« back to all changes in this revision

Viewing changes to state/user.go

  • Committer: William Reade
  • Date: 2012-01-20 21:32:53 UTC
  • mto: This revision was merged to the branch mainline in revision 47.
  • Revision ID: fwereade@gmail.com-20120120213253-csks5e12opl8t1rq
hefty rearrangement, few actual changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2013 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package state
5
 
 
6
 
import (
7
 
        "fmt"
8
 
        "regexp"
9
 
 
10
 
        "labix.org/v2/mgo"
11
 
        "labix.org/v2/mgo/txn"
12
 
 
13
 
        "launchpad.net/juju-core/errors"
14
 
        "launchpad.net/juju-core/utils"
15
 
)
16
 
 
17
 
var validUser = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9]*$")
18
 
 
19
 
// AddUser adds a user to the state.
20
 
func (st *State) AddUser(name, password string) (*User, error) {
21
 
        if !validUser.MatchString(name) {
22
 
                return nil, fmt.Errorf("invalid user name %q", name)
23
 
        }
24
 
        u := &User{
25
 
                st: st,
26
 
                doc: userDoc{
27
 
                        Name:         name,
28
 
                        PasswordHash: utils.PasswordHash(password),
29
 
                },
30
 
        }
31
 
        ops := []txn.Op{{
32
 
                C:      st.users.Name,
33
 
                Id:     name,
34
 
                Assert: txn.DocMissing,
35
 
                Insert: &u.doc,
36
 
        }}
37
 
        err := st.runTransaction(ops)
38
 
        if err == txn.ErrAborted {
39
 
                err = fmt.Errorf("user already exists")
40
 
        }
41
 
        if err != nil {
42
 
                return nil, err
43
 
        }
44
 
        return u, nil
45
 
}
46
 
 
47
 
// getUser fetches information about the user with the
48
 
// given name into the provided userDoc.
49
 
func (st *State) getUser(name string, udoc *userDoc) error {
50
 
        err := st.users.Find(D{{"_id", name}}).One(udoc)
51
 
        if err == mgo.ErrNotFound {
52
 
                err = errors.NotFoundf("user %q", name)
53
 
        }
54
 
        return err
55
 
}
56
 
 
57
 
// User returns the state user for the given name,
58
 
func (st *State) User(name string) (*User, error) {
59
 
        u := &User{st: st}
60
 
        if err := st.getUser(name, &u.doc); err != nil {
61
 
                return nil, err
62
 
        }
63
 
        return u, nil
64
 
}
65
 
 
66
 
// User represents a juju client user.
67
 
type User struct {
68
 
        st  *State
69
 
        doc userDoc
70
 
}
71
 
 
72
 
type userDoc struct {
73
 
        Name         string `bson:"_id_"`
74
 
        PasswordHash string
75
 
}
76
 
 
77
 
// Name returns the user name,
78
 
func (u *User) Name() string {
79
 
        return u.doc.Name
80
 
}
81
 
 
82
 
// Tag returns the Tag for
83
 
// the user ("user-$username")
84
 
func (u *User) Tag() string {
85
 
        return "user-" + u.doc.Name
86
 
}
87
 
 
88
 
// SetPassword sets the password associated with the user.
89
 
func (u *User) SetPassword(password string) error {
90
 
        return u.SetPasswordHash(utils.PasswordHash(password))
91
 
}
92
 
 
93
 
// SetPasswordHash sets the password to the
94
 
// inverse of utils.PasswordHash(pwHash).
95
 
// It can be used when we know only the hash
96
 
// of the password, but not the clear text.
97
 
func (u *User) SetPasswordHash(pwHash string) error {
98
 
        ops := []txn.Op{{
99
 
                C:      u.st.users.Name,
100
 
                Id:     u.Name(),
101
 
                Update: D{{"$set", D{{"passwordhash", pwHash}}}},
102
 
        }}
103
 
        if err := u.st.runTransaction(ops); err != nil {
104
 
                return fmt.Errorf("cannot set password of user %q: %v", u.Name(), err)
105
 
        }
106
 
        u.doc.PasswordHash = pwHash
107
 
        return nil
108
 
}
109
 
 
110
 
// PasswordValid returns whether the given password
111
 
// is valid for the user.
112
 
func (u *User) PasswordValid(password string) bool {
113
 
        return utils.PasswordHash(password) == u.doc.PasswordHash
114
 
}
115
 
 
116
 
// Refresh refreshes information about the user
117
 
// from the state.
118
 
func (u *User) Refresh() error {
119
 
        var udoc userDoc
120
 
        if err := u.st.getUser(u.Name(), &udoc); err != nil {
121
 
                return err
122
 
        }
123
 
        u.doc = udoc
124
 
        return nil
125
 
}