Add member code.

This commit is contained in:
Gucheng Wang 2021-11-20 09:39:50 +08:00
parent 55ab30e8b6
commit fcc41ff46e
8 changed files with 455 additions and 3 deletions

View File

@ -14,9 +14,25 @@
package discuzx
import (
"github.com/astaxie/beego"
"github.com/casbin/casnode/object"
)
var dbName = "ultrax"
var ossEndpoint = ""
var ossAccessKeyId = ""
var ossAccessKeySecret = ""
var ossBucketName = "casnode"
var cdnDomain = "https://cdn.casnode.com/"
var cdnDomain = "https://cdn.casbin.com/"
var discuzxDomain = "https://forum.casbin.com/"
var CasdoorOrganization = ""
var CasdoorApplication = ""
func init() {
object.InitConfig()
CasdoorOrganization = beego.AppConfig.String("casdoorOrganization")
CasdoorApplication = beego.AppConfig.String("casdoorApplication")
}

67
discuzx/member.go Normal file
View File

@ -0,0 +1,67 @@
// 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 discuzx
type Member struct {
Uid int
Email string
Username string
Password string
Status int
Emailstatus int
Avatarstatus int
Videophotostatus int
Adminid int
Groupid int
Groupexpiry int
Extgroupids string
Regdate int
Credits int
Allowadmincp int
}
func getMembers() []*Member {
members := []*Member{}
err := adapter.Engine.Table("pre_common_member").Find(&members)
if err != nil {
panic(err)
}
return members
}
func getMember(id int) *Member {
member := Member{Uid: id}
existed, err := adapter.Engine.Table("pre_common_member").Get(&member)
if err != nil {
panic(err)
}
if existed {
return &member
} else {
return nil
}
}
func getMemberMap() map[int]*Member {
members := getMembers()
m := map[int]*Member{}
for _, member := range members {
m[member.Uid] = member
}
return m
}

59
discuzx/member_ex.go Normal file
View File

@ -0,0 +1,59 @@
// 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 discuzx
type MemberEx struct {
*Member
*Profile
*UcenterMember
}
func getMembersEx() []*MemberEx {
members := getMembers()
profileMap := getProfileMap()
ucenterMemberMap := getUcenterMemberMap()
membersEx := []*MemberEx{}
for _, member := range members {
memberEx := &MemberEx{
Member: member,
Profile: profileMap[member.Uid],
UcenterMember: ucenterMemberMap[member.Uid],
}
membersEx = append(membersEx, memberEx)
}
return membersEx
}
func getMemberEx(id int) *MemberEx {
member := getMember(id)
profile := getProfile(id)
ucenterMember := getUcenterMember(id)
return &MemberEx{
Member: member,
Profile: profile,
UcenterMember: ucenterMember,
}
}
func getMemberExMap() map[int]*MemberEx {
membersEx := getMembersEx()
m := map[int]*MemberEx{}
for _, memberEx := range membersEx {
m[memberEx.Member.Uid] = memberEx
}
return m
}

72
discuzx/member_profile.go Normal file
View File

@ -0,0 +1,72 @@
// 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 discuzx
type Profile struct {
Uid int
Realname string
Gender int
Birthyear int
Birthmonth int
Birthday int
Mobile string
Idcardtype string
Idcard string
Address string
Resideprovince string
Residecity string
Residedist string
Residecommunity string
Education string
Occupation string
Position string
Site string
Bio string
Interest string
}
func getProfiles() []*Profile {
profiles := []*Profile{}
err := adapter.Engine.Table("pre_common_member_profile").Find(&profiles)
if err != nil {
panic(err)
}
return profiles
}
func getProfile(id int) *Profile {
profile := Profile{Uid: id}
existed, err := adapter.Engine.Table("pre_common_member_profile").Get(&profile)
if err != nil {
panic(err)
}
if existed {
return &profile
} else {
return nil
}
}
func getProfileMap() map[int]*Profile {
profiles := getProfiles()
m := map[int]*Profile{}
for _, profile := range profiles {
m[profile.Uid] = profile
}
return m
}

61
discuzx/member_ucenter.go Normal file
View File

@ -0,0 +1,61 @@
// 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 discuzx
type UcenterMember struct {
Uid int
Username string
Password string
Email string
Regip string
Regdate int
Lastloginip int
Lastlogintime int
Salt string
}
func getUcenterMembers() []*UcenterMember {
ucenterMembers := []*UcenterMember{}
err := adapter.Engine.Table("pre_ucenter_members").Find(&ucenterMembers)
if err != nil {
panic(err)
}
return ucenterMembers
}
func getUcenterMember(id int) *UcenterMember {
ucenterMember := UcenterMember{Uid: id}
existed, err := adapter.Engine.Table("pre_ucenter_members").Get(&ucenterMember)
if err != nil {
panic(err)
}
if existed {
return &ucenterMember
} else {
return nil
}
}
func getUcenterMemberMap() map[int]*UcenterMember {
ucenterMembers := getUcenterMembers()
m := map[int]*UcenterMember{}
for _, ucenterMember := range ucenterMembers {
m[ucenterMember.Uid] = ucenterMember
}
return m
}

View File

