Newer
Older
piotr.kupczyk@id.ethz.ch
committed
import _ from 'lodash'
import React from 'react'
import autoBind from 'auto-bind'
import { withStyles } from '@material-ui/core/styles'
import Grid from '@src/js/components/common/grid/Grid.jsx'
piotr.kupczyk@id.ethz.ch
committed
import UserGroupLink from '@src/js/components/common/link/UserGroupLink.jsx'
piotr.kupczyk@id.ethz.ch
committed
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
import openbis from '@src/js/services/openbis.js'
import ids from '@src/js/common/consts/ids.js'
import logger from '@src/js/common/logger.js'
const styles = theme => ({
inherited: {
color: theme.palette.hint.main
},
implicit: {
fontStyle: 'italic'
}
})
const ALL_VALUE = '(all)'
class RolesGrid extends React.PureComponent {
constructor(props) {
super(props)
autoBind(this)
}
render() {
logger.log(logger.DEBUG, 'RolesGrid.render')
const {
id,
rows,
selectedRowId,
onSelectedRowChange,
controllerRef
} = this.props
let columnNames
if (id === ids.USER_ROLES_GRID_ID) {
columnNames = ['inheritedFrom', 'level', 'space', 'project', 'role']
} else if (id === ids.USER_GROUP_ROLES_GRID_ID) {
columnNames = ['level', 'space', 'project', 'role']
} else {
throw 'Unsupported id: ' + id
}
const columns = this.getColumns().filter(
column => columnNames.indexOf(column.name) !== -1
)
return (
<Grid
id={id}
controllerRef={controllerRef}
header='Roles'
columns={columns}
rows={rows}
selectedRowId={selectedRowId}
onSelectedRowChange={onSelectedRowChange}
/>
)
}
getColumns() {
const { id } = this.props
piotr.kupczyk@id.ethz.ch
committed
return [
{
name: 'inheritedFrom',
label: 'Inherited From',
sort: id === ids.USER_ROLES_GRID_ID ? 'asc' : null,
piotr.kupczyk@id.ethz.ch
committed
getValue: this.getInheritedFromValue,
renderValue: this.renderInheritedFromValue,
compareValue: params => {
return (
10000 * this.compareInheritedFromValue(params) +
1000 * this.compareLevelValue(params) +
100 * this.compareSpaceValue(params) +
10 * this.compareProjectValue(params) +
this.compareRoleValue(params)
)
}
},
{
name: 'level',
label: 'Level',
sort: id === ids.USER_GROUP_ROLES_GRID_ID ? 'asc' : null,
piotr.kupczyk@id.ethz.ch
committed
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
getValue: this.getLevelValue,
renderValue: this.renderLevelValue,
compareValue: params => {
return (
1000 * this.compareLevelValue(params) +
100 * this.compareSpaceValue(params) +
10 * this.compareProjectValue(params) +
this.compareRoleValue(params)
)
}
},
{
name: 'space',
label: 'Space',
getValue: this.getSpaceValue,
renderValue: this.renderSpaceValue,
compareValue: params => {
return (
1000 * this.compareSpaceValue(params) +
100 * this.compareLevelValue(params) +
10 * this.compareProjectValue(params) +
this.compareRoleValue(params)
)
}
},
{
name: 'project',
label: 'Project',
getValue: this.getProjectValue,
renderValue: this.renderProjectValue,
compareValue: params => {
return (
1000 * this.compareProjectValue(params) +
100 * this.compareLevelValue(params) +
10 * this.compareSpaceValue(params) +
this.compareRoleValue(params)
)
}
},
{
name: 'role',
label: 'Role',
getValue: this.getRoleValue,
renderValue: this.renderRoleValue,
compareValue: params => {
return (
1000 * this.compareRoleValue(params) +
100 * this.compareLevelValue(params) +
10 * this.compareSpaceValue(params) +
this.compareProjectValue(params)
)
}
}
]
}
getInheritedFromValue({ row }) {
return row.inheritedFrom.value
}
getLevelValue({ row }) {
return row.level.value
}
getSpaceValue({ row }) {
if (row.level.value === openbis.RoleLevel.INSTANCE) {
return ALL_VALUE
} else {
return row.space.value
}
}
getProjectValue({ row }) {
if (row.level.value === openbis.RoleLevel.INSTANCE) {
return ALL_VALUE
} else if (row.level.value === openbis.RoleLevel.SPACE) {
return row.space.value ? ALL_VALUE : null
} else {
return row.project.value
}
}
getRoleValue({ row }) {
return row.role.value
}
piotr.kupczyk@id.ethz.ch
committed
renderInheritedFromValue({ value }) {
return <UserGroupLink groupCode={value} />
piotr.kupczyk@id.ethz.ch
committed
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
}
renderLevelValue({ value, row }) {
return this.renderDefault({ value, row })
}
renderSpaceValue({ value, row }) {
if (!row.space.value && value) {
return (
<div className={this.props.classes.implicit}>
{this.renderDefault({ value, row })}
</div>
)
} else {
return this.renderDefault({ value, row })
}
}
renderProjectValue({ value, row }) {
if (!row.project.value && value) {
return (
<div className={this.props.classes.implicit}>
{this.renderDefault({ value, row })}
</div>
)
} else {
return this.renderDefault({ value, row })
}
}
renderRoleValue({ value, row }) {
return this.renderDefault({ value, row })
}
renderDefault({ value, row }) {
const { classes } = this.props
if (row.inheritedFrom && row.inheritedFrom.value) {
return <div className={classes.inherited}>{value}</div>
} else {
return value
}
}
compareInheritedFromValue({ row1, row2, defaultCompare }) {
const inheritedFrom1 = this.getInheritedFromValue({ row: row1 })
const inheritedFrom2 = this.getInheritedFromValue({ row: row2 })
if (inheritedFrom1 && !inheritedFrom2) {
return -1
} else if (!inheritedFrom1 && inheritedFrom2) {
return 1
} else {
return defaultCompare(inheritedFrom1, inheritedFrom2)
}
}
compareLevelValue({ row1, row2, defaultCompare }) {
const LEVEL_SORTING = {
[null]: 0,
[openbis.RoleLevel.INSTANCE]: 1,
[openbis.RoleLevel.SPACE]: 2,
[openbis.RoleLevel.PROJECT]: 3
}
const level1 = this.getLevelValue({ row: row1 })
const level2 = this.getLevelValue({ row: row2 })
return defaultCompare(LEVEL_SORTING[level1], LEVEL_SORTING[level2])
}
compareSpaceValue({ row1, row2, defaultCompare }) {
const space1 = this.getSpaceValue({ row: row1 })
const space2 = this.getSpaceValue({ row: row2 })
return defaultCompare(space1, space2)
}
compareProjectValue({ row1, row2, defaultCompare }) {
const project1 = this.getProjectValue({ row: row1 })
const project2 = this.getProjectValue({ row: row2 })
return defaultCompare(project1, project2)
}
compareRoleValue({ row1, row2, defaultCompare }) {
const ROLE_SORTING = {
[null]: 0,
[openbis.Role.ETL_SERVER]: 1,
[openbis.Role.ADMIN]: 2,
[openbis.Role.POWER_USER]: 3,
[openbis.Role.USER]: 4,
[openbis.Role.OBSERVER]: 5,
[openbis.Role.DISABLED]: 6
}
const role1 = this.getRoleValue({ row: row1 })
const role2 = this.getRoleValue({ row: row2 })
return defaultCompare(ROLE_SORTING[role1], ROLE_SORTING[role2])
}
}
export default _.flow(withStyles(styles))(RolesGrid)