2019-10-30 23:12:04 +00:00
|
|
|
package migrator
|
|
|
|
|
2020-06-16 07:58:16 +00:00
|
|
|
import (
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
|
|
|
)
|
2019-10-30 23:12:04 +00:00
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
func (m *Migrator) updateResourceControlsToDBVersion22() error {
|
2022-04-14 04:25:13 +00:00
|
|
|
migrateLog.Info("- updating resource controls")
|
2019-11-12 23:41:42 +00:00
|
|
|
legacyResourceControls, err := m.resourceControlService.ResourceControls()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, resourceControl := range legacyResourceControls {
|
|
|
|
resourceControl.AdministratorsOnly = false
|
|
|
|
|
|
|
|
err := m.resourceControlService.UpdateResourceControl(resourceControl.ID, &resourceControl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Migrator) updateUsersAndRolesToDBVersion22() error {
|
2022-04-14 04:25:13 +00:00
|
|
|
migrateLog.Info("- updating users and roles")
|
2019-10-30 23:12:04 +00:00
|
|
|
legacyUsers, err := m.userService.Users()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
settings, err := m.settingsService.Settings()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-30 23:12:04 +00:00
|
|
|
for _, user := range legacyUsers {
|
2020-06-16 07:58:16 +00:00
|
|
|
user.PortainerAuthorizations = authorization.DefaultPortainerAuthorizations()
|
2019-10-30 23:12:04 +00:00
|
|
|
err = m.userService.UpdateUser(user.ID, &user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
endpointAdministratorRole, err := m.roleService.Role(portainer.RoleID(1))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 08:22:47 +00:00
|
|
|
endpointAdministratorRole.Priority = 1
|
2020-06-16 07:58:16 +00:00
|
|
|
endpointAdministratorRole.Authorizations = authorization.DefaultEndpointAuthorizationsForEndpointAdministratorRole()
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
err = m.roleService.UpdateRole(endpointAdministratorRole.ID, endpointAdministratorRole)
|
|
|
|
|
|
|
|
helpDeskRole, err := m.roleService.Role(portainer.RoleID(2))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 08:22:47 +00:00
|
|
|
helpDeskRole.Priority = 2
|
2020-06-16 07:58:16 +00:00
|
|
|
helpDeskRole.Authorizations = authorization.DefaultEndpointAuthorizationsForHelpDeskRole(settings.AllowVolumeBrowserForRegularUsers)
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
err = m.roleService.UpdateRole(helpDeskRole.ID, helpDeskRole)
|
|
|
|
|
|
|
|
standardUserRole, err := m.roleService.Role(portainer.RoleID(3))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 08:22:47 +00:00
|
|
|
standardUserRole.Priority = 3
|
2020-06-16 07:58:16 +00:00
|
|
|
standardUserRole.Authorizations = authorization.DefaultEndpointAuthorizationsForStandardUserRole(settings.AllowVolumeBrowserForRegularUsers)
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
err = m.roleService.UpdateRole(standardUserRole.ID, standardUserRole)
|
|
|
|
|
|
|
|
readOnlyUserRole, err := m.roleService.Role(portainer.RoleID(4))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-18 08:22:47 +00:00
|
|
|
readOnlyUserRole.Priority = 4
|
2020-06-16 07:58:16 +00:00
|
|
|
readOnlyUserRole.Authorizations = authorization.DefaultEndpointAuthorizationsForReadOnlyUserRole(settings.AllowVolumeBrowserForRegularUsers)
|
2019-11-12 23:41:42 +00:00
|
|
|
|
|
|
|
err = m.roleService.UpdateRole(readOnlyUserRole.ID, readOnlyUserRole)
|
2020-05-20 05:23:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-11-12 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
return m.authorizationService.UpdateUsersAuthorizations()
|
2019-10-30 23:12:04 +00:00
|
|
|
}
|