/* * Copyright 2019 Red Hat, Inc. and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as React from 'react'; import {withRouter, RouteComponentProps} from 'react-router-dom'; import {AxiosResponse} from 'axios'; import { Badge, Button, DataList, DataListAction, DataListItemCells, DataListCell, DataListItemRow, Stack, StackItem, Title, TitleLevel, DataListItem, } from '@patternfly/react-core'; import { BitbucketIcon, CubeIcon, FacebookIcon, GithubIcon, GitlabIcon, GoogleIcon, InstagramIcon, LinkIcon, LinkedinIcon, MicrosoftIcon, OpenshiftIcon, PaypalIcon, StackOverflowIcon, TwitterIcon, UnlinkIcon } from '@patternfly/react-icons'; import {AccountServiceClient} from '../../account-service/account.service'; import {Msg} from '../../widgets/Msg'; import {ContentPage} from '../ContentPage'; import {createRedirect} from '../../util/RedirectUri'; interface LinkedAccount { connected: boolean; social: boolean; providerAlias: string; providerName: string; displayName: string; linkedUsername: string; } interface LinkedAccountsPageProps extends RouteComponentProps { } interface LinkedAccountsPageState { linkedAccounts: LinkedAccount[]; unLinkedAccounts: LinkedAccount[]; } /** * @author Stan Silvert */ class LinkedAccountsPage extends React.Component { public constructor(props: LinkedAccountsPageProps) { super(props); this.state = { linkedAccounts: [], unLinkedAccounts: [] } this.getLinkedAccounts(); } private getLinkedAccounts(): void { AccountServiceClient.Instance.doGet("/linked-accounts") .then((response: AxiosResponse) => { console.log({response}); const linkedAccounts = response.data.filter((account) => account.connected); const unLinkedAccounts = response.data.filter((account) => !account.connected); this.setState({linkedAccounts: linkedAccounts, unLinkedAccounts: unLinkedAccounts}); }); } private unLinkAccount(account: LinkedAccount): void { const url = '/linked-accounts/' + account.providerName; AccountServiceClient.Instance.doDelete(url) .then((response: AxiosResponse) => { console.log({response}); this.getLinkedAccounts(); }); } private linkAccount(account: LinkedAccount): void { const url = '/linked-accounts/' + account.providerName; const redirectUri: string = createRedirect(this.props.location.pathname); AccountServiceClient.Instance.doGet(url, { params: {providerId: account.providerName, redirectUri}}) .then((response: AxiosResponse<{accountLinkUri: string}>) => { console.log({response}); window.location.href = response.data.accountLinkUri; }); } public render(): React.ReactNode { return ( <Msg msgKey='linkedLoginProviders'/> {this.makeRows(this.state.linkedAccounts, true)} <Msg msgKey='unlinkedLoginProviders'/> {this.makeRows(this.state.unLinkedAccounts, false)} ); } private emptyRow(isLinked: boolean): React.ReactNode { let isEmptyMessage = ''; if (isLinked) { isEmptyMessage = Msg.localize('linkedEmpty'); } else { isEmptyMessage = Msg.localize('unlinkedEmpty'); } return ( {isEmptyMessage} ]}/> ) } private makeRows(accounts: LinkedAccount[], isLinked: boolean): React.ReactNode { if (accounts.length === 0) { return this.emptyRow(isLinked); } return ( <> { accounts.map( (account: LinkedAccount) => ( {this.findIcon(account)}

{account.displayName}

, {this.badge(account)}, {account.linkedUsername}, ]}/> {isLinked && } {!isLinked && }
)) } ) } private badge(account: LinkedAccount): React.ReactNode { if (account.social) { return (); } return (); } private findIcon(account: LinkedAccount): React.ReactNode { const socialIconId = `${account.providerAlias}-idp-icon-social`; if (account.providerName.toLowerCase().includes('github')) return (); if (account.providerName.toLowerCase().includes('linkedin')) return (); if (account.providerName.toLowerCase().includes('facebook')) return (); if (account.providerName.toLowerCase().includes('google')) return (); if (account.providerName.toLowerCase().includes('instagram')) return (); if (account.providerName.toLowerCase().includes('microsoft')) return (); if (account.providerName.toLowerCase().includes('bitbucket')) return (); if (account.providerName.toLowerCase().includes('twitter')) return (); if (account.providerName.toLowerCase().includes('openshift')) return (); if (account.providerName.toLowerCase().includes('gitlab')) return (); if (account.providerName.toLowerCase().includes('paypal')) return (); if (account.providerName.toLowerCase().includes('stackoverflow')) return (); return (); } }; const LinkedAccountsPagewithRouter = withRouter(LinkedAccountsPage); export {LinkedAccountsPagewithRouter as LinkedAccountsPage};