2018-06-19 11:15:10 +00:00
|
|
|
package migrator
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/boltdb/bolt"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/bolt/user"
|
2017-05-23 18:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Migrator) updateAdminUserToDBVersion1() error {
|
2018-06-19 11:15:10 +00:00
|
|
|
u, err := m.userService.UserByUsername("admin")
|
2017-05-23 18:56:10 +00:00
|
|
|
if err == nil {
|
|
|
|
admin := &portainer.User{
|
|
|
|
Username: "admin",
|
|
|
|
Password: u.Password,
|
|
|
|
Role: portainer.AdministratorRole,
|
|
|
|
}
|
2018-06-19 11:15:10 +00:00
|
|
|
err = m.userService.CreateUser(admin)
|
2017-05-23 18:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = m.removeLegacyAdminUser()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-19 11:15:10 +00:00
|
|
|
} else if err != nil && err != portainer.ErrObjectNotFound {
|
2017-05-23 18:56:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Migrator) removeLegacyAdminUser() error {
|
2018-06-19 11:15:10 +00:00
|
|
|
return m.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
bucket := tx.Bucket([]byte(user.BucketName))
|
|
|
|
return bucket.Delete([]byte("admin"))
|
2017-05-23 18:56:10 +00:00
|
|
|
})
|
|
|
|
}
|