Fix prompt redirect logic and db sync bug.

This commit is contained in:
Yang Luo
2021-06-20 13:27:26 +08:00
parent d3a8ab8347
commit a43db3e55a
8 changed files with 126 additions and 7 deletions

View File

@ -22,6 +22,7 @@ import (
"strings" "strings"
"github.com/casdoor/casdoor/object" "github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/original"
"github.com/casdoor/casdoor/util" "github.com/casdoor/casdoor/util"
) )
@ -151,7 +152,11 @@ func (c *ApiController) Signup() {
IsForbidden: false, IsForbidden: false,
Properties: map[string]string{}, Properties: map[string]string{},
} }
object.AddUser(user)
affected := object.AddUser(user)
if affected {
original.AddUserToOriginalDatabase(user)
}
if application.HasPromptPage() { if application.HasPromptPage() {
// The prompt page needs the user to be signed in // The prompt page needs the user to be signed in

View File

@ -20,6 +20,7 @@ import (
"strings" "strings"
"github.com/casdoor/casdoor/object" "github.com/casdoor/casdoor/object"
"github.com/casdoor/casdoor/original"
) )
// @Title GetGlobalUsers // @Title GetGlobalUsers
@ -75,7 +76,13 @@ func (c *ApiController) UpdateUser() {
return return
} }
c.Data["json"] = wrapActionResponse(object.UpdateUser(id, &user)) affected := object.UpdateUser(id, &user)
if affected {
newUser := object.GetUser(user.GetId())
original.UpdateUserToOriginalDatabase(newUser)
}
c.Data["json"] = wrapActionResponse(affected)
c.ServeJSON() c.ServeJSON()
} }

View File

@ -33,6 +33,11 @@ func initConfig() {
} }
func initAdapter() { func initAdapter() {
if dbName == "dbName" {
adapter = nil
return
}
adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName) adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName)
createTable(adapter) createTable(adapter)
} }

51
original/public_api.go Normal file
View File

@ -0,0 +1,51 @@
// Copyright 2021 The casbin Authors. All Rights Reserved.
//
// 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.
package original
import (
"fmt"
"github.com/casdoor/casdoor/object"
)
func isEnabled() bool {
if adapter == nil {
initAdapter()
if adapter == nil {
return false
}
}
return true
}
func AddUserToOriginalDatabase(user *object.User) {
if !isEnabled() {
return
}
updatedOUser := createOriginalUserFromUser(user)
addUser(updatedOUser)
fmt.Printf("Add from user to oUser: %v\n", updatedOUser)
}
func UpdateUserToOriginalDatabase(user *object.User) {
if !isEnabled() {
return
}
updatedOUser := createOriginalUserFromUser(user)
updateUser(updatedOUser)
fmt.Printf("Update from user to oUser: %v\n", updatedOUser)
}

View File

