casdoor/object/syncer_util.go

204 lines
5.4 KiB
Go
Raw Normal View History

2021-12-18 01:08:03 +08:00
// 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 object
import (
"fmt"
"strconv"
"strings"
"github.com/casbin/casdoor/util"
)
func (syncer *Syncer) getFullAvatarUrl(avatar string) string {
2021-12-19 22:30:54 +08:00
if syncer.AvatarBaseUrl == "" {
return avatar
}
2021-12-18 01:08:03 +08:00
if !strings.HasPrefix(avatar, "https://") {
return fmt.Sprintf("%s%s", syncer.AvatarBaseUrl, avatar)
}
return avatar
}
func (syncer *Syncer) getPartialAvatarUrl(avatar string) string {
if strings.HasPrefix(avatar, syncer.AvatarBaseUrl) {
return avatar[len(syncer.AvatarBaseUrl):]
}
return avatar
}
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) createUserFromOriginalUser(originalUser *OriginalUser, affiliationMap map[int]string) *User {
user := *originalUser
user.Owner = syncer.Organization
if user.Name == "" {
user.Name = originalUser.Id
}
if user.CreatedTime == "" {
user.CreatedTime = util.GetCurrentTime()
}
if user.Type == "" {
user.Type = "normal-user"
}
if originalUser.Score != 0 {
affiliation, ok := affiliationMap[originalUser.Score]
2021-12-18 01:08:03 +08:00
if !ok {
2021-12-19 22:30:54 +08:00
panic(fmt.Sprintf("Affiliation not found: %d", originalUser.Score))
2021-12-18 01:08:03 +08:00
}
2021-12-19 22:30:54 +08:00
user.Affiliation = affiliation
2021-12-18 01:08:03 +08:00
}
2021-12-19 22:30:54 +08:00
if user.Properties == nil {
user.Properties = map[string]string{}
2021-12-18 01:08:03 +08:00
}
2021-12-19 22:30:54 +08:00
return &user
}
func (syncer *Syncer) createOriginalUserFromUser(user *User) *OriginalUser {
2021-12-18 01:08:03 +08:00
return user
}
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) setUserByKeyValue(user *User, key string, value string) {
switch key {
case "Name":
user.Name = value
case "CreatedTime":
user.CreatedTime = value
case "UpdatedTime":
user.UpdatedTime = value
case "Id":
user.Id = value
case "Type":
user.Type = value
case "Password":
user.Password = value
case "PasswordSalt":
user.PasswordSalt = value
case "DisplayName":
user.DisplayName = value
case "Avatar":
user.Avatar = syncer.getPartialAvatarUrl(value)
case "PermanentAvatar":
user.PermanentAvatar = value
case "Email":
user.Email = value
case "Phone":
user.Phone = value
case "Location":
user.Location = value
case "Address":
user.Address = []string{value}
case "Affiliation":
user.Affiliation = value
case "Title":
user.Title = value
case "IdCardType":
user.IdCardType = value
case "IdCard":
user.IdCard = value
case "Homepage":
user.Homepage = value
case "Bio":
user.Bio = value
case "Tag":
user.Tag = value
case "Region":
user.Region = value
case "Language":
user.Language = value
case "Gender":
user.Gender = value
case "Birthday":
user.Birthday = value
case "Education":
user.Education = value
case "Score":
user.Score = util.ParseInt(value)
case "Ranking":
user.Ranking = util.ParseInt(value)
case "IsDefaultAvatar":
user.IsDefaultAvatar = util.ParseBool(value)
case "IsOnline":
user.IsOnline = util.ParseBool(value)
case "IsAdmin":
user.IsAdmin = util.ParseBool(value)
case "IsGlobalAdmin":
user.IsGlobalAdmin = util.ParseBool(value)
case "IsForbidden":
user.IsForbidden = util.ParseBool(value)
case "IsDeleted":
user.IsDeleted = util.ParseBool(value)
case "CreatedIp":
user.CreatedIp = value
2021-12-18 01:08:03 +08:00
}
2021-12-19 22:30:54 +08:00
}
2021-12-18 01:08:03 +08:00
2021-12-19 22:30:54 +08:00
func (syncer *Syncer) getOriginalUsersFromMap(results []map[string]string) []*OriginalUser {
users := []*OriginalUser{}
for _, result := range results {
originalUser := &OriginalUser{}
for _, tableColumn := range syncer.TableColumns {
syncer.setUserByKeyValue(originalUser, tableColumn.CasdoorName, result[tableColumn.Name])
}
users = append(users, originalUser)
2021-12-18 01:08:03 +08:00
}
2021-12-19 22:30:54 +08:00
return users
}
func (syncer *Syncer) getMapFromOriginalUser(user *OriginalUser) map[string]string {
m := map[string]string{}
m["Name"] = user.Name
m["CreatedTime"] = user.CreatedTime
m["UpdatedTime"] = user.UpdatedTime
m["Id"] = user.Id
m["Type"] = user.Type
m["Password"] = user.Password
m["PasswordSalt"] = user.PasswordSalt
m["DisplayName"] = user.DisplayName
m["Avatar"] = syncer.getFullAvatarUrl(user.Avatar)
m["PermanentAvatar"] = user.PermanentAvatar
m["Email"] = user.Email
m["Phone"] = user.Phone
m["Location"] = user.Location
m["Address"] = strings.Join(user.Address, "|")
m["Affiliation"] = user.Affiliation
m["Title"] = user.Title
m["IdCardType"] = user.IdCardType
m["IdCard"] = user.IdCard
m["Homepage"] = user.Homepage
m["Bio"] = user.Bio
m["Tag"] = user.Tag
m["Region"] = user.Region
m["Language"] = user.Language
m["Gender"] = user.Gender
m["Birthday"] = user.Birthday
m["Education"] = user.Education
m["Score"] = strconv.Itoa(user.Score)
m["Ranking"] = strconv.Itoa(user.Ranking)
m["IsDefaultAvatar"] = util.BoolToString(user.IsDefaultAvatar)
m["IsOnline"] = util.BoolToString(user.IsOnline)
m["IsAdmin"] = util.BoolToString(user.IsAdmin)
m["IsGlobalAdmin"] = util.BoolToString(user.IsGlobalAdmin)
m["IsForbidden"] = util.BoolToString(user.IsForbidden)
m["IsDeleted"] = util.BoolToString(user.IsDeleted)
m["CreatedIp"] = user.CreatedIp
m2 := map[string]string{}
for _, tableColumn := range syncer.TableColumns {
m2[tableColumn.CasdoorName] = m[tableColumn.CasdoorName]
}
return m2
2021-12-18 01:08:03 +08:00
}