github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/user/user.go (about) 1 package user 2 3 import ( 4 "time" 5 6 "github.com/evergreen-ci/evergreen/db" 7 "github.com/pkg/errors" 8 "gopkg.in/mgo.v2" 9 "gopkg.in/mgo.v2/bson" 10 ) 11 12 type DBUser struct { 13 Id string `bson:"_id"` 14 FirstName string `bson:"first_name"` 15 LastName string `bson:"last_name"` 16 DispName string `bson:"display_name"` 17 EmailAddress string `bson:"email"` 18 PatchNumber int `bson:"patch_number"` 19 PubKeys []PubKey `bson:"public_keys" json:"public_keys"` 20 CreatedAt time.Time `bson:"created_at"` 21 Settings UserSettings `bson:"settings"` 22 APIKey string `bson:"apikey"` 23 } 24 25 type PubKey struct { 26 Name string `bson:"name" json:"name"` 27 Key string `bson:"key" json:"key"` 28 CreatedAt time.Time `bson:"created_at" json:"created_at"` 29 } 30 31 type UserSettings struct { 32 Timezone string `json:"timezone" bson:"timezone"` 33 NewWaterfall bool `json:"new_waterfall" bson:"new_waterfall"` 34 } 35 36 func (u *DBUser) Username() string { 37 return u.Id 38 } 39 40 func (u *DBUser) DisplayName() string { 41 if u.DispName != "" { 42 return u.DispName 43 } 44 return u.Id 45 } 46 47 func (u *DBUser) Email() string { 48 return u.EmailAddress 49 } 50 51 func (u *DBUser) GetAPIKey() string { 52 return u.APIKey 53 } 54 55 func (u *DBUser) GetPublicKey(keyname string) (string, error) { 56 for _, publicKey := range u.PubKeys { 57 if publicKey.Name == keyname { 58 return publicKey.Key, nil 59 } 60 } 61 return "", errors.Errorf("Unable to find public key '%v' for user '%v'", keyname, u.Username()) 62 } 63 64 func (u *DBUser) PublicKeys() []PubKey { 65 return u.PubKeys 66 } 67 68 func (u *DBUser) Insert() error { 69 u.CreatedAt = time.Now() 70 return db.Insert(Collection, u) 71 } 72 73 // IncPatchNumber increases the count for the user's patch submissions by one, 74 // and then returns the new count. 75 func (u *DBUser) IncPatchNumber() (int, error) { 76 dbUser := &DBUser{} 77 _, err := db.FindAndModify( 78 Collection, 79 bson.M{ 80 IdKey: u.Id, 81 }, 82 nil, 83 mgo.Change{ 84 Update: bson.M{ 85 "$inc": bson.M{ 86 PatchNumberKey: 1, 87 }, 88 }, 89 Upsert: true, 90 ReturnNew: true, 91 }, 92 dbUser, 93 ) 94 if err != nil { 95 return 0, err 96 } 97 return dbUser.PatchNumber, nil 98 99 }