Support affiliation sync.

This commit is contained in:
Yang Luo
2021-06-04 23:00:54 +08:00
parent 081da18403
commit eb658ad8ee
7 changed files with 82 additions and 18 deletions

View File

@ -114,7 +114,7 @@ func UpdateUser(id string, user *User) bool {
user.UpdateUserHash() user.UpdateUserHash()
affected, err := adapter.Engine.ID(core.PK{owner, name}).Cols("display_name", "avatar", "address", "affiliation", "tag", "is_admin", "is_global_admin", "is_forbidden", "hash").Update(user) affected, err := adapter.Engine.ID(core.PK{owner, name}).Cols("display_name", "avatar", "address", "affiliation", "score", "tag", "is_admin", "is_global_admin", "is_forbidden", "hash").Update(user)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -123,7 +123,7 @@ func UpdateUser(id string, user *User) bool {
} }
func UpdateUserForOriginal(user *User) bool { func UpdateUserForOriginal(user *User) bool {
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols("display_name", "password", "phone", "avatar", "is_forbidden", "hash", "pre_hash").Update(user) affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols("display_name", "password", "phone", "avatar", "affiliation", "score", "is_forbidden", "hash", "pre_hash").Update(user)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -17,6 +17,7 @@ package object
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strconv"
"strings" "strings"
"github.com/casdoor/casdoor/util" "github.com/casdoor/casdoor/util"
@ -124,7 +125,7 @@ func ClearUserProperties(user *User, providerType string) bool {
} }
func calculateHash(user *User) string { func calculateHash(user *User) string {
s := strings.Join([]string{user.Id, user.Password, user.DisplayName, user.Avatar, user.Phone}, "|") s := strings.Join([]string{user.Id, user.Password, user.DisplayName, user.Avatar, user.Phone, strconv.Itoa(user.Score)}, "|")
return util.GetMd5Hash(s) return util.GetMd5Hash(s)
} }

44
original/affiliation.go Normal file
View File

@ -0,0 +1,44 @@
// 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
type Affiliation struct {
Id int `xorm:"int notnull pk autoincr" json:"id"`
Name string `xorm:"varchar(128)" json:"name"`
}
func (Affiliation) TableName() string {
return affiliationTableName
}
func getAffiliations() []*Affiliation {
affiliations := []*Affiliation{}
err := adapter.Engine.Asc("id").Find(&affiliations)
if err != nil {
panic(err)
}
return affiliations
}
func getAffiliationMap() ([]*Affiliation, map[int]string) {
affiliations := getAffiliations()
m := map[int]string{}
for _, affiliation := range affiliations {
m[affiliation.Id] = affiliation.Name
}
return affiliations, m
}

View File

@ -15,7 +15,8 @@
package original package original
var dbName = "dbName" var dbName = "dbName"
var tableName = "tableName" var userTableName = "userTableName"
var affiliationTableName = "affiliationTableName"
var avatarBaseUrl = "https://cdn.example.com/" var avatarBaseUrl = "https://cdn.example.com/"
var orgName = "orgName" var orgName = "orgName"

View File

@ -37,7 +37,16 @@ func getPartialAvatarUrl(avatar string) string {
return avatar return avatar
} }
func createUserFromOriginalUser(originalUser *User) *object.User { func createUserFromOriginalUser(originalUser *User, affiliationMap map[int]string) *object.User {
affiliation := ""
if originalUser.SchoolId != 0 {
var ok bool
affiliation, ok = affiliationMap[originalUser.SchoolId]
if !ok {
panic(fmt.Sprintf("SchoolId not found: %d", originalUser.SchoolId))
}
}
user := &object.User{ user := &object.User{
Owner: orgName, Owner: orgName,
Name: strconv.Itoa(originalUser.Id), Name: strconv.Itoa(originalUser.Id),
@ -49,7 +58,8 @@ func createUserFromOriginalUser(originalUser *User) *object.User {
Avatar: getFullAvatarUrl(originalUser.Avatar), Avatar: getFullAvatarUrl(originalUser.Avatar),
Email: "", Email: "",
Phone: originalUser.Cellphone, Phone: originalUser.Cellphone,
Affiliation: "", Affiliation: affiliation,
Score: originalUser.SchoolId,
IsAdmin: false, IsAdmin: false,
IsGlobalAdmin: false, IsGlobalAdmin: false,
IsForbidden: originalUser.Deleted != 0, IsForbidden: originalUser.Deleted != 0,
@ -68,6 +78,7 @@ func createOriginalUserFromUser(user *object.User) *User {
Name: user.DisplayName, Name: user.DisplayName,
Password: user.Password, Password: user.Password,
Cellphone: user.Phone, Cellphone: user.Phone,
SchoolId: user.Score,
Avatar: getPartialAvatarUrl(user.Avatar), Avatar: getPartialAvatarUrl(user.Avatar),
Deleted: deleted, Deleted: deleted,
} }
@ -81,11 +92,13 @@ func syncUsers() {
oUsers, oUserMap := getUserMapOriginal() oUsers, oUserMap := getUserMapOriginal()
fmt.Printf("Users: %d, oUsers: %d\n", len(users), len(oUsers)) fmt.Printf("Users: %d, oUsers: %d\n", len(users), len(oUsers))
_, affiliationMap := getAffiliationMap()
newUsers := []*object.User{} newUsers := []*object.User{}
for _, oUser := range oUsers { for _, oUser := range oUsers {
id := strconv.Itoa(oUser.Id) id := strconv.Itoa(oUser.Id)
if _, ok := userMap[id]; !ok { if _, ok := userMap[id]; !ok {
newUser := createUserFromOriginalUser(oUser) newUser := createUserFromOriginalUser(oUser, affiliationMap)
fmt.Printf("New user: %v\n", newUser) fmt.Printf("New user: %v\n", newUser)
newUsers = append(newUsers, newUser) newUsers = append(newUsers, newUser)
} else { } else {
@ -94,7 +107,7 @@ func syncUsers() {
if user.Hash == user.PreHash { if user.Hash == user.PreHash {
if user.Hash != oHash { if user.Hash != oHash {
updatedUser := createUserFromOriginalUser(oUser) updatedUser := createUserFromOriginalUser(oUser, affiliationMap)
updatedUser.Hash = oHash updatedUser.Hash = oHash
updatedUser.PreHash = oHash updatedUser.PreHash = oHash
object.UpdateUserForOriginal(updatedUser) object.UpdateUserForOriginal(updatedUser)
@ -115,7 +128,7 @@ func syncUsers() {
user.PreHash = user.Hash user.PreHash = user.Hash
object.SetUserField(user, "pre_hash", user.PreHash) object.SetUserField(user, "pre_hash", user.PreHash)
} else { } else {
updatedUser := createUserFromOriginalUser(oUser) updatedUser := createUserFromOriginalUser(oUser, affiliationMap)
updatedUser.Hash = oHash updatedUser.Hash = oHash
updatedUser.PreHash = oHash updatedUser.PreHash = oHash
object.UpdateUserForOriginal(updatedUser) object.UpdateUserForOriginal(updatedUser)

View File

@ -26,12 +26,13 @@ type User struct {
Name string `xorm:"varchar(128)" json:"name"` Name string `xorm:"varchar(128)" json:"name"`
Password string `xorm:"varchar(128)" json:"password"` Password string `xorm:"varchar(128)" json:"password"`
Cellphone string `xorm:"varchar(128)" json:"cellphone"` Cellphone string `xorm:"varchar(128)" json:"cellphone"`
SchoolId int `json:"schoolId"`
Avatar string `xorm:"varchar(128)" json:"avatar"` Avatar string `xorm:"varchar(128)" json:"avatar"`
Deleted int `xorm:"tinyint(1)" json:"deleted"` Deleted int `xorm:"tinyint(1)" json:"deleted"`
} }
func (User) TableName() string { func (User) TableName() string {
return tableName return userTableName
} }
func getUsersOriginal() []*User { func getUsersOriginal() []*User {
@ -55,7 +56,7 @@ func getUserMapOriginal() ([]*User, map[string]*User) {
} }
func updateUser(user *User) bool { func updateUser(user *User) bool {
affected, err := adapter.Engine.ID(user.Id).Cols("name", "password", "cellphone", "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 {
panic(err) panic(err)
} }
@ -64,6 +65,6 @@ func updateUser(user *User) bool {
} }
func calculateHash(user *User) string { func calculateHash(user *User) string {
s := strings.Join([]string{strconv.Itoa(user.Id), user.Password, user.Name, getFullAvatarUrl(user.Avatar), user.Cellphone}, "|") s := strings.Join([]string{strconv.Itoa(user.Id), user.Password, user.Name, getFullAvatarUrl(user.Avatar), user.Cellphone, strconv.Itoa(user.SchoolId)}, "|")
return util.GetMd5Hash(s) return util.GetMd5Hash(s)
} }

View File

@ -55,8 +55,6 @@ class AffiliationSelect extends React.Component {
} }
render() { render() {
let options = this.state.addressOptions;
return ( return (
<React.Fragment> <React.Fragment>
{ {
@ -66,9 +64,10 @@ class AffiliationSelect extends React.Component {
{i18next.t("user:Address")}: {i18next.t("user:Address")}:
</Col> </Col>
<Col span={22} > <Col span={22} >
<Cascader style={{width: '400px'}} value={this.props.user.address} options={options} onChange={value => { <Cascader style={{width: '400px'}} value={this.props.user.address} options={this.state.addressOptions} onChange={value => {
this.updateUserField('address', value); this.updateUserField('address', value);
this.updateUserField('affiliation', ''); this.updateUserField('affiliation', '');
this.updateUserField('score', 0);
this.getAffiliationOptions(this.props.application, this.props.user); this.getAffiliationOptions(this.props.application, this.props.user);
}} placeholder={i18next.t("signup:Please input your address!")} /> }} placeholder={i18next.t("signup:Please input your address!")} />
</Col> </Col>
@ -86,12 +85,17 @@ class AffiliationSelect extends React.Component {
this.updateUserField('affiliation', e.target.value); this.updateUserField('affiliation', e.target.value);
}} /> }} />
) : ( ) : (
<Select virtual={false} style={{width: '400px'}} value={Setting.myParseInt(this.props.user.affiliation)} onChange={(value => {this.updateUserField('affiliation', value.toString());})}> <Select virtual={false} style={{width: '400px'}} value={this.props.user.affiliation} onChange={(value => {
const name = value;
const id = this.state.affiliationOptions.filter(affiliationOption => affiliationOption.name === name)[0].id;
this.updateUserField('affiliation', name);
this.updateUserField('score', id);
})}>
{ {
<Option key={0} value={0}></Option> <Option key={0} value={""}>(empty)</Option>
} }
{ {
this.state.affiliationOptions.map((affiliationOption, index) => <Option key={affiliationOption.id} value={affiliationOption.id}>{affiliationOption.name}</Option>) this.state.affiliationOptions.map((affiliationOption, index) => <Option key={affiliationOption.id} value={affiliationOption.name}>{affiliationOption.name}</Option>)
} }
</Select> </Select>
) )