Example #1
0
func Initialise() {
	logging.Info("data", "Initialise()")
	trackingSetup()

	dbConn, err := sql.Open("postgres", "postgres://"+config.All().Database.Username+
		":"+config.All().Database.Password+
		"@"+config.All().Database.Address+
		"/"+config.All().Database.Name+
		"?sslmode=require")
	if err != nil {
		logging.Error("data", "Error opening DB connection")
		logging.Error("data", "Error: ", err)
		tracking_notifyFault(err)
	}

	DB, err = gorm.Open("postgres", dbConn)
	DB.LogMode(true)

	if err != nil {
		logging.Error("data", "Error launching DB engine")
		logging.Error("data", "Error: ", err)
	}

	checkStructures(DB)

	//make sure that objects in the config BaseObjects are
	//existing, creating them if nessesary.
	for _, usr := range config.All().BaseObjects.AdminUsers {

		tmp := user.User{}

		DB.Where(&user.User{Username: usr.Username}).First(&tmp)

		if tmp.Username != usr.Username { //if the user was not found
			logging.Info("data", "Creating admin user: "******"data", "Could not create admin. ", err)
			}

			DB.Create(&user.User{Username: usr.Username,
				Permissions: []user.Permission{user.Permission{Name: user.PERM_ADMIN}},
				AuthMethods: []user.AuthenticationMethod{authMethod},
			})
		}
	}

	logging.Info("data", "Initialisation finished.")
}
Example #2
0
func upgrade_v3(db gorm.DB) {
	// Hash the passwords
	logging.Info("Migrating 2 => 3")

	var unhashed []user.AuthenticationMethod
	db.Where("method_type = ?", user.AUTH_PASSWD).Find(&unhashed)

	for i := 0; i < len(unhashed); i++ {
		hashed, err := user.HashedPasswordAuth(unhashed[i].Value)
		if err != nil {
			logging.Error("data", "Could not transform password. ID ", unhashed[i].ID)
		} else {
			hashed.UserID = unhashed[i].UserID

			db.Create(&hashed)
			db.Delete(&unhashed[i])
		}
	}
}