@ -31,7 +31,7 @@ type Thread struct {
func getThreads() []*Thread {
threads := []*Thread{}
err := adapter.Engine.Table("forum_thread").Find(&threads)
err := adapter.Engine.Table("pre_forum_thread").Find(&threads)
if err != nil {
panic(err)
}
@ -41,7 +41,7 @@ func getThreads() []*Thread {
func getThread(id int) *Thread {
thread := Thread{Tid: id}
existed, err := adapter.Engine.Table("forum_thread").Get(&thread)
existed, err := adapter.Engine.Table("pre_forum_thread").Get(&thread)
if err != nil {
panic(err)
}

141
discuzx/user.go Normal file
View File

@ -0,0 +1,141 @@
// 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 discuzx
import (
"fmt"
"strconv"
"github.com/casbin/casnode/casdoor"
"github.com/casdoor/casdoor-go-sdk/auth"
)
func addUser(memberEx *MemberEx) bool {
avatar := fmt.Sprintf("%suc_server/avatar.php?uid=%d", discuzxDomain, memberEx.Member.Uid)
avatar = getRedirectUrl(avatar)
user := &auth.User{
Owner: CasdoorOrganization,
Name: memberEx.Member.Username,
CreatedTime: getTimeFromUnixSeconds(memberEx.Member.Regdate),
Id: strconv.Itoa(memberEx.Member.Uid),
Type: "normal-user",
//Password: memberEx.UcenterMember.Password,
//PasswordSalt: memberEx.UcenterMember.Salt,
//DisplayName: displayName,
Avatar: avatar,
PermanentAvatar: avatar,
Email: memberEx.Member.Email,
//Phone: memberEx.Profile.Mobile,
//Location: memberEx.Profile.Residecity,
Address: []string{},
//Affiliation: memberEx.Profile.Occupation,
//Title: memberEx.Profile.Position,
//IdCardType: idCardType,
//IdCard: idCard,
//Homepage: memberEx.Profile.Site,
//Bio: memberEx.Profile.Bio,
//Tag: memberEx.Profile.Interest,
Region: "CN",
Language: "zh",
//Gender: gender,
//Birthday: birthday,
//Education: memberEx.Profile.Education,
Score: memberEx.Member.Credits,
Ranking: memberEx.Member.Uid,
IsOnline: false,
IsAdmin: false,
IsGlobalAdmin: false,
IsForbidden: false,
IsDeleted: false,
SignupApplication: CasdoorApplication,
//CreatedIp: memberEx.UcenterMember.Regip,
//LastSigninTime: getTimeFromUnixSeconds(memberEx.UcenterMember.Lastlogintime),
LastSigninIp: "",
Properties: map[string]string{},
}
if memberEx.UcenterMember == nil {
fmt.Printf("[%d, %s] memberEx.UcenterMember == nil\n", memberEx.Member.Uid, memberEx.Member.Username)
} else {
user.Password = memberEx.UcenterMember.Password
user.PasswordSalt = memberEx.UcenterMember.Salt
user.CreatedIp = memberEx.UcenterMember.Regip
user.LastSigninTime = getTimeFromUnixSeconds(memberEx.UcenterMember.Lastlogintime)
}
if memberEx.Profile == nil {
fmt.Printf("[%d, %s] memberEx.Profile == nil\n", memberEx.Member.Uid, memberEx.Member.Username)
} else {
displayName := memberEx.Profile.Realname
if displayName == "" {
displayName = memberEx.Member.Username
}
idCardType := ""
idCard := ""
if memberEx.Profile.Idcard != "" {
idCardType = "IdCard"
idCard = memberEx.Profile.Idcard
}
gender := "Male"
if memberEx.Profile.Gender == 2 {
gender = "Female"
}
birthday := ""
if memberEx.Profile.Birthyear != 0 && memberEx.Profile.Birthmonth != 0 && memberEx.Profile.Birthday != 0 {
birthday = fmt.Sprintf("%02d-%02d-%02d", memberEx.Profile.Birthyear, memberEx.Profile.Birthmonth, memberEx.Profile.Birthday)
}
address := []string{}
if memberEx.Profile.Resideprovince != "" {
address = append(address, memberEx.Profile.Resideprovince)
if memberEx.Profile.Residecity != "" {
address = append(address, memberEx.Profile.Residecity)
if memberEx.Profile.Residedist != "" {
address = append(address, memberEx.Profile.Residedist)
if memberEx.Profile.Residecommunity != "" {
address = append(address, memberEx.Profile.Residecommunity)
}
}
}
}
user.Address = address
user.DisplayName = displayName
user.IdCardType = idCardType
user.IdCard = idCard
user.Gender = gender
user.Birthday = birthday
user.Phone = memberEx.Profile.Mobile
user.Location = memberEx.Profile.Residecity
user.Affiliation = memberEx.Profile.Occupation
user.Title = memberEx.Profile.Position
user.Homepage = memberEx.Profile.Site
user.Bio = memberEx.Profile.Bio
user.Tag = memberEx.Profile.Interest
user.Education = memberEx.Profile.Education
}
affected := casdoor.AddUser(user)
if !affected {
panic("addTopic(): not affected")
}
return affected
}

36
discuzx/user_test.go Normal file
View File

@ -0,0 +1,36 @@
// 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 discuzx
import (
"fmt"
"testing"
"github.com/casbin/casnode/casdoor"
"github.com/casbin/casnode/object"
)
func TestAddUsers(t *testing.T) {
object.InitConfig()
InitAdapter()
object.InitAdapter()
casdoor.InitCasdoorAdapter()
membersEx := getMembersEx()
for i, memberEx := range membersEx {
addUser(memberEx)
fmt.Printf("[%d/%d]: Added user: [%d, %s] to Casdoor\n", i, len(membersEx), memberEx.Member.Uid, memberEx.Member.Username)
}
}