fleet/server/service/service_users.go

60 lines
1.7 KiB
Go
Raw Normal View History

2016-09-26 18:48:55 +00:00
package service
2016-08-28 03:59:17 +00:00
import (
"context"
2016-08-28 03:59:17 +00:00
"github.com/fleetdm/fleet/v4/server"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/ptr"
2016-08-28 03:59:17 +00:00
)
func (svc *Service) CreateInitialUser(ctx context.Context, p fleet.UserPayload) (*fleet.User, error) {
// skipauth: Only the initial user creation should be allowed to skip
// authorization (because there is not yet a user context to check against).
svc.authz.SkipAuthorization(ctx)
setupRequired, err := svc.SetupRequired(ctx)
if err != nil {
return nil, err
}
if !setupRequired {
return nil, ctxerr.New(ctx, "a user already exists")
}
// Initial user should be global admin with no explicit teams
p.GlobalRole = ptr.String(fleet.RoleAdmin)
p.Teams = nil
return svc.newUser(ctx, p)
}
func (svc *Service) newUser(ctx context.Context, p fleet.UserPayload) (*fleet.User, error) {
var ssoEnabled bool
// if user is SSO generate a fake password
if (p.SSOInvite != nil && *p.SSOInvite) || (p.SSOEnabled != nil && *p.SSOEnabled) {
fakePassword, err := server.GenerateRandomText(14)
if err != nil {
return nil, ctxerr.Wrap(ctx, err, "generate stand-in password")
}
p.Password = &fakePassword
ssoEnabled = true
}
user, err := p.User(svc.config.Auth.SaltKeySize, svc.config.Auth.BcryptCost)
if err != nil {
return nil, err
}
user.SSOEnabled = ssoEnabled
user, err = svc.ds.NewUser(ctx, user)
if err != nil {
return nil, err
}
2016-08-28 03:59:17 +00:00
return user, nil
}
func (svc *Service) UserUnauthorized(ctx context.Context, id uint) (*fleet.User, error) {
// Explicitly no authorization check. Should only be used by middleware.
return svc.ds.UserByID(ctx, id)
2016-08-29 00:29:56 +00:00
}