mirror of
https://git.rwth-aachen.de/acs/public/villas/web-backend-go/
synced 2025-03-30 00:00:12 +01:00
refactor database.DBAddAdminUser() to database.AddAdminUser()
This commit is contained in:
parent
2932e92438
commit
140973ab27
4 changed files with 105 additions and 73 deletions
95
database/admin.go
Normal file
95
database/admin.go
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
/** Package database
|
||||||
|
*
|
||||||
|
* @author Sonja Happ <sonja.happ@eonerc.rwth-aachen.de>
|
||||||
|
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
|
||||||
|
* @license GNU General Public License (version 3)
|
||||||
|
*
|
||||||
|
* VILLASweb-backend-go
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*********************************************************************************/
|
||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/zpatrick/go-config"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddAdminUser adds a default admin user to the DB
|
||||||
|
func AddAdminUser(cfg *config.Config) (string, error) {
|
||||||
|
DBpool.AutoMigrate(User{})
|
||||||
|
|
||||||
|
// Check if admin user exists in DB
|
||||||
|
var users []User
|
||||||
|
DBpool.Where("Role = ?", "Admin").Find(&users)
|
||||||
|
|
||||||
|
adminPW := ""
|
||||||
|
|
||||||
|
if len(users) == 0 {
|
||||||
|
fmt.Println("No admin user found in DB, adding default admin user.")
|
||||||
|
|
||||||
|
adminName, err := cfg.String("admin.user")
|
||||||
|
if err != nil || adminName == "" {
|
||||||
|
adminName = "admin"
|
||||||
|
}
|
||||||
|
|
||||||
|
adminPW, err = cfg.String("admin.pass")
|
||||||
|
if err != nil || adminPW == "" {
|
||||||
|
adminPW = generatePassword(16)
|
||||||
|
fmt.Printf(" Generated admin password: %s for admin user %s\n", adminPW, adminName)
|
||||||
|
}
|
||||||
|
|
||||||
|
mail, err := cfg.String("admin.mail")
|
||||||
|
if err == nil || mail == "" {
|
||||||
|
mail = "admin@example.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
pwEnc, _ := bcrypt.GenerateFromPassword([]byte(adminPW), 10)
|
||||||
|
|
||||||
|
// create a copy of global test data
|
||||||
|
user := User{
|
||||||
|
Username: adminName,
|
||||||
|
Password: string(pwEnc),
|
||||||
|
Role: "Admin",
|
||||||
|
Mail: mail,
|
||||||
|
Active: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// add admin user to DB
|
||||||
|
err = DBpool.Create(&user).Error
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return adminPW, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generatePassword(Len int) string {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
||||||
|
"abcdefghijklmnopqrstuvwxyz" +
|
||||||
|
"0123456789")
|
||||||
|
|
||||||
|
var b strings.Builder
|
||||||
|
for i := 0; i < Len; i++ {
|
||||||
|
b.WriteRune(chars[rand.Intn(len(chars))])
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.String()
|
||||||
|
}
|
|
@ -24,11 +24,6 @@ package database
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
_ "github.com/jinzhu/gorm/dialects/postgres"
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
||||||
|
@ -123,61 +118,3 @@ func MigrateModels() {
|
||||||
DBpool.AutoMigrate(&Widget{})
|
DBpool.AutoMigrate(&Widget{})
|
||||||
DBpool.AutoMigrate(&Result{})
|
DBpool.AutoMigrate(&Result{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DBAddAdminUser adds a default admin user to the DB
|
|
||||||
func DBAddAdminUser(cfg *config.Config) (string, error) {
|
|
||||||
DBpool.AutoMigrate(User{})
|
|
||||||
|
|
||||||
// Check if admin user exists in DB
|
|
||||||
var users []User
|
|
||||||
DBpool.Where("Role = ?", "Admin").Find(&users)
|
|
||||||
|
|
||||||
adminPW := ""
|
|
||||||
|
|
||||||
if len(users) == 0 {
|
|
||||||
fmt.Println("No admin user found in DB, adding default admin user.")
|
|
||||||
|
|
||||||
adminName, err := cfg.String("admin.user")
|
|
||||||
if err != nil || adminName == "" {
|
|
||||||
adminName = "admin"
|
|
||||||
}
|
|
||||||
|
|
||||||
adminPW, err = cfg.String("admin.pass")
|
|
||||||
if err != nil || adminPW == "" {
|
|
||||||
adminPW = generatePassword(16)
|
|
||||||
fmt.Printf(" Generated admin password: %s for admin user %s\n", adminPW, adminName)
|
|
||||||
}
|
|
||||||
|
|
||||||
mail, err := cfg.String("admin.mail")
|
|
||||||
if err == nil || mail == "" {
|
|
||||||
mail = "admin@example.com"
|
|
||||||
}
|
|
||||||
|
|
||||||
pwEnc, _ := bcrypt.GenerateFromPassword([]byte(adminPW), 10)
|
|
||||||
|
|
||||||
// create a copy of global test data
|
|
||||||
user := User{Username: adminName, Password: string(pwEnc),
|
|
||||||
Role: "Admin", Mail: mail, Active: true}
|
|
||||||
|
|
||||||
// add admin user to DB
|
|
||||||
err = DBpool.Create(&user).Error
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return adminPW, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func generatePassword(Len int) string {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
chars := []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
|
|
||||||
"abcdefghijklmnopqrstuvwxyz" +
|
|
||||||
"0123456789")
|
|
||||||
|
|
||||||
var b strings.Builder
|
|
||||||
for i := 0; i < Len; i++ {
|
|
||||||
b.WriteRune(chars[rand.Intn(len(chars))])
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
|
|
@ -75,7 +75,7 @@ func TestMain(m *testing.M) {
|
||||||
func TestAuthenticate(t *testing.T) {
|
func TestAuthenticate(t *testing.T) {
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// try to authenticate with non JSON body
|
// try to authenticate with non JSON body
|
||||||
|
@ -172,7 +172,7 @@ func TestAuthenticateQueryToken(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -193,7 +193,7 @@ func TestAddGetUser(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -317,7 +317,7 @@ func TestUsersNotAllowedActions(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -376,7 +376,7 @@ func TestGetAllUsers(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -429,7 +429,7 @@ func TestModifyAddedUserAsUser(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -584,7 +584,7 @@ func TestInvalidUserUpdate(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -656,7 +656,7 @@ func TestModifyAddedUserAsAdmin(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
@ -773,7 +773,7 @@ func TestDeleteUser(t *testing.T) {
|
||||||
|
|
||||||
database.DropTables()
|
database.DropTables()
|
||||||
database.MigrateModels()
|
database.MigrateModels()
|
||||||
adminpw, err := database.DBAddAdminUser(configuration.GlobalConfig)
|
adminpw, err := database.AddAdminUser(configuration.GlobalConfig)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// authenticate as admin
|
// authenticate as admin
|
||||||
|
|
2
start.go
2
start.go
|
@ -135,7 +135,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that at least one admin user exists in DB
|
// Make sure that at least one admin user exists in DB
|
||||||
_, err = database.DBAddAdminUser(configuration.GlobalConfig)
|
_, err = database.AddAdminUser(configuration.GlobalConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error: adding admin user failed:", err.Error())
|
fmt.Println("error: adding admin user failed:", err.Error())
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue