internal/control/scope.go

d7d409e545d4b0999652559ac53e94ce556b56f6
gitbay/internal/control/scope.go history · blame · raw

48 lines · 1506 bytes

 1package control
 2
 3import (
 4	"fmt"
 5
 6	"gitbay.org/gitbay/internal/policy"
 7	"gitbay.org/gitbay/internal/store"
 8)
 9
10// ReadableOrgRepoIDs is the org's repositories user may read. Counts on
11// org labels and milestones are taken over these, so a private
12// repository's issues never show in a number someone outside it sees. A
13// zero user is anonymous.
14func ReadableOrgRepoIDs(st *store.Store, user store.User, orgID int64) ([]int64, error) {
15	repos, err := st.ListReposForOwner("org", orgID)
16	if err != nil {
17		return nil, err
18	}
19	var ids []int64
20	for _, r := range repos {
21		grant := ""
22		if user.ID != 0 {
23			if grant, err = st.AccessRole(r.ID, user.ID); err != nil {
24				return nil, err
25			}
26		}
27		if policy.CanRead(user, r, grant) {
28			ids = append(ids, r.ID)
29		}
30	}
31	return ids, nil
32}
33
34// ReadableScope is the set a repository's label and milestone counts
35// span: its org's readable repositories, or just itself when a user owns
36// it. The caller has already been allowed to read repo.
37func ReadableScope(st *store.Store, user store.User, repo store.Repo) ([]int64, error) {
38	if repo.OwnerKind == "org" {
39		return ReadableOrgRepoIDs(st, user, repo.OwnerID)
40	}
41	return []int64{repo.ID}, nil
42}
43
44// orgScopedMsg names the org command that manages a row a repository
45// command was asked to change.
46func orgScopedMsg(repo store.Repo, noun, name, verb string) string {
47	return fmt.Sprintf("%s is an org %s of %s; manage it with org %s %s %s %s", name, noun, repo.OwnerName, noun, verb, repo.OwnerName, name)
48}