~evanj/cms

ref: 9d7fbcbdf7e7332ea56493092efe5cffbadccdd6 cms/internal/s/rbac/rbac.go -rw-r--r-- 7.0 KiB
9d7fbcbdEvan M Jones Fix(go.mod/vendor): Attempting to fix vendoring for deploys. 2 months ago
                                                                                
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Code generated by "rbac_gen"; DO NOT EDIT.

package rbac

import (
	"log"

	"git.sr.ht/~evanj/cms/internal/m/content"
	"git.sr.ht/~evanj/cms/internal/m/contenttype"
	"git.sr.ht/~evanj/cms/internal/m/hook"
	"git.sr.ht/~evanj/cms/internal/m/invite"
	"git.sr.ht/~evanj/cms/internal/m/org"
	"git.sr.ht/~evanj/cms/internal/m/role"
	"git.sr.ht/~evanj/cms/internal/m/space"
	"git.sr.ht/~evanj/cms/internal/m/tier"
	"git.sr.ht/~evanj/cms/internal/m/user"
	"git.sr.ht/~evanj/cms/internal/s/db"
	"git.sr.ht/~evanj/cms/internal/s/rl"
)

type RBAC struct {
	rl.RL
	log *log.Logger
	db  rl.RL // Or DB or Cache, depends on DI order in main.
}

func New(l *log.Logger, db rl.RL) RBAC {
	return RBAC{db, l, db}
}

// SPACE

