mirror of
https://github.com/casdoor/casdoor.git
synced 2025-05-23 18:54:03 +08:00
Return error for RunSyncer()
This commit is contained in:
parent
7e3c361ce7
commit
988b26b3c2
@ -160,7 +160,11 @@ func (c *ApiController) RunSyncer() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
object.RunSyncer(syncer)
|
err = object.RunSyncer(syncer)
|
||||||
|
if err != nil {
|
||||||
|
c.ResponseError(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.ResponseOk()
|
c.ResponseOk()
|
||||||
}
|
}
|
||||||
|
@ -250,7 +250,7 @@ func (syncer *Syncer) getKey() string {
|
|||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunSyncer(syncer *Syncer) {
|
func RunSyncer(syncer *Syncer) error {
|
||||||
syncer.initAdapter()
|
syncer.initAdapter()
|
||||||
syncer.syncUsers()
|
return syncer.syncUsers()
|
||||||
}
|
}
|
||||||
|
@ -52,11 +52,14 @@ func addSyncerJob(syncer *Syncer) error {
|
|||||||
|
|
||||||
syncer.initAdapter()
|
syncer.initAdapter()
|
||||||
|
|
||||||
syncer.syncUsers()
|
err := syncer.syncUsers()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
schedule := fmt.Sprintf("@every %ds", syncer.SyncInterval)
|
schedule := fmt.Sprintf("@every %ds", syncer.SyncInterval)
|
||||||
cron := getCronMap(syncer.Name)
|
cron := getCronMap(syncer.Name)
|
||||||
_, err := cron.AddFunc(schedule, syncer.syncUsers)
|
_, err = cron.AddFunc(schedule, syncer.syncUsersNoError)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -19,9 +19,9 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (syncer *Syncer) syncUsers() {
|
func (syncer *Syncer) syncUsers() error {
|
||||||
if len(syncer.TableColumns) == 0 {
|
if len(syncer.TableColumns) == 0 {
|
||||||
return
|
return fmt.Errorf("The syncer table columns should not be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Running syncUsers()..\n")
|
fmt.Printf("Running syncUsers()..\n")
|
||||||
@ -35,10 +35,8 @@ func (syncer *Syncer) syncUsers() {
|
|||||||
line := fmt.Sprintf("[%s] %s\n", timestamp, err.Error())
|
line := fmt.Sprintf("[%s] %s\n", timestamp, err.Error())
|
||||||
_, err = updateSyncerErrorText(syncer, line)
|
_, err = updateSyncerErrorText(syncer, line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Users: %d, oUsers: %d\n", len(users), len(oUsers))
|
fmt.Printf("Users: %d, oUsers: %d\n", len(users), len(oUsers))
|
||||||
@ -71,28 +69,30 @@ func (syncer *Syncer) syncUsers() {
|
|||||||
updatedUser := syncer.createUserFromOriginalUser(oUser, affiliationMap)
|
updatedUser := syncer.createUserFromOriginalUser(oUser, affiliationMap)
|
||||||
updatedUser.Hash = oHash
|
updatedUser.Hash = oHash
|
||||||
updatedUser.PreHash = oHash
|
updatedUser.PreHash = oHash
|
||||||
|
|
||||||
|
fmt.Printf("Update from oUser to user: %v\n", updatedUser)
|
||||||
_, err = syncer.updateUserForOriginalByFields(updatedUser, key)
|
_, err = syncer.updateUserForOriginalByFields(updatedUser, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("Update from oUser to user: %v\n", updatedUser)
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if user.PreHash == oHash {
|
if user.PreHash == oHash {
|
||||||
if !syncer.IsReadOnly {
|
if !syncer.IsReadOnly {
|
||||||
updatedOUser := syncer.createOriginalUserFromUser(user)
|
updatedOUser := syncer.createOriginalUserFromUser(user)
|
||||||
|
|
||||||
|
fmt.Printf("Update from user to oUser: %v\n", updatedOUser)
|
||||||
_, err = syncer.updateUser(updatedOUser)
|
_, err = syncer.updateUser(updatedOUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("Update from user to oUser: %v\n", updatedOUser)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update preHash
|
// update preHash
|
||||||
user.PreHash = user.Hash
|
user.PreHash = user.Hash
|
||||||
_, err = SetUserField(user, "pre_hash", user.PreHash)
|
_, err = SetUserField(user, "pre_hash", user.PreHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if user.Hash == oHash {
|
if user.Hash == oHash {
|
||||||
@ -100,17 +100,18 @@ func (syncer *Syncer) syncUsers() {
|
|||||||
user.PreHash = user.Hash
|
user.PreHash = user.Hash
|
||||||
_, err = SetUserField(user, "pre_hash", user.PreHash)
|
_, err = SetUserField(user, "pre_hash", user.PreHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
updatedUser := syncer.createUserFromOriginalUser(oUser, affiliationMap)
|
updatedUser := syncer.createUserFromOriginalUser(oUser, affiliationMap)
|
||||||
updatedUser.Hash = oHash
|
updatedUser.Hash = oHash
|
||||||
updatedUser.PreHash = oHash
|
updatedUser.PreHash = oHash
|
||||||
|
|
||||||
|
fmt.Printf("Update from oUser to user (2nd condition): %v\n", updatedUser)
|
||||||
_, err = syncer.updateUserForOriginalByFields(updatedUser, key)
|
_, err = syncer.updateUserForOriginalByFields(updatedUser, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
fmt.Printf("Update from oUser to user (2nd condition): %v\n", updatedUser)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -118,7 +119,7 @@ func (syncer *Syncer) syncUsers() {
|
|||||||
}
|
}
|
||||||
_, err = AddUsersInBatch(newUsers)
|
_, err = AddUsersInBatch(newUsers)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !syncer.IsReadOnly {
|
if !syncer.IsReadOnly {
|
||||||
@ -126,12 +127,22 @@ func (syncer *Syncer) syncUsers() {
|
|||||||
id := user.Id
|
id := user.Id
|
||||||
if _, ok := oUserMap[id]; !ok {
|
if _, ok := oUserMap[id]; !ok {
|
||||||
newOUser := syncer.createOriginalUserFromUser(user)
|
newOUser := syncer.createOriginalUserFromUser(user)
|
||||||
|
|
||||||
|
fmt.Printf("New oUser: %v\n", newOUser)
|
||||||
_, err = syncer.addUser(newOUser)
|
_, err = syncer.addUser(newOUser)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (syncer *Syncer) syncUsersNoError() {
|
||||||
|
err := syncer.syncUsers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("New oUser: %v\n", newOUser)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -234,12 +234,30 @@ class SyncerEditPage extends React.Component {
|
|||||||
</Select>
|
</Select>
|
||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
|
<Row style={{marginTop: "20px"}} >
|
||||||
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
||||||
|
{Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} :
|
||||||
|
</Col>
|
||||||
|
<Col span={22} >
|
||||||
|
<Select virtual={false} style={{width: "100%"}} value={this.state.syncer.databaseType} onChange={(value => {this.updateSyncerField("databaseType", value);})}>
|
||||||
|
{
|
||||||
|
[
|
||||||
|
{id: "mysql", name: "MySQL"},
|
||||||
|
{id: "postgres", name: "PostgreSQL"},
|
||||||
|
{id: "mssql", name: "SQL Server"},
|
||||||
|
{id: "oracle", name: "Oracle"},
|
||||||
|
{id: "sqlite3", name: "Sqlite 3"},
|
||||||
|
].map((databaseType, index) => <Option key={index} value={databaseType.id}>{databaseType.name}</Option>)
|
||||||
|
}
|
||||||
|
</Select>
|
||||||
|
</Col>
|
||||||
|
</Row>
|
||||||
<Row style={{marginTop: "20px"}} >
|
<Row style={{marginTop: "20px"}} >
|
||||||
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
||||||
{Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} :
|
{Setting.getLabel(i18next.t("provider:Host"), i18next.t("provider:Host - Tooltip"))} :
|
||||||
</Col>
|
</Col>
|
||||||
<Col span={22} >
|
<Col span={22} >
|
||||||
<Input value={this.state.syncer.host} onChange={e => {
|
<Input prefix={<LinkOutlined />} value={this.state.syncer.host} onChange={e => {
|
||||||
this.updateSyncerField("host", e.target.value);
|
this.updateSyncerField("host", e.target.value);
|
||||||
}} />
|
}} />
|
||||||
</Col>
|
</Col>
|
||||||
@ -274,24 +292,6 @@ class SyncerEditPage extends React.Component {
|
|||||||
}} />
|
}} />
|
||||||
</Col>
|
</Col>
|
||||||
</Row>
|
</Row>
|
||||||
<Row style={{marginTop: "20px"}} >
|
|
||||||
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
|
||||||
{Setting.getLabel(i18next.t("syncer:Database type"), i18next.t("syncer:Database type - Tooltip"))} :
|
|
||||||
</Col>
|
|
||||||
<Col span={22} >
|
|
||||||
<Select virtual={false} style={{width: "100%"}} value={this.state.syncer.databaseType} onChange={(value => {this.updateSyncerField("databaseType", value);})}>
|
|
||||||
{
|
|
||||||
[
|
|
||||||
{id: "mysql", name: "MySQL"},
|
|
||||||
{id: "postgres", name: "PostgreSQL"},
|
|
||||||
{id: "mssql", name: "SQL Server"},
|
|
||||||
{id: "oracle", name: "Oracle"},
|
|
||||||
{id: "sqlite3", name: "Sqlite 3"},
|
|
||||||
].map((databaseType, index) => <Option key={index} value={databaseType.id}>{databaseType.name}</Option>)
|
|
||||||
}
|
|
||||||
</Select>
|
|
||||||
</Col>
|
|
||||||
</Row>
|
|
||||||
<Row style={{marginTop: "20px"}} >
|
<Row style={{marginTop: "20px"}} >
|
||||||
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
|
||||||
{Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} :
|
{Setting.getLabel(i18next.t("syncer:Database"), i18next.t("syncer:Database - Tooltip"))} :
|
||||||
|
@ -86,8 +86,13 @@ class SyncerListPage extends BaseListPage {
|
|||||||
this.setState({loading: true});
|
this.setState({loading: true});
|
||||||
SyncerBackend.runSyncer("admin", this.state.data[i].name)
|
SyncerBackend.runSyncer("admin", this.state.data[i].name)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
if (res.status === "ok") {
|
||||||
this.setState({loading: false});
|
this.setState({loading: false});
|
||||||
Setting.showMessage("success", "Syncer sync users successfully");
|
Setting.showMessage("success", i18next.t("general:Successfully synced"));
|
||||||
|
} else {
|
||||||
|
this.setState({loading: false});
|
||||||
|
Setting.showMessage("error", `${i18next.t("general:Failed to sync")}: ${res.msg}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
@ -151,6 +156,13 @@ class SyncerListPage extends BaseListPage {
|
|||||||
{text: "LDAP", value: "LDAP"},
|
{text: "LDAP", value: "LDAP"},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: i18next.t("syncer:Database type"),
|
||||||
|
dataIndex: "databaseType",
|
||||||
|
key: "databaseType",
|
||||||
|
width: "130px",
|
||||||
|
sorter: (a, b) => a.databaseType.localeCompare(b.databaseType),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: i18next.t("provider:Host"),
|
title: i18next.t("provider:Host"),
|
||||||
dataIndex: "host",
|
dataIndex: "host",
|
||||||
@ -183,13 +195,6 @@ class SyncerListPage extends BaseListPage {
|
|||||||
sorter: true,
|
sorter: true,
|
||||||
...this.getColumnSearchProps("password"),
|
...this.getColumnSearchProps("password"),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
title: i18next.t("syncer:Database type"),
|
|
||||||
dataIndex: "databaseType",
|
|
||||||
key: "databaseType",
|
|
||||||
width: "120px",
|
|
||||||
sorter: (a, b) => a.databaseType.localeCompare(b.databaseType),
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
title: i18next.t("syncer:Database"),
|
title: i18next.t("syncer:Database"),
|
||||||
dataIndex: "database",
|
dataIndex: "database",
|
||||||
@ -208,7 +213,7 @@ class SyncerListPage extends BaseListPage {
|
|||||||
title: i18next.t("syncer:Sync interval"),
|
title: i18next.t("syncer:Sync interval"),
|
||||||
dataIndex: "syncInterval",
|
dataIndex: "syncInterval",
|
||||||
key: "syncInterval",
|
key: "syncInterval",
|
||||||
width: "130px",
|
width: "140px",
|
||||||
sorter: true,
|
sorter: true,
|
||||||
...this.getColumnSearchProps("syncInterval"),
|
...this.getColumnSearchProps("syncInterval"),
|
||||||
},
|
},
|
||||||
|
@ -96,7 +96,7 @@ class SyncerTableColumnTable extends React.Component {
|
|||||||
key: "casdoorName",
|
key: "casdoorName",
|
||||||
render: (text, record, index) => {
|
render: (text, record, index) => {
|
||||||
return (
|
return (
|
||||||
<Select virtual={false} style={{width: "100%"}} value={text} onChange={(value => {this.updateField(table, index, "casdoorName", value);})}>
|
<Select virtual={false} showSearch style={{width: "100%"}} value={text} onChange={(value => {this.updateField(table, index, "casdoorName", value);})}>
|
||||||
{
|
{
|
||||||
["Name", "CreatedTime", "UpdatedTime", "Id", "Type", "Password", "PasswordSalt", "DisplayName", "FirstName", "LastName", "Avatar", "PermanentAvatar",
|
["Name", "CreatedTime", "UpdatedTime", "Id", "Type", "Password", "PasswordSalt", "DisplayName", "FirstName", "LastName", "Avatar", "PermanentAvatar",
|
||||||
"Email", "EmailVerified", "Phone", "Location", "Address", "Affiliation", "Title", "IdCardType", "IdCard", "Homepage", "Bio", "Tag", "Region",
|
"Email", "EmailVerified", "Phone", "Location", "Address", "Affiliation", "Title", "IdCardType", "IdCard", "Homepage", "Bio", "Tag", "Region",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user