aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core/auth/options.go
blob: 7bcda68e7a7fe25c2781e3b5f2939da963457009 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package auth

import (
	"github.com/MichaelMure/git-bug/entity"
	"github.com/MichaelMure/git-bug/identity"
)

type options struct {
	target string
	userId entity.Id
	kind   CredentialKind
}

type Option func(opts *options)

func matcher(opts []Option) *options {
	result := &options{}
	for _, opt := range opts {
		opt(result)
	}
	return result
}

func (opts *options) Match(cred Credential) bool {
	if opts.target != "" && cred.Target() != opts.target {
		return false
	}

	if opts.userId != "" && cred.UserId() != opts.userId {
		return false
	}

	if opts.kind != "" && cred.Kind() != opts.kind {
		return false
	}

	return true
}

func WithTarget(target string) Option {
	return func(opts *options) {
		opts.target = target
	}
}

func WithUser(user identity.Interface) Option {
	return func(opts *options) {
		opts.userId = user.Id()
	}
}

func WithUserId(userId entity.Id) Option {
	return func(opts *options) {
		opts.userId = userId
	}
}

func WithKind(kind CredentialKind) Option {
	return func(opts *options) {
		opts.kind = kind
	}
}