@ -55,6 +55,15 @@ func getUserMapOriginal() ([]*User, map[string]*User) {
return users, m return users, m
} }
func addUser(user *User) bool {
affected, err := adapter.Engine.Insert(user)
if err != nil {
panic(err)
}
return affected != 0
}
func updateUser(user *User) bool { func updateUser(user *User) bool {
affected, err := adapter.Engine.ID(user.Id).Cols("name", "password", "cellphone", "school_id", "avatar", "deleted").Update(user) affected, err := adapter.Engine.ID(user.Id).Cols("name", "password", "cellphone", "school_id", "avatar", "deleted").Update(user)
if err != nil { if err != nil {

View File

@ -178,6 +178,12 @@ class App extends Component {
}); });
} }
onUpdateAccount(account) {
this.setState({
account: account
});
}
handleRightDropdownClick(e) { handleRightDropdownClick(e) {
if (e.key === '201') { if (e.key === '201') {
this.props.history.push(`/account`); this.props.history.push(`/account`);
@ -422,7 +428,7 @@ class App extends Component {
return ( return (
<Switch> <Switch>
<Route exact path="/signup" render={(props) => this.renderHomeIfLoggedIn(<SignupPage {...props} />)}/> <Route exact path="/signup" render={(props) => this.renderHomeIfLoggedIn(<SignupPage {...props} />)}/>
<Route exact path="/signup/:applicationName" render={(props) => this.renderHomeIfLoggedIn(<SignupPage {...props} />)}/> <Route exact path="/signup/:applicationName" render={(props) => this.renderHomeIfLoggedIn(<SignupPage {...props} onUpdateAccount={(account) => {this.onUpdateAccount(account)}} />)}/>
<Route exact path="/login" render={(props) => this.renderHomeIfLoggedIn(<SelfLoginPage {...props} />)}/> <Route exact path="/login" render={(props) => this.renderHomeIfLoggedIn(<SelfLoginPage {...props} />)}/>
<Route exact path="/signup/oauth/authorize" render={(props) => <LoginPage type={"code"} mode={"signup"} {...props} />}/> <Route exact path="/signup/oauth/authorize" render={(props) => <LoginPage type={"code"} mode={"signup"} {...props} />}/>
<Route exact path="/login/oauth/authorize" render={(props) => <LoginPage type={"code"} mode={"signin"} {...props} />}/> <Route exact path="/login/oauth/authorize" render={(props) => <LoginPage type={"code"} mode={"signin"} {...props} />}/>
@ -430,7 +436,7 @@ class App extends Component {
<Route exact path="/forget" render={(props) => this.renderHomeIfLoggedIn(<SelfForgetPage {...props} />)}/> <Route exact path="/forget" render={(props) => this.renderHomeIfLoggedIn(<SelfForgetPage {...props} />)}/>
<Route exact path="/forget/:applicationName" render={(props) => this.renderHomeIfLoggedIn(<ForgetPage {...props} />)}/> <Route exact path="/forget/:applicationName" render={(props) => this.renderHomeIfLoggedIn(<ForgetPage {...props} />)}/>
<Route exact path="/prompt" render={(props) => this.renderLoginIfNotLoggedIn(<PromptPage account={this.state.account} {...props} />)}/> <Route exact path="/prompt" render={(props) => this.renderLoginIfNotLoggedIn(<PromptPage account={this.state.account} {...props} />)}/>
<Route exact path="/prompt/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<PromptPage account={this.state.account} {...props} />)}/> <Route exact path="/prompt/:applicationName" render={(props) => this.renderLoginIfNotLoggedIn(<PromptPage account={this.state.account} onUpdateAccount={(account) => {this.onUpdateAccount(account)}} {...props} />)}/>
</Switch> </Switch>
) )
} }

View File

@ -16,10 +16,11 @@ import React from "react";
import {Link} from "react-router-dom"; import {Link} from "react-router-dom";
import {Button, Col, Result, Row} from "antd"; import {Button, Col, Result, Row} from "antd";
import * as ApplicationBackend from "../backend/ApplicationBackend"; import * as ApplicationBackend from "../backend/ApplicationBackend";
import * as UserBackend from "../backend/UserBackend";
import * as AuthBackend from "./AuthBackend";
import * as Setting from "../Setting"; import * as Setting from "../Setting";
import i18next from "i18next"; import i18next from "i18next";
import AffiliationSelect from "../common/AffiliationSelect"; import AffiliationSelect from "../common/AffiliationSelect";
import * as UserBackend from "../backend/UserBackend";
import OAuthWidget from "../common/OAuthWidget"; import OAuthWidget from "../common/OAuthWidget";
class PromptPage extends React.Component { class PromptPage extends React.Component {
@ -160,6 +161,23 @@ class PromptPage extends React.Component {
return true; return true;
} }
onUpdateAccount(account) {
this.props.onUpdateAccount(account);
}
logout() {
AuthBackend.logout()
.then((res) => {
if (res.status === 'ok') {
this.onUpdateAccount(null);
Setting.goToLogin(this, this.getApplicationObj());
} else {
Setting.showMessage("error", `Failed to log out: ${res.msg}`);
}
});
}
submitUserEdit(isFinal) { submitUserEdit(isFinal) {
let user = Setting.deepCopy(this.state.user); let user = Setting.deepCopy(this.state.user);
UserBackend.updateUser(this.state.user.owner, this.state.user.name, user) UserBackend.updateUser(this.state.user.owner, this.state.user.name, user)
@ -168,7 +186,7 @@ class PromptPage extends React.Component {
if (isFinal) { if (isFinal) {
Setting.showMessage("success", `Successfully saved`); Setting.showMessage("success", `Successfully saved`);
Setting.goToLogin(this, this.getApplicationObj()); this.logout();
} }
} else { } else {
if (isFinal) { if (isFinal) {

View File

@ -113,13 +113,31 @@ class SignupPage extends React.Component {
} }
} }
onUpdateAccount(account) {
this.props.onUpdateAccount(account);
}
onFinish(values) { onFinish(values) {
const application = this.getApplicationObj(); const application = this.getApplicationObj();
values.phonePrefix = application.organizationObj.phonePrefix; values.phonePrefix = application.organizationObj.phonePrefix;
AuthBackend.signup(values) AuthBackend.signup(values)
.then((res) => { .then((res) => {
if (res.status === 'ok') { if (res.status === 'ok') {
AuthBackend.getAccount("")
.then((res) => {
let account = null;
if (res.status === "ok") {
account = res.data;
account.organization = res.data2;
this.onUpdateAccount(account);
Setting.goToLinkSoft(this, this.getResultPath(application)); Setting.goToLinkSoft(this, this.getResultPath(application));
} else {
if (res.msg !== "Please sign in first") {
Setting.showMessage("error", `Failed to sign in: ${res.msg}`);
}
}
});
} else { } else {
Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); Setting.showMessage("error", i18next.t(`signup:${res.msg}`));
} }