@@ -42,6 +42,13 @@ import history from "../../../history";
42
42
import SearchIcon from "../../../icons/SearchIcon" ;
43
43
import HelpBox from "../../../common/HelpBox" ;
44
44
import PageLayout from "../Common/Layout/PageLayout" ;
45
+ import {
46
+ CONSOLE_UI_RESOURCE ,
47
+ IAM_SCOPES ,
48
+ } from "../../../common/SecureComponent/permissions" ;
49
+ import SecureComponent , {
50
+ hasPermission ,
51
+ } from "../../../common/SecureComponent/SecureComponent" ;
45
52
46
53
const styles = ( theme : Theme ) =>
47
54
createStyles ( {
@@ -91,38 +98,54 @@ const ListPolicies = ({ classes, setErrorSnackMessage }: IPoliciesProps) => {
91
98
const [ filterPolicies , setFilterPolicies ] = useState < string > ( "" ) ;
92
99
const [ policyEdit , setPolicyEdit ] = useState < any > ( null ) ;
93
100
101
+ const viewPolicy = hasPermission ( CONSOLE_UI_RESOURCE , [
102
+ IAM_SCOPES . ADMIN_GET_POLICY ,
103
+ ] ) ;
104
+
105
+ const deletePolicy = hasPermission ( CONSOLE_UI_RESOURCE , [
106
+ IAM_SCOPES . ADMIN_DELETE_POLICY ,
107
+ ] ) ;
108
+
109
+ const displayPolicies = hasPermission ( CONSOLE_UI_RESOURCE , [
110
+ IAM_SCOPES . ADMIN_LIST_USER_POLICIES ,
111
+ ] ) ;
112
+
94
113
useEffect ( ( ) => {
95
114
fetchRecords ( ) ;
96
115
} , [ ] ) ;
97
116
98
117
useEffect ( ( ) => {
99
118
if ( loading ) {
100
- api
101
- . invoke ( "GET" , `/api/v1/policies` )
102
- . then ( ( res : PolicyList ) => {
103
- const policies = get ( res , "policies" , [ ] ) ;
119
+ if ( displayPolicies ) {
120
+ api
121
+ . invoke ( "GET" , `/api/v1/policies` )
122
+ . then ( ( res : PolicyList ) => {
123
+ const policies = get ( res , "policies" , [ ] ) ;
104
124
105
- policies . sort ( ( pa , pb ) => {
106
- if ( pa . name > pb . name ) {
107
- return 1 ;
108
- }
125
+ policies . sort ( ( pa , pb ) => {
126
+ if ( pa . name > pb . name ) {
127
+ return 1 ;
128
+ }
109
129
110
- if ( pa . name < pb . name ) {
111
- return - 1 ;
112
- }
130
+ if ( pa . name < pb . name ) {
131
+ return - 1 ;
132
+ }
113
133
114
- return 0 ;
115
- } ) ;
134
+ return 0 ;
135
+ } ) ;
116
136
117
- setLoading ( false ) ;
118
- setRecords ( policies ) ;
119
- } )
120
- . catch ( ( err : ErrorResponseHandler ) => {
121
- setLoading ( false ) ;
122
- setErrorSnackMessage ( err ) ;
123
- } ) ;
137
+ setLoading ( false ) ;
138
+ setRecords ( policies ) ;
139
+ } )
140
+ . catch ( ( err : ErrorResponseHandler ) => {
141
+ setLoading ( false ) ;
142
+ setErrorSnackMessage ( err ) ;
143
+ } ) ;
144
+ } else {
145
+ setLoading ( false ) ;
146
+ }
124
147
}
125
- } , [ loading , setLoading , setRecords , setErrorSnackMessage ] ) ;
148
+ } , [ loading , setLoading , setRecords , setErrorSnackMessage , displayPolicies ] ) ;
126
149
127
150
const fetchRecords = ( ) => {
128
151
setLoading ( true ) ;
@@ -154,8 +177,17 @@ const ListPolicies = ({ classes, setErrorSnackMessage }: IPoliciesProps) => {
154
177
} ;
155
178
156
179
const tableActions = [
157
- { type : "view" , onClick : viewAction } ,
158
- { type : "delete" , onClick : confirmDeletePolicy , sendOnlyId : true } ,
180
+ {
181
+ type : "view" ,
182
+ onClick : viewAction ,
183
+ disableButtonFunction : ( ) => ! viewPolicy ,
184
+ } ,
185
+ {
186
+ type : "delete" ,
187
+ onClick : confirmDeletePolicy ,
188
+ sendOnlyId : true ,
189
+ disableButtonFunction : ( ) => ! deletePolicy ,
190
+ } ,
159
191
] ;
160
192
161
193
const filteredRecords = records . filter ( ( elementItem ) =>
@@ -199,30 +231,41 @@ const ListPolicies = ({ classes, setErrorSnackMessage }: IPoliciesProps) => {
199
231
} }
200
232
variant = "standard"
201
233
/>
202
- < Button
203
- variant = "contained"
204
- color = "primary"
205
- endIcon = { < AddIcon /> }
206
- onClick = { ( ) => {
207
- setAddScreenOpen ( true ) ;
208
- setPolicyEdit ( null ) ;
209
- } }
234
+ < SecureComponent
235
+ scopes = { [ IAM_SCOPES . ADMIN_CREATE_POLICY ] }
236
+ resource = { CONSOLE_UI_RESOURCE }
210
237
>
211
- Create Policy
212
- </ Button >
238
+ < Button
239
+ variant = "contained"
240
+ color = "primary"
241
+ endIcon = { < AddIcon /> }
242
+ onClick = { ( ) => {
243
+ setAddScreenOpen ( true ) ;
244
+ setPolicyEdit ( null ) ;
245
+ } }
246
+ >
247
+ Create Policy
248
+ </ Button >
249
+ </ SecureComponent >
213
250
</ Grid >
214
251
< Grid item xs = { 12 } >
215
252
< br />
216
253
</ Grid >
217
254
< Grid item xs = { 12 } >
218
- < TableWrapper
219
- itemActions = { tableActions }
220
- columns = { [ { label : "Name" , elementKey : "name" } ] }
221
- isLoading = { loading }
222
- records = { filteredRecords }
223
- entityName = "Policies"
224
- idField = "name"
225
- />
255
+ < SecureComponent
256
+ scopes = { [ IAM_SCOPES . ADMIN_LIST_USER_POLICIES ] }
257
+ resource = { CONSOLE_UI_RESOURCE }
258
+ errorProps = { { disabled : true } }
259
+ >
260
+ < TableWrapper
261
+ itemActions = { tableActions }
262
+ columns = { [ { label : "Name" , elementKey : "name" } ] }
263
+ isLoading = { loading }
264
+ records = { filteredRecords }
265
+ entityName = "Policies"
266
+ idField = "name"
267
+ />
268
+ </ SecureComponent >
226
269
</ Grid >
227
270
< Grid item xs = { 12 } >
228
271
< HelpBox
0 commit comments