-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
225 additions
and
6 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
frontend/awx/access/users/UserPage/UserTokenSecretsModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { t } from 'i18next'; | ||
import { SetStateAction } from 'react'; | ||
import { Modal, ClipboardCopy, ClipboardCopyVariant } from '@patternfly/react-core'; | ||
import { PageDetails, PageDetail } from '../../../../../framework'; | ||
import { formatDateString } from '../../../../../framework/utils/formatDateString'; | ||
import { Token } from '../../../interfaces/Token'; | ||
|
||
export function UserTokenSecretsModal(props: { | ||
onClose: (value: SetStateAction<Token | undefined>) => void; | ||
newToken: Token; | ||
}) { | ||
const { token, refresh_token } = props.newToken; | ||
return ( | ||
<Modal | ||
aria-label={t`Token information`} | ||
isOpen | ||
variant="medium" | ||
position="top" | ||
title={t('Token information')} | ||
onClose={() => { | ||
props.onClose(undefined); | ||
}} | ||
hasNoBodyWrapper | ||
> | ||
<PageDetails | ||
alertPrompts={[t`This is the only time the token will be shown.`]} | ||
numberOfColumns="single" | ||
> | ||
<PageDetail label={t`Token`}> | ||
<ClipboardCopy isReadOnly variant={ClipboardCopyVariant.expansion}> | ||
{token} | ||
</ClipboardCopy> | ||
</PageDetail> | ||
{refresh_token && ( | ||
<PageDetail label={t`Refresh Token`}> | ||
<ClipboardCopy isReadOnly variant={ClipboardCopyVariant.expansion}> | ||
{refresh_token} | ||
</ClipboardCopy> | ||
</PageDetail> | ||
)} | ||
<PageDetail label={t`Expires`}>{formatDateString(props.newToken.expires)}</PageDetail> | ||
</PageDetails> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { useEffect } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
import { | ||
LoadingPage, | ||
PageFormSelect, | ||
PageFormSubmitHandler, | ||
PageHeader, | ||
useGetPageUrl, | ||
usePageNavigate, | ||
} from '../../../../framework'; | ||
import { useAwxActiveUser } from '../../common/useAwxActiveUser'; | ||
import { AwxRoute } from '../../main/AwxRoutes'; | ||
import { AwxUser } from '../../interfaces/User'; | ||
import { Token } from '../../interfaces/Token'; | ||
import { AwxPageForm } from '../../common/AwxPageForm'; | ||
import { PageFormTextInput } from '../../../../framework'; | ||
import { usePostRequest } from '../../../common/crud/usePostRequest'; | ||
import { awxAPI } from '../../common/api/awx-utils'; | ||
import { PageFormApplicationSelect } from '../../administration/applications/components/PageFormApplicationSelect'; | ||
|
||
export function CreateUserToken(props: { onSuccessfulCreate: (newToken: Token) => void }) { | ||
const params = useParams<{ id: string }>(); | ||
const { activeAwxUser } = useAwxActiveUser(); | ||
const pageNavigate = usePageNavigate(); | ||
|
||
useEffect(() => { | ||
if (activeAwxUser === undefined || activeAwxUser?.id.toString() !== params.id) { | ||
// redirect to user details for the active/logged-in user | ||
pageNavigate(AwxRoute.UserDetails, { params: { id: activeAwxUser?.id } }); | ||
} | ||
}, [activeAwxUser, params.id, pageNavigate]); | ||
|
||
if (!activeAwxUser) return <LoadingPage breadcrumbs tabs />; | ||
|
||
return activeAwxUser?.id.toString() === params.id ? ( | ||
<CreateUserTokenInternal user={activeAwxUser} onCreate={props.onSuccessfulCreate} /> | ||
) : ( | ||
<></> | ||
); | ||
} | ||
|
||
function CreateUserTokenInternal(props: { user: AwxUser; onCreate: (newToken: Token) => void }) { | ||
const { user } = props; | ||
const { t } = useTranslation(); | ||
const getPageUrl = useGetPageUrl(); | ||
const navigate = useNavigate(); | ||
const postRequest = usePostRequest<Token, Token>(); | ||
const pageNavigate = usePageNavigate(); | ||
|
||
const onCancel = () => navigate(-1); | ||
const onSubmit: PageFormSubmitHandler<Token> = async (tokenInput) => { | ||
const newToken = await postRequest(awxAPI`/tokens/`, tokenInput); | ||
props.onCreate(newToken); | ||
pageNavigate(AwxRoute.UserTokenDetails, { params: { id: user.id, tokenid: newToken.id } }); | ||
}; | ||
|
||
return ( | ||
<> | ||
<PageHeader | ||
title={t('Create Token')} | ||
breadcrumbs={[ | ||
{ label: t('Users'), to: getPageUrl(AwxRoute.Users) }, | ||
{ | ||
label: user.username, | ||
to: getPageUrl(AwxRoute.UserDetails, { params: { id: user.id } }), | ||
}, | ||
{ | ||
label: t('Tokens'), | ||
to: getPageUrl(AwxRoute.UserTokens, { params: { id: user.id } }), | ||
}, | ||
]} | ||
/> | ||
<AwxPageForm | ||
submitText={t('Create token')} | ||
onSubmit={onSubmit} | ||
cancelText={t('Cancel')} | ||
onCancel={onCancel} | ||
> | ||
<UserTokenFormInputs /> | ||
</AwxPageForm> | ||
</> | ||
); | ||
} | ||
|
||
function UserTokenFormInputs() { | ||
const { t } = useTranslation(); | ||
return ( | ||
<> | ||
<PageFormApplicationSelect name="application" isRequired={false} /> | ||
<PageFormTextInput<Token> | ||
name="description" | ||
label={t('Description')} | ||
placeholder={t('Enter token description')} | ||
isRequired={false} | ||
/> | ||
<PageFormSelect<Token> | ||
name="scope" | ||
label={t('Scope')} | ||
placeholderText={t('Select a scope')} | ||
options={[ | ||
{ | ||
label: t('Read'), | ||
value: 'read', | ||
}, | ||
{ | ||
label: t('Write'), | ||
value: 'write', | ||
}, | ||
]} | ||
isRequired | ||
/> | ||
</> | ||
); | ||
} |
32 changes: 32 additions & 0 deletions
32
frontend/awx/administration/applications/components/PageFormApplicationSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { FieldPath, FieldValues } from 'react-hook-form'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { PageFormSingleSelectAwxResource } from '../../../common/PageFormSingleSelectAwxResource'; | ||
import { awxAPI } from '../../../common/api/awx-utils'; | ||
import { useApplicationsColumns } from '../hooks/useApplicationsColumns'; | ||
import { useApplicationsFilters } from '../hooks/useApplicationsFilters'; | ||
import { Application } from '../../../interfaces/Application'; | ||
|
||
export function PageFormApplicationSelect< | ||
TFieldValues extends FieldValues = FieldValues, | ||
TFieldName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>, | ||
>(props: { name: TFieldName; isRequired?: boolean; isDisabled?: string; helperText?: string }) { | ||
const { t } = useTranslation(); | ||
const applicationColumns = useApplicationsColumns({ disableLinks: true }); | ||
const applicationFilters = useApplicationsFilters(); | ||
return ( | ||
<PageFormSingleSelectAwxResource<Application, TFieldValues, TFieldName> | ||
name={props.name} | ||
id="application" | ||
label={t('Application')} | ||
placeholder={t('Select application')} | ||
queryPlaceholder={t('Loading applications...')} | ||
queryErrorText={t('Error loading applications')} | ||
isRequired={props.isRequired} | ||
isDisabled={props.isDisabled} | ||
helperText={props.helperText} | ||
url={awxAPI`/applications/`} | ||
tableColumns={applicationColumns} | ||
toolbarFilters={applicationFilters} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters