Initialize creates a copy of the mongodb connection and then uses that connection to create the Users collection in mongodb. Then, it ensures that there is a unique index on the uuid key and the username key in this collection, creating the indexes if necessary.
()
| 25 | // create the Users collection in mongodb. Then, it ensures that there is a unique index |
| 26 | // on the uuid key and the username key in this collection, creating the indexes if necessary. |
| 27 | func Initialize() (err error) { |
| 28 | session := db.Connection.Session.Copy() |
| 29 | defer session.Close() |
| 30 | c := session.DB(conf.MONGODB_DATABASE).C("Users") |
| 31 | if err = c.EnsureIndex(mgo.Index{Key: []string{"uuid"}, Unique: true}); err != nil { |
| 32 | return err |
| 33 | } |
| 34 | if err = c.EnsureIndex(mgo.Index{Key: []string{"username"}, Unique: true}); err != nil { |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // Setting admin users based on config file. First, set all users to Admin = false |
| 39 | if _, err = c.UpdateAll(bson.M{}, bson.M{"$set": bson.M{"shock_admin": false}}); err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | // process list of amin users from config, create those that are missing |
| 44 | for _, v := range conf.AdminUsers { |
| 45 | info, err := c.UpdateAll(bson.M{"username": v}, bson.M{"$set": bson.M{"shock_admin": true}}) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | if info.Updated == 0 { |
| 50 | if _, err := New(v, "", true); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return |
| 56 | } |
| 57 | |
| 58 | func New(username string, password string, isAdmin bool) (u *User, err error) { |
| 59 | u = &User{Uuid: uuid.New(), Username: username, Password: password, Admin: isAdmin} |