forked from openshiporg/openship
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.js
191 lines (188 loc) · 5.92 KB
/
access.js
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
import { permissionsList } from "./schemas/fields";
/*
The basic level of access to the system is being signed in as a valid user. This gives you access
to the Admin UI, access to your own User and Todo items, and read access to roles.
*/
export const isSignedIn = ({ session }) => !!session;
/*
Permissions are shorthand functions for checking that the current user's role has the specified
permission boolean set to true
*/
// export const permissions = {
// // canCreateTodos: ({ session }: ListAccessArgs) =>
// // !!session?.data.role?.canCreateTodos,
// // canManageAllTodos: ({ session }: ListAccessArgs) =>
// // !!session?.data.role?.canManageAllTodos,
// canManageUsers: ({ session }: ListAccessArgs) =>
// !!session?.data.role?.canManageUsers,
// canManageRoles: ({ session }: ListAccessArgs) =>
// !!session?.data.role?.canManageRoles,
// canManageOrders: ({ session }: ListAccessArgs) =>
// !!session?.data.role?.canManageOrders,
// canManageShops: ({ session }: ListAccessArgs) =>
// !!session?.data.role?.canManageShops,
// canManageChannels: ({ session }: ListAccessArgs) =>
// !!session?.data.role?.canManageChannels,
// canManageMatches: ({ session }: ListAccessArgs) =>
// !!session?.data.role?.canManageMatches,
// };
export const permissions = Object.fromEntries(
permissionsList.map((permission) => [
permission,
function ({ session }) {
return !!session?.data.role?.[permission];
},
])
);
/*
Rules are logical functions that can be used for list access, and may return a boolean (meaning
all or no items are available) or a set of filters that limit the available items
*/
export const rules = {
ownItem({ session }) {
if (!isSignedIn({ session })) {
return false;
}
// 2. If not, do they own this item?
return { user: { id: session?.itemId } };
},
canReadUsers: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
if (permissions.canSeeOtherUsers({ session })) {
return true; // They can read everything!
}
// Can only see yourself
return { id: { equals: session.itemId } };
},
canUpdateUsers: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
if (permissions.canManageUsers({ session })) {
return true;
}
// Can update yourself
return { id: { equals: session.itemId } };
},
canReadOrders: ({ session }) => {
if (!isSignedIn({ session })) {
return false;
}
if (permissions.canSeeOtherOrders({ session })) {
return true; // They can read everything!
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canUpdateOrders: ({ session }) => {
if (!isSignedIn({ session })) {
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canManageOrders({ session })) {
return true;
}
// 2. If not, do they own this item?
return { user: { id: { equals: session?.itemId } } };
},
canReadShops: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canSeeOtherShops({ session })) {
return true; // They can read everything!
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canUpdateShops: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canManageShops({ session })) {
return true;
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canReadChannels: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canSeeOtherChannels({ session })) {
return true; // They can read everything!
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canUpdateChannels: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canManageChannels({ session })) {
return true;
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canReadMatches: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canSeeOtherMatches({ session })) {
return true; // They can read everything!
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canUpdateMatches: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canManageMatches({ session })) {
return true;
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canReadLinks: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canSeeOtherLinks({ session })) {
return true; // They can read everything!
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
canUpdateLinks: ({ session }) => {
if (!session) {
// No session? No Users.
return false;
}
// 1. Do they have the permission of canManageProducts
if (permissions.canManageLinks({ session })) {
return true;
}
// 2. If not, do they own this item?
return { user: { id: { equals: session.itemId } } };
},
};