-
Notifications
You must be signed in to change notification settings - Fork 10
/
users.ts
133 lines (129 loc) · 3.29 KB
/
users.ts
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
import {
checkbox,
password,
relationship,
text,
virtual,
} from '@keystone-6/core/fields';
import { list, graphql } from '@keystone-6/core';
import path from 'path';
import { permissions, rules } from './access';
import { GitHubRepo, githubReposResolver } from './fields/githubRepos/field';
const fieldModes = {
editSelfOrRead: ({ session, item }: any) =>
permissions.canManageUsers({ session }) || session.itemId === item.id
? 'edit'
: 'read',
editSelfOrHidden: ({ session, item }: any) =>
permissions.canManageUsers({ session }) || session.itemId === item.id
? 'edit'
: 'hidden',
};
export const User = list({
access: {
operation: {
create: () => true,
},
filter: {
query: () => true,
update: rules.canManageUserList,
delete: rules.canManageUserList,
},
},
ui: {
hideCreate: context => !permissions.canManageUsers(context),
hideDelete: context => !permissions.canManageUsers(context),
itemView: {
defaultFieldMode: context =>
permissions.canManageUsers(context) ? 'edit' : 'hidden',
},
listView: {
defaultFieldMode: context =>
permissions.canManageUsers(context) ? 'read' : 'hidden',
},
},
fields: {
name: text({
ui: {
itemView: { fieldMode: fieldModes.editSelfOrRead },
},
}),
email: text({
isIndexed: 'unique',
validation: {
isRequired: true,
},
access: {
read: rules.canManageUser,
},
ui: {
itemView: { fieldMode: fieldModes.editSelfOrHidden },
},
}),
password: password({
validation: {
isRequired: true,
},
ui: {
itemView: { fieldMode: fieldModes.editSelfOrHidden },
},
}),
role: relationship({
ref: 'Role.users',
access: permissions.canManageUsers,
}),
githubUsername: text({
label: 'GitHub Username',
ui: {
itemView: { fieldMode: fieldModes.editSelfOrRead },
},
}),
githubRepos: virtual({
field: graphql.field({
type: graphql.nonNull(graphql.list(GitHubRepo)),
resolve: githubReposResolver,
}),
ui: {
views: path.join(__dirname, './fields/githubRepos/components'),
createView: { fieldMode: 'hidden' },
listView: { fieldMode: 'hidden' },
itemView: { fieldMode: 'read' },
query: '{ name htmlUrl description homepage stargazersCount }',
},
}),
authoredPosts: relationship({
ref: 'Post.author',
many: true,
ui: {
createView: { fieldMode: 'hidden' },
},
}),
pollAnswers: relationship({
ref: 'PollAnswer.answeredByUsers',
many: true,
access: permissions.canManageUsers,
ui: {
hideCreate: true,
createView: { fieldMode: 'hidden' },
},
}),
},
});
export const Role = list({
access: {
filter: {
delete: permissions.canManageUsers,
query: permissions.canManageUsers,
update: permissions.canManageUsers,
},
},
ui: {
isHidden: context => !permissions.canManageUsers(context),
},
fields: {
name: text(),
canManageContent: checkbox({ defaultValue: false }),
canManageUsers: checkbox({ defaultValue: false }),
users: relationship({ ref: 'User.role', many: true }),
},
});