diff --git a/controllers/account.go b/controllers/account.go index 21ab94ee..2005beab 100644 --- a/controllers/account.go +++ b/controllers/account.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/casdoor/casdoor/object" + "github.com/casdoor/casdoor/original" "github.com/casdoor/casdoor/util" ) @@ -151,7 +152,11 @@ func (c *ApiController) Signup() { IsForbidden: false, Properties: map[string]string{}, } - object.AddUser(user) + + affected := object.AddUser(user) + if affected { + original.AddUserToOriginalDatabase(user) + } if application.HasPromptPage() { // The prompt page needs the user to be signed in diff --git a/controllers/user.go b/controllers/user.go index fa4d10f7..477be264 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -20,6 +20,7 @@ import ( "strings" "github.com/casdoor/casdoor/object" + "github.com/casdoor/casdoor/original" ) // @Title GetGlobalUsers @@ -75,7 +76,13 @@ func (c *ApiController) UpdateUser() { 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() } diff --git a/original/adapter.go b/original/adapter.go index c0250858..39d0409e 100644 --- a/original/adapter.go +++ b/original/adapter.go @@ -33,6 +33,11 @@ func initConfig() { } func initAdapter() { + if dbName == "dbName" { + adapter = nil + return + } + adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName) createTable(adapter) } diff --git a/original/public_api.go b/original/public_api.go new file mode 100644 index 00000000..d62a51b7 --- /dev/null +++ b/original/public_api.go @@ -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) +} diff --git a/original/user_original.go b/original/user_original.go index 1f99e6c2..ade91d69 100644 --- a/original/user_original.go +++ b/original/user_original.go @@ -55,6 +55,15 @@ func getUserMapOriginal() ([]*User, map[string]*User) { 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 { affected, err := adapter.Engine.ID(user.Id).Cols("name", "password", "cellphone", "school_id", "avatar", "deleted").Update(user) if err != nil { diff --git a/web/src/App.js b/web/src/App.js index e4b05276..0b4f0ac8 100644 --- a/web/src/App.js +++ b/web/src/App.js @@ -178,6 +178,12 @@ class App extends Component { }); } + onUpdateAccount(account) { + this.setState({ + account: account + }); + } + handleRightDropdownClick(e) { if (e.key === '201') { this.props.history.push(`/account`); @@ -422,7 +428,7 @@ class App extends Component { return ( this.renderHomeIfLoggedIn()}/> - this.renderHomeIfLoggedIn()}/> + this.renderHomeIfLoggedIn( {this.onUpdateAccount(account)}} />)}/> this.renderHomeIfLoggedIn()}/> }/> }/> @@ -430,7 +436,7 @@ class App extends Component { this.renderHomeIfLoggedIn()}/> this.renderHomeIfLoggedIn()}/> this.renderLoginIfNotLoggedIn()}/> - this.renderLoginIfNotLoggedIn()}/> + this.renderLoginIfNotLoggedIn( {this.onUpdateAccount(account)}} {...props} />)}/> ) } diff --git a/web/src/auth/PromptPage.js b/web/src/auth/PromptPage.js index 8d0bb662..ca6eebba 100644 --- a/web/src/auth/PromptPage.js +++ b/web/src/auth/PromptPage.js @@ -16,10 +16,11 @@ import React from "react"; import {Link} from "react-router-dom"; import {Button, Col, Result, Row} from "antd"; import * as ApplicationBackend from "../backend/ApplicationBackend"; +import * as UserBackend from "../backend/UserBackend"; +import * as AuthBackend from "./AuthBackend"; import * as Setting from "../Setting"; import i18next from "i18next"; import AffiliationSelect from "../common/AffiliationSelect"; -import * as UserBackend from "../backend/UserBackend"; import OAuthWidget from "../common/OAuthWidget"; class PromptPage extends React.Component { @@ -160,6 +161,23 @@ class PromptPage extends React.Component { 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) { let user = Setting.deepCopy(this.state.user); UserBackend.updateUser(this.state.user.owner, this.state.user.name, user) @@ -168,7 +186,7 @@ class PromptPage extends React.Component { if (isFinal) { Setting.showMessage("success", `Successfully saved`); - Setting.goToLogin(this, this.getApplicationObj()); + this.logout(); } } else { if (isFinal) { diff --git a/web/src/auth/SignupPage.js b/web/src/auth/SignupPage.js index aaca4e9f..a3a526c6 100644 --- a/web/src/auth/SignupPage.js +++ b/web/src/auth/SignupPage.js @@ -113,13 +113,31 @@ class SignupPage extends React.Component { } } + onUpdateAccount(account) { + this.props.onUpdateAccount(account); + } + onFinish(values) { const application = this.getApplicationObj(); values.phonePrefix = application.organizationObj.phonePrefix; AuthBackend.signup(values) .then((res) => { if (res.status === 'ok') { - Setting.goToLinkSoft(this, this.getResultPath(application)); + 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)); + } else { + if (res.msg !== "Please sign in first") { + Setting.showMessage("error", `Failed to sign in: ${res.msg}`); + } + } + }); } else { Setting.showMessage("error", i18next.t(`signup:${res.msg}`)); }