forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
85 lines (84 loc) · 3.23 KB
/
Copy pathschema.ts
File metadata and controls
85 lines (84 loc) · 3.23 KB
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
import { list } from '@keystone-6/core';
import { allOperations, allowAll } from '@keystone-6/core/access';
import { text, checkbox, password } from '@keystone-6/core/fields';
export const lists = {
User: list({
access: {
operation: {
...allOperations(allowAll),
// Only allow admins to delete users
delete: ({ session }) => session?.data.isAdmin,
},
},
ui: {
// Since you can't delete users unless you're an admin, we hide the UI for it
hideDelete: ({ session }) => !session?.data.isAdmin,
listView: {
// These are the default columns that will be displayed in the list view
initialColumns: ['name', 'email', 'isAdmin'],
},
},
fields: {
// The user's name
name: text({ validation: { isRequired: true } }),
// The user's email address, used as the identity field for auth
email: text({ isIndexed: 'unique', validation: { isRequired: true } }),
// The user's password, used as the secret field for auth
password: password({
access: {
// Passwords can always be set when creating items
// Users can change their own passwords, and Admins can change anyone's password
update: ({ session, item }) =>
session && (session.data.isAdmin || session.itemId === item.id),
},
ui: {
// Based on the same logic as update access, the password field is editable.
// The password field is hidden from non-Admin users (except for themselves)
// createView: {
// fieldMode: ({ session }) => (session?.data.isAdmin ? 'edit' : 'hidden'),
// },
itemView: {
fieldMode: ({ session, item }) =>
session && (session.data.isAdmin || session.itemId === item.id) ? 'edit' : 'hidden',
},
listView: {
fieldMode: ({ session }) => (session?.item?.isAdmin ? 'read' : 'hidden'),
},
},
}),
// This is used for access control, both in the schema and for the Admin UI
isAdmin: checkbox({
access: {
// Only Admins can set the isAdmin flag for any users
create: ({ session }) => session?.data.isAdmin,
update: ({ session }) => session?.data.isAdmin,
},
ui: {
// All users can see the isAdmin status, only admins can change it
createView: {
fieldMode: ({ session }) => (session?.data.isAdmin ? 'edit' : 'hidden'),
},
itemView: {
fieldMode: ({ session }) => (session?.data.isAdmin ? 'edit' : 'read'),
},
},
}),
/* TODO: Come back to this when we review how to restrict signin to valid users
// This controls whether users can sign in or not
isEnabled: checkbox({
access: {
// Only Admins can change the isEnabled flag for any users
// create: ({ session }) => session?.data.isAdmin,
update: ({ session }) => session?.data.isAdmin,
},
ui: {
// All users can see the isEnabled status, only admins can change it
itemView: {
fieldMode: ({ session }) => (session?.data.isAdmin ? 'edit' : 'read'),
},
},
}),
*/
},
}),
};