2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-06-18 02:05:19 +08:00
//
// 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 React from "react" ;
2021-06-20 09:46:06 +08:00
import { Button , Col , Result , Row } from "antd" ;
2021-06-18 02:05:19 +08:00
import * as ApplicationBackend from "../backend/ApplicationBackend" ;
2021-06-20 13:27:26 +08:00
import * as UserBackend from "../backend/UserBackend" ;
import * as AuthBackend from "./AuthBackend" ;
2021-06-18 02:05:19 +08:00
import * as Setting from "../Setting" ;
import i18next from "i18next" ;
import AffiliationSelect from "../common/AffiliationSelect" ;
2021-06-20 02:02:08 +08:00
import OAuthWidget from "../common/OAuthWidget" ;
2021-06-18 02:05:19 +08:00
class PromptPage extends React . Component {
constructor ( props ) {
super ( props ) ;
this . state = {
classes : props ,
type : props . type ,
applicationName : props . applicationName !== undefined ? props . applicationName : ( props . match === undefined ? null : props . match . params . applicationName ) ,
application : null ,
2021-06-20 02:02:08 +08:00
user : null ,
2021-06-18 02:05:19 +08:00
} ;
}
UNSAFE _componentWillMount ( ) {
2021-06-20 02:02:08 +08:00
this . getUser ( ) ;
2021-06-18 02:05:19 +08:00
this . getApplication ( ) ;
}
2021-06-20 02:02:08 +08:00
getUser ( ) {
const organizationName = this . props . account . owner ;
const userName = this . props . account . name ;
UserBackend . getUser ( organizationName , userName )
. then ( ( user ) => {
this . setState ( {
user : user ,
} ) ;
} ) ;
}
2021-06-18 02:05:19 +08:00
getApplication ( ) {
if ( this . state . applicationName === null ) {
return ;
}
ApplicationBackend . getApplication ( "admin" , this . state . applicationName )
. then ( ( application ) => {
this . setState ( {
application : application ,
} ) ;
} ) ;
}
getApplicationObj ( ) {
if ( this . props . application !== undefined ) {
return this . props . application ;
} else {
return this . state . application ;
}
}
parseUserField ( key , value ) {
// if ([].includes(key)) {
// value = Setting.myParseInt(value);
// }
return value ;
}
updateUserField ( key , value ) {
value = this . parseUserField ( key , value ) ;
let user = this . state . user ;
user [ key ] = value ;
this . setState ( {
user : user ,
} ) ;
2021-06-20 02:02:08 +08:00
this . submitUserEdit ( false ) ;
2021-06-18 02:05:19 +08:00
}
2021-06-20 02:02:08 +08:00
renderAffiliation ( application ) {
2021-06-20 09:46:06 +08:00
if ( ! Setting . isAffiliationPrompted ( application ) ) {
2021-06-18 23:43:36 +08:00
return null ;
}
2021-06-20 02:02:08 +08:00
if ( application === null || this . state . user === null ) {
2021-06-18 23:43:36 +08:00
return null ;
}
return (
2022-07-10 15:45:55 +08:00
< AffiliationSelect labelSpan = { 6 } application = { application } user = { this . state . user } onUpdateUserField = { ( key , value ) => { return this . updateUserField ( key , value ) ; } } / >
) ;
2021-06-18 23:43:36 +08:00
}
2021-06-20 02:02:08 +08:00
unlinked ( ) {
this . getUser ( ) ;
}
2021-06-18 02:05:19 +08:00
renderContent ( application ) {
return (
2022-07-10 15:45:55 +08:00
< div style = { { width : "400px" } } >
2021-06-18 02:05:19 +08:00
{
2021-06-18 23:43:36 +08:00
this . renderAffiliation ( application )
2021-06-18 02:05:19 +08:00
}
2021-06-20 02:02:08 +08:00
< div >
{
( application === null || this . state . user === null ) ? null : (
2022-07-10 15:45:55 +08:00
application ? . providers . filter ( providerItem => Setting . isProviderPrompted ( providerItem ) ) . map ( ( providerItem , index ) => < OAuthWidget key = { providerItem . name } labelSpan = { 6 } user = { this . state . user } application = { application } providerItem = { providerItem } onUnlinked = { ( ) => { return this . unlinked ( ) ; } } / > )
2021-06-20 02:02:08 +08:00
)
}
< / d i v >
2021-06-18 02:05:19 +08:00
< / d i v >
2022-07-10 15:45:55 +08:00
) ;
2021-06-18 02:05:19 +08:00
}
2021-06-20 22:17:03 +08:00
onUpdateAccount ( account ) {
this . props . onUpdateAccount ( account ) ;
2021-06-18 02:05:19 +08:00
}
2021-06-20 22:17:03 +08:00
getRedirectUrl ( ) {
// "/prompt/app-example?redirectUri=http://localhost:2000/callback&code=8eb113b072296818f090&state=app-example"
const params = new URLSearchParams ( this . props . location . search ) ;
const redirectUri = params . get ( "redirectUri" ) ;
const code = params . get ( "code" ) ;
const state = params . get ( "state" ) ;
if ( redirectUri === null || code === null || state === null ) {
return "" ;
2021-06-20 02:02:08 +08:00
}
2021-06-20 22:17:03 +08:00
return ` ${ redirectUri } ?code= ${ code } &state= ${ state } ` ;
2021-06-20 13:27:26 +08:00
}
logout ( ) {
AuthBackend . logout ( )
. then ( ( res ) => {
2022-07-10 15:45:55 +08:00
if ( res . status === "ok" ) {
2021-06-20 13:27:26 +08:00
this . onUpdateAccount ( null ) ;
2022-03-19 19:50:05 +08:00
let redirectUrl = this . getRedirectUrl ( ) ;
if ( redirectUrl === "" ) {
2022-07-10 15:45:55 +08:00
redirectUrl = res . data2 ;
2022-03-19 19:50:05 +08:00
}
2021-06-20 22:17:03 +08:00
if ( redirectUrl !== "" ) {
Setting . goToLink ( redirectUrl ) ;
} else {
Setting . goToLogin ( this , this . getApplicationObj ( ) ) ;
}
2021-06-20 13:27:26 +08:00
} else {
Setting . showMessage ( "error" , ` Failed to log out: ${ res . msg } ` ) ;
}
} ) ;
}
2021-06-20 02:02:08 +08:00
submitUserEdit ( isFinal ) {
2021-06-18 02:05:19 +08:00
let user = Setting . deepCopy ( this . state . user ) ;
UserBackend . updateUser ( this . state . user . owner , this . state . user . name , user )
. then ( ( res ) => {
if ( res . msg === "" ) {
2021-06-20 02:02:08 +08:00
if ( isFinal ) {
2022-07-10 15:45:55 +08:00
Setting . showMessage ( "success" , "Successfully saved" ) ;
2021-06-18 02:05:19 +08:00
2021-06-20 13:27:26 +08:00
this . logout ( ) ;
2021-06-20 02:02:08 +08:00
}
2021-06-18 02:05:19 +08:00
} else {
2021-06-20 02:02:08 +08:00
if ( isFinal ) {
Setting . showMessage ( "error" , res . msg ) ;
}
2021-06-18 02:05:19 +08:00
}
} )
. catch ( error => {
2021-06-20 02:02:08 +08:00
if ( isFinal ) {
Setting . showMessage ( "error" , ` Failed to connect to server: ${ error } ` ) ;
}
2021-06-18 02:05:19 +08:00
} ) ;
}
render ( ) {
const application = this . getApplicationObj ( ) ;
if ( application === null ) {
return null ;
}
2021-06-20 09:46:06 +08:00
if ( ! Setting . hasPromptPage ( application ) ) {
return (
< Result
status = "error"
title = "Sign Up Error"
subTitle = { "You are unexpected to see this prompt page" }
extra = { [
2021-07-16 17:04:16 +08:00
< Button type = "primary" key = "signin" onClick = { ( ) => {
2021-06-20 09:46:06 +08:00
Setting . goToLogin ( this , application ) ;
} } >
2021-07-16 17:04:16 +08:00
Sign In
< / B u t t o n >
2021-06-20 09:46:06 +08:00
] }
>
< / R e s u l t >
2022-07-10 15:45:55 +08:00
) ;
2021-06-20 09:46:06 +08:00
}
2021-06-18 02:05:19 +08:00
return (
< Row >
< Col span = { 24 } style = { { display : "flex" , justifyContent : "center" } } >
< div style = { { marginTop : "80px" , marginBottom : "50px" , textAlign : "center" } } >
{
Setting . renderHelmet ( application )
}
{
Setting . renderLogo ( application )
}
{
this . renderContent ( application )
}
< Row style = { { margin : 10 } } >
< Col span = { 18 } >
< / C o l >
< / R o w >
< div style = { { marginTop : "50px" } } >
2022-07-10 15:45:55 +08:00
< Button disabled = { ! Setting . isPromptAnswered ( this . state . user , application ) } type = "primary" size = "large" onClick = { ( ) => { this . submitUserEdit ( true ) ; } } > { i18next . t ( "code:Submit and complete" ) } < / B u t t o n >
2021-06-18 02:05:19 +08:00
< / d i v >
< / d i v >
< / C o l >
< / R o w >
2022-07-10 15:45:55 +08:00
) ;
2021-06-18 02:05:19 +08:00
}
}
export default PromptPage ;