<commit_before>package service

import (
	"crypto/rand"
	"encoding/base64"
	"time"

	"github.com/kolide/kolide-ose/server/contexts/viewer"
	"github.com/kolide/kolide-ose/server/kolide"
	"golang.org/x/net/context"
)

func (svc service) NewUser(ctx context.Context, p kolide.UserPayload) (*kolide.User, error) {
	err := svc.VerifyInvite(ctx, *p.Email, *p.InviteToken)
	if err != nil {
		return nil, err
	}
	invite, err := svc.ds.InviteByEmail(*p.Email)
	if err != nil {
		return nil, err
	}
	user, err := p.User(svc.config.Auth.SaltKeySize, svc.config.Auth.BcryptCost)
	if err != nil {
		return nil, err
	}
	user, err = svc.ds.NewUser(user)
	if err != nil {
		return nil, err
	}
	err = svc.ds.DeleteInvite(invite)
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (svc service) ModifyUser(ctx context.Context, userID uint, p kolide.UserPayload) (*kolide.User, error) {
	user, err := svc.User(ctx, userID)
	if err != nil {
		return nil, err
	}

	// the method assumes that the correct authorization
	// has been validated higher up the stack
	if p.Username != nil {
		user.Username = *p.Username
	}

	if p.Name != nil {
		user.Name = *p.Name
	}

	if p.Admin != nil {
		user.Admin = *p.Admin
	}

	if p.Email != nil {
		user.Email = *p.Email
	}

	if p.Enabled != nil {
		user.Enabled = *p.Enabled
	}

	if p.Position != nil {
		user.Position = *p.Position
	}

	if p.GravatarURL != nil {
		user.GravatarURL = *p.GravatarURL
	}

	if p.AdminForcedPasswordReset != nil {
		err = svc.RequestPasswordReset(ctx, user.Email)
		if err != nil {
			return nil, err
		}
	}

	if p.Password != nil {
		err := user.SetPassword(
			*p.Password,
			svc.config.Auth.SaltKeySize,
			svc.config.Auth.BcryptCost,
		)
		if err != nil {
			return nil, err
		}
		user.AdminForcedPasswordReset = false
	}

	err = svc.saveUser(user)
	if err != nil {<commit_msg>only set AdminForcedPasswordReset if payload value is true (#289)<commit_after>package service

import (
	"crypto/rand"
	"encoding/base64"
	"time"

	"github.com/kolide/kolide-ose/server/contexts/viewer"
	"github.com/kolide/kolide-ose/server/kolide"
	"golang.org/x/net/context"
)

func (svc service) NewUser(ctx context.Context, p kolide.UserPayload) (*kolide.User, error) {
	err := svc.VerifyInvite(ctx, *p.Email, *p.InviteToken)
	if err != nil {
		return nil, err
	}
	invite, err := svc.ds.InviteByEmail(*p.Email)
	if err != nil {
		return nil, err
	}
	user, err := p.User(svc.config.Auth.SaltKeySize, svc.config.Auth.BcryptCost)
	if err != nil {
		return nil, err
	}
	user, err = svc.ds.NewUser(user)
	if err != nil {
		return nil, err
	}
	err = svc.ds.DeleteInvite(invite)
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (svc service) ModifyUser(ctx context.Context, userID uint, p kolide.UserPayload) (*kolide.User, error) {
	user, err := svc.User(ctx, userID)
	if err != nil {
		return nil, err
	}

	// the method assumes that the correct authorization
	// has been validated higher up the stack
	if p.Username != nil {
		user.Username = *p.Username
	}

	if p.Name != nil {
		user.Name = *p.Name
	}

	if p.Admin != nil {
		user.Admin = *p.Admin
	}

	if p.Email != nil {
		user.Email = *p.Email
	}

	if p.Enabled != nil {
		user.Enabled = *p.Enabled
	}

	if p.Position != nil {
		user.Position = *p.Position
	}

	if p.GravatarURL != nil {
		user.GravatarURL = *p.GravatarURL
	}

	if p.AdminForcedPasswordReset != nil {
		if *p.AdminForcedPasswordReset {
			err = svc.RequestPasswordReset(ctx, user.Email)
			if err != nil {
				return nil, err
			}
		}
	}

	if p.Password != nil {
		err := user.SetPassword(
			*p.Password,
			svc.config.Auth.SaltKeySize,
			svc.config.Auth.BcryptCost,
		)
		if err != nil {
			return nil, err
		}
		user.AdminForcedPasswordReset = false
	}

	err = svc.saveUser(user)
	if err != nil {
{'fraction_non_alphanumeric': 0.1414969584765935, 'fraction_numerical': 0.0018513620735255223, 'mean_word_length': 3.12882096069869, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7126714', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1490, 'n_words': 350}