<commit_before>package tasks

import (
	"context"
	"time"

	"golang.org/x/crypto/bcrypt"

	"pixur.org/pixur/be/schema"
	"pixur.org/pixur/be/schema/db"
	tab "pixur.org/pixur/be/schema/tables"
	"pixur.org/pixur/be/status"
)

type CreateUserTask struct {
	// Deps
	DB  db.DB
	Now func() time.Time

	// Inputs
	Ident  string
	Secret string
	// Special input that overrides the defaults.   Used for site bootstrapping.
	Capability []schema.User_Capability

	// Results
	CreatedUser *schema.User
}

func requireCapability(ctx context.Context, j *tab.Job, caps ...schema.User_Capability) (
	*schema.User, status.S) {
	var u *schema.User
	if userID, ok := UserIDFromCtx(ctx); ok {
		users, err := j.FindUsers(db.Opts{
			Prefix: tab.UsersPrimary{&userID},
			Lock:   db.LockNone,
		})
		if err != nil {
			return nil, status.InternalError(err, "can't lookup user")
		}
		if len(users) != 1 {
			return nil, status.Unauthenticated(nil, "can't lookup user")
		}
		u = users[0]
	} else {
		u = schema.AnonymousUser
	}
	// TODO: make sure sorted.
	for _, c := range caps {
		if !schema.UserHasPerm(u, c) {
			return u, status.PermissionDeniedf(nil, "missing cap %v", c)
		}
	}

	return u, nil
}

func (t *CreateUserTask) Run(ctx context.Context) (errCap status.S) {
	var err error<commit_msg>be/tasks: return unauthenticated if anonymous user lacks capability<commit_after>package tasks

import (
	"context"
	"time"

	"golang.org/x/crypto/bcrypt"

	"pixur.org/pixur/be/schema"
	"pixur.org/pixur/be/schema/db"
	tab "pixur.org/pixur/be/schema/tables"
	"pixur.org/pixur/be/status"
)

type CreateUserTask struct {
	// Deps
	DB  db.DB
	Now func() time.Time

	// Inputs
	Ident  string
	Secret string
	// Special input that overrides the defaults.   Used for site bootstrapping.
	Capability []schema.User_Capability

	// Results
	CreatedUser *schema.User
}

func requireCapability(ctx context.Context, j *tab.Job, caps ...schema.User_Capability) (
	*schema.User, status.S) {
	var u *schema.User
	userID, userIDPresent := UserIDFromCtx(ctx)
	if userIDPresent {
		users, err := j.FindUsers(db.Opts{
			Prefix: tab.UsersPrimary{&userID},
			Lock:   db.LockNone,
		})
		if err != nil {
			return nil, status.InternalError(err, "can't lookup user")
		}
		if len(users) != 1 {
			return nil, status.Unauthenticated(nil, "can't lookup user")
		}
		u = users[0]
	} else {
		u = schema.AnonymousUser
	}
	// TODO: make sure sorted.
	for _, c := range caps {
		if !schema.UserHasPerm(u, c) {
			if !userIDPresent {
				return nil, status.Unauthenticated(nil, "unauthenticated user missing cap %v", c)
			}
			return u, status.PermissionDeniedf(nil, "missing cap %v", c)
		}
	}

	return u, nil
}

func (t *CreateUserTask) Run(ctx context.Context) (errCap status.S) {
	var err error
{'fraction_non_alphanumeric': 0.13418181818181818, 'fraction_numerical': 0.0014545454545454545, 'mean_word_length': 3.5246710526315788, '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': '1180156', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1018, 'n_words': 253}