summaryrefslogtreecommitdiffstats
path: root/models/perm/access_mode.go
blob: 0364191e2e9b29121be90a1468dc149cf6e0339c (plain)
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
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package perm

import (
	"fmt"
	"slices"

	"code.gitea.io/gitea/modules/util"
)

// AccessMode specifies the users access mode
type AccessMode int

const (
	AccessModeNone AccessMode = iota // 0: no access

	AccessModeRead  // 1: read access
	AccessModeWrite // 2: write access
	AccessModeAdmin // 3: admin access
	AccessModeOwner // 4: owner access
)

// ToString returns the string representation of the access mode, do not make it a Stringer, otherwise it's difficult to render in templates
func (mode AccessMode) ToString() string {
	switch mode {
	case AccessModeRead:
		return "read"
	case AccessModeWrite:
		return "write"
	case AccessModeAdmin:
		return "admin"
	case AccessModeOwner:
		return "owner"
	default:
		return "none"
	}
}

func (mode AccessMode) LogString() string {
	return fmt.Sprintf("<AccessMode:%d:%s>", mode, mode.ToString())
}

// ParseAccessMode returns corresponding access mode to given permission string.
func ParseAccessMode(permission string, allowed ...AccessMode) AccessMode {
	m := AccessModeNone
	switch permission {
	case "read":
		m = AccessModeRead
	case "write":
		m = AccessModeWrite
	case "admin":
		m = AccessModeAdmin
	default:
		// the "owner" access is not really used for user input, it's mainly for checking access level in code, so don't parse it
	}
	if len(allowed) == 0 {
		return m
	}
	return util.Iif(slices.Contains(allowed, m), m, AccessModeNone)
}