@@ -5,7 +5,7 @@ import {Entities} from "../components/Entities";
55import I18n from "../locale/I18n" ;
66import { Button , ButtonType , Checkbox , Loader } from "@surfnet/sds" ;
77import { useNavigate } from "react-router-dom" ;
8- import { apiTokens , createToken , deleteToken , generateToken } from "../api" ;
8+ import { allIdentityProviders , apiTokens , createToken , deleteToken , generateToken } from "../api" ;
99import { dateFromEpoch } from "../utils/Date" ;
1010import TrashIcon from "@surfnet/sds/icons/functional-icons/bin.svg" ;
1111import ChevronLeft from "@surfnet/sds/icons/functional-icons/arrow-left-2.svg" ;
@@ -16,6 +16,7 @@ import ErrorIndicator from "../components/ErrorIndicator";
1616import { isEmpty , stopEvent } from "../utils/Utils" ;
1717import { Page } from "../components/Page" ;
1818import { AUTHORITIES , highestAuthority } from "../utils/UserRole" ;
19+ import SelectField from "../components/SelectField" ;
1920
2021export const Tokens = ( ) => {
2122 const { user, setFlash} = useAppStore ( state => state ) ;
@@ -31,6 +32,8 @@ export const Tokens = () => {
3132 const [ initial , setInitial ] = useState ( true ) ;
3233 const [ confirmation , setConfirmation ] = useState ( { } ) ;
3334 const [ confirmationOpen , setConfirmationOpen ] = useState ( false ) ;
35+ const [ identityProviders , setIdentityProviders ] = useState ( [ ] ) ;
36+ const [ organizationGUIDIdentityProvider , setOrganizationGUIDIdentityProvider ] = useState ( { } ) ;
3437
3538 const isGuest = authority === AUTHORITIES . GUEST ;
3639
@@ -43,6 +46,22 @@ export const Tokens = () => {
4346 setDescription ( "" ) ;
4447 setTokenValue ( null ) ;
4548 setConfirmationOpen ( false ) ;
49+ setInitial ( true ) ;
50+ setDescription ( "" ) ;
51+ setOrganizationGUIDIdentityProvider ( { } ) ;
52+ //Now fetch all IdentityProviders for the superuser
53+ if ( user . superUser ) {
54+ allIdentityProviders ( ) . then ( idps => {
55+ const identityProviderOptions = idps
56+ . filter ( idp => ! isEmpty ( idp . institutionGuid ) )
57+ . map ( idp => ( {
58+ value : idp . id ,
59+ label : idp [ "name:en" ] ,
60+ institutionGuid : idp . institutionGuid
61+ } ) )
62+ setIdentityProviders ( identityProviderOptions ) ;
63+ } ) ;
64+ }
4665 } ) ;
4766 } , [ ] ) ;
4867
@@ -70,11 +89,11 @@ export const Tokens = () => {
7089 } ;
7190
7291 const submitNewToken = ( ) => {
73- if ( isEmpty ( description ) ) {
92+ if ( isEmpty ( description ) || ( user . superUser && isEmpty ( organizationGUIDIdentityProvider ) ) ) {
7493 setInitial ( false ) ;
7594 } else {
7695 setLoading ( true ) ;
77- createToken ( description ) . then ( ( ) => {
96+ createToken ( description , organizationGUIDIdentityProvider . institutionGuid ) . then ( ( ) => {
7897 setFlash ( I18n . t ( "tokens.createFlash" ) ) ;
7998 fetchTokens ( ) ;
8099 } ) ;
@@ -84,18 +103,26 @@ export const Tokens = () => {
84103
85104 const cancelSideScreen = e => {
86105 stopEvent ( e ) ;
106+ setInitial ( true ) ;
87107 setNewToken ( false ) ;
108+ setDescription ( "" ) ;
109+ setOrganizationGUIDIdentityProvider ( { } ) ;
88110 }
89111
90112 const createNewToken = ( ) => {
91113 setLoading ( true ) ;
114+ setInitial ( true ) ;
92115 generateToken ( ) . then ( res => {
93116 setNewToken ( true ) ;
94117 setTokenValue ( res . token ) ;
95118 setLoading ( false ) ;
96119 } ) ;
97120 }
98121
122+ const changeOrganizationGUIDIdentityProvider = option => {
123+ setOrganizationGUIDIdentityProvider ( option ) ;
124+ }
125+
99126 const renderNewToken = ( ) => {
100127 return (
101128 < Page className = { "page new-token" } >
@@ -125,12 +152,30 @@ export const Tokens = () => {
125152 { ( ! initial && isEmpty ( description ) ) && < ErrorIndicator
126153 msg = { I18n . t ( "tokens.required" ) } /> }
127154
155+ { user . superUser &&
156+ < SelectField name = { I18n . t ( "roles.identityProvider" ) }
157+ toolTip = { I18n . t ( "tooltips.roleIdentityProvider" ) }
158+ value = { identityProviders . find ( idp => idp . value === organizationGUIDIdentityProvider . value ) }
159+ placeholder = { I18n . t ( "roles.identityProviderPlaceholder" ) }
160+ options = { identityProviders }
161+ onChange = { changeOrganizationGUIDIdentityProvider }
162+ searchable = { true }
163+ required = { true }
164+ /> }
165+ { ( ! initial && isEmpty ( organizationGUIDIdentityProvider . institutionGuid ) && user . superUser ) &&
166+ < ErrorIndicator msg = { I18n . t ( "forms.required" , {
167+ attribute : I18n . t ( "roles.organizationGUID" ) . toLowerCase ( )
168+ } ) } /> }
169+ { ! isEmpty ( organizationGUIDIdentityProvider . institutionGuid ) &&
170+ < em className = "info" > { I18n . t ( "roles.organizationGUIDValue" , { guid : organizationGUIDIdentityProvider . institutionGuid } ) } </ em > }
171+
128172 < section className = "actions" >
129173 < Button type = { ButtonType . Secondary }
130174 txt = { I18n . t ( "forms.cancel" ) }
131175 onClick = { ( ) => setNewToken ( false ) } />
132176 < Button txt = { I18n . t ( "forms.save" ) }
133- disabled = { ! initial && isEmpty ( description ) }
177+ disabled = { ! initial && ( isEmpty ( description ) ||
178+ ( user . superUser && isEmpty ( organizationGUIDIdentityProvider . institutionGuid ) ) ) }
134179 onClick = { ( ) => submitNewToken ( ) } />
135180 </ section >
136181 </ div >
0 commit comments