-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
68 lines (59 loc) · 1.69 KB
/
account.go
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
63
64
65
66
67
68
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
package admission
import (
"context"
"errors"
"strings"
"github.com/ozonep/drone/core"
)
// ErrMembership is returned when attempting to create a new
// user account for a user that is not a member of an approved
// organization.
var ErrMembership = errors.New("User must be a member of an approved organization")
// Membership limits user access by organization membership.
func Membership(service core.OrganizationService, accounts []string) core.AdmissionService {
lookup := map[string]struct{}{}
for _, account := range accounts {
account = strings.TrimSpace(account)
account = strings.ToLower(account)
lookup[account] = struct{}{}
}
return &membership{service: service, account: lookup}
}
type membership struct {
service core.OrganizationService
account map[string]struct{}
}
func (s *membership) Admit(ctx context.Context, user *core.User) error {
// this admission policy is only enforced for
// new users. Existing users are always admitted.
if user.ID != 0 {
return nil
}
// if the membership whitelist is empty assume the system
// is open admission.
if len(s.account) == 0 {
return nil
}
// if the username is in the whitelist when can admin
// the user without making an API call to fetch the
// organization list.
_, ok := s.account[strings.ToLower(user.Login)]
if ok {
return nil
}
orgs, err := s.service.List(ctx, user)
if err != nil {
return err
}
for _, org := range orgs {
_, ok := s.account[strings.ToLower(org.Name)]
if ok {
return nil
}
}
return ErrMembership
}