-
Notifications
You must be signed in to change notification settings - Fork 0
/
priv.go
174 lines (146 loc) · 5.03 KB
/
priv.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// MIT License
//
// Copyright (c) 2022 xybor-x
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package xypriv
import (
"fmt"
"reflect"
"strings"
)
// Relation represents for the relation of a Subject over another one.
type Relation string
// Privilege represents for the privilege of a Subject over another one.
type Privilege int
// AccessLevel represents for the access level of a Resource.
type AccessLevel int
// Resource instances are objects that need to be protected from illegal
// actions.
type Resource interface {
// Context returns the context of resource.
Context() any
// Owner returns the owner of resource
Owner() Subject
}
// StaticResource instances are Resources whose permission is based on only the
// action.
type StaticResource interface {
Resource
// Permission defines the access level of resource. It is only based on the
// action.
Permission(action ...string) AccessLevel
}
// DynamicResource instances are Resources whose permission is based on both
// the action and the subject who wants to perform the action.
type DynamicResource interface {
Resource
// Permission defines the access level of resource. It is based on both the
// action and the subject who wants to perform the action.
Permission(s Subject, action ...string) AccessLevel
}
// Subject instances are entities that want to perform action on Resource.
type Subject interface {
// Relation returns the privilege value of the current Subject over passed
// Subject.
Relation(ctx any, s Subject) Relation
}
// Delegatee instances helps to limit the privileges of a Subject.
type Delegatee interface {
// Delegate returns true if the condition is allowed to perform, and vice
// versa.
Delegate(relation Relation, resource Resource, action ...string) bool
}
// Checker supports check if a subject can perform action on resource or not.
type Checker struct {
subject Subject
delegatee Delegatee
action []string
}
// Check returns a Checker with the subject.
func Check(s Subject) *Checker {
return &Checker{subject: s}
}
// Delegate delegates the privileges to a delegatee.
func (c *Checker) Delegate(d Delegatee) *Checker {
c.delegatee = d
return c
}
// Perform adds action to Checker and returns itself.
func (c *Checker) Perform(action ...string) *Checker {
c.action = action
return c
}
// On checks if a subject can perform action on resource or not.
func (c *Checker) On(resource Resource) error {
var accessLevel AccessLevel
switch t := resource.(type) {
case StaticResource:
accessLevel = t.Permission(c.action...)
case DynamicResource:
accessLevel = t.Permission(c.subject, c.action...)
default:
panic(NotImplementedError.New(
"expected an object implementing Resource or EnhancedResource"))
}
var ctx = resource.Context()
var owner = resource.Owner()
if ctx == owner && owner != nil {
panic(XyprivError.New("do not use the owner as the context, you " +
"should set the context as nil in this case"))
}
var privilege = Anyone
if c.subject != nil {
var relation = c.subject.Relation(ctx, owner)
privilege = getPrivilege(ctx, relation)
if c.delegatee != nil && !c.delegatee.Delegate(relation, resource, c.action...) {
return PermissionError.Newf(
"%s do not have the permission to %s %s",
getName(c.subject), strings.Join(c.action, "_"), getName(resource))
}
}
if int(privilege) < int(accessLevel) {
return PermissionError.Newf(
"%s do not have the permission to %s %s",
getName(c.subject), strings.Join(c.action, "_"), getName(resource))
}
return nil
}
// getName returns the name of object.
func getName(a any) string {
var name = func() string {
if a == nil {
return "nil"
}
if s, ok := a.(fmt.Stringer); ok {
return s.String()
}
var atype = reflect.TypeOf(a)
switch atype.Kind() {
case reflect.String:
return a.(string)
case reflect.Struct:
return atype.Name()
case reflect.Pointer:
return atype.Elem().Name()
default:
panic(XyprivError.New("expected a string, struct, or pointer of struct to get its name"))
}
}()
return strings.ReplaceAll(name, ".", "_")
}