func (rbac RBAC) SpaceNew(user user.User, name, desc string) (space.Space, error) {
	if user.Role().Can(role.SpaceCreate) {
		return rbac.db.SpaceNew(user, name, desc)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) SpaceCopy(user user.User, prevS space.Space, name, desc string) (space.Space, error) {
	if user.Role().Can(role.SpaceCreate) {
		return rbac.db.SpaceCopy(user, prevS, name, desc)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) SpaceUpdate(user user.User, space space.Space, name, desc string) (space.Space, error) {
	if user.Role().Can(role.SpaceUpdate) {
		return rbac.db.SpaceUpdate(user, space, name, desc)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) SpaceGet(user user.User, spaceID string) (space.Space, error) {
	if user.Role().Can(role.SpaceGet) {
		return rbac.db.SpaceGet(user, spaceID)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) SpacesPerUser(user user.User, before int) (space.SpaceList, error) {
	if user.Role().Can(role.SpaceGet) {
		return rbac.db.SpacesPerUser(user, before)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) SpaceDelete(user user.User, space space.Space) error {
	if user.Role().Can(role.SpaceDelete) {
		return rbac.db.SpaceDelete(user, space)
	}
	return role.ErrNoPermission
}

// INVITE

func (rbac RBAC) InviteNew(u user.User, o org.Org) (invite.Invite, error) {
	if u.Role().Can(role.InviteCreate) {
		return rbac.db.InviteNew(u, o)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) InviteGet(u user.User, id string) (invite.Invite, error) {
	if u.Role().Can(role.InviteGet) {
		return rbac.db.InviteGet(u, id)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) InviteList(u user.User, o org.Org) (r []invite.Invite, err error) {
	if u.Role().Can(role.InviteGet) {
		return rbac.db.InviteList(u, o)
	}
	return nil, role.ErrNoPermission
}

// CONTENTTYPE

func (rbac RBAC) ContentTypeNew(u user.User, space space.Space, name string, params []db.ContentTypeNewParam) (contenttype.ContentType, error) {
	if u.Role().Can(role.ContentTypeCreate) {
		return rbac.db.ContentTypeNew(u, space, name, params)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentTypeUpdate(u user.User, space space.Space, contenttype contenttype.ContentType, name string, newParams []db.ContentTypeNewParam, updateParams []db.ContentTypeUpdateParam) (contenttype.ContentType, error) {
	if u.Role().Can(role.ContentTypeUpdate) {
		return rbac.db.ContentTypeUpdate(u, space, contenttype, name, newParams, updateParams)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentTypesPerSpace(u user.User, space space.Space, before int) (contenttype.ContentTypeList, error) {
	if u.Role().Can(role.ContentTypeGet) {
		return rbac.db.ContentTypesPerSpace(u, space, before)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentTypeGet(u user.User, space space.Space, contenttypeID string) (contenttype.ContentType, error) {
	if u.Role().Can(role.ContentTypeGet) {
		return rbac.db.ContentTypeGet(u, space, contenttypeID)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentTypeSearch(u user.User, space space.Space, query string, before int) (contenttype.ContentTypeList, error) {
	if u.Role().Can(role.ContentTypeGet) {
		return rbac.db.ContentTypeSearch(u, space, query, before)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentTypeDelete(u user.User, space space.Space, ct contenttype.ContentType) error {
	if u.Role().Can(role.ContentTypeDelete) {
		rbac.db.ContentTypeDelete(u, space, ct)
	}
	return role.ErrNoPermission
}

// HOOK

func (rbac RBAC) HookNew(u user.User, space space.Space, url string) (hook.Hook, error) {
	if u.Role().Can(role.HookCreate) {
		return rbac.db.HookNew(u, space, url)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) HookGet(u user.User, space space.Space, id string) (hook.Hook, error) {
	if u.Role().Can(role.HookGet) {
		return rbac.db.HookGet(u, space, id)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) HookDelete(u user.User, s space.Space, h hook.Hook) error {
	if u.Role().Can(role.HookDelete) {
		return rbac.db.HookDelete(u, s, h)
	}
	return role.ErrNoPermission
}

func (rbac RBAC) HooksPerSpace(u user.User, space space.Space, before int) (hook.HookList, error) {
	if u.Role().Can(role.HookGet) {
		return rbac.db.HooksPerSpace(u, space, before)
	}
	return nil, role.ErrNoPermission
}

// CONTENT

func (rbac RBAC) ContentNew(u user.User, space space.Space, ct contenttype.ContentType, params []db.ContentNewParam) (content.Content, error) {
	if u.Role().Can(role.ContentCreate) {
		return rbac.db.ContentNew(u, space, ct, params)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentUpdate(u user.User, space space.Space, ct contenttype.ContentType, content content.Content, newParams []db.ContentNewParam, updateParams []db.ContentUpdateParam) (content.Content, error) {
	if u.Role().Can(role.ContentUpdate) {
		return rbac.db.ContentUpdate(u, space, ct, content, newParams, updateParams)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentDelete(u user.User, space space.Space, ct contenttype.ContentType, content content.Content) error {
	if u.Role().Can(role.ContentDelete) {
		return rbac.db.ContentDelete(u, space, ct, content)
	}
	return role.ErrNoPermission
}

func (rbac RBAC) ContentPerContentType(u user.User, space space.Space, ct contenttype.ContentType, before int, order db.OrderType, sortField string) (content.ContentList, error) {
	if u.Role().Can(role.ContentGet) {
		return rbac.db.ContentPerContentType(u, space, ct, before, order, sortField)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentSearch(u user.User, space space.Space, ct contenttype.ContentType, sortField, query string, before int) (content.ContentList, error) {
	if u.Role().Can(role.ContentGet) {
		return rbac.db.ContentSearch(u, space, ct, sortField, query, before)
	}
	return nil, role.ErrNoPermission
}

func (rbac RBAC) ContentGet(u user.User, space space.Space, ct contenttype.ContentType, contentID string) (content.Content, error) {
	if u.Role().Can(role.ContentGet) {
		return rbac.db.ContentGet(u, space, ct, contentID)
	}
	return nil, role.ErrNoPermission
}

// ORG

func (rbac RBAC) OrgUpdateTier(u user.User, o org.Org, t tier.Tier, paymentCustomerID string) error {
	if u.Role().Can(role.OrgUpdate) {
		return rbac.db.OrgUpdateTier(u, o, t, paymentCustomerID)
	}
	return role.ErrNoPermission
}