casdoor/original/user_original.go

80 lines
2.0 KiB
Go
Raw Normal View History

2021-05-02 13:05:54 +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 original
2021-05-08 22:04:45 +08:00
import (
"strconv"
"strings"
"github.com/casbin/casdoor/util"
2021-05-08 22:04:45 +08:00
)
2021-05-02 13:05:54 +08:00
type User struct {
Id int `xorm:"int notnull pk autoincr" json:"id"`
Name string `xorm:"varchar(128)" json:"name"`
Password string `xorm:"varchar(128)" json:"password"`
Cellphone string `xorm:"varchar(128)" json:"cellphone"`
2021-06-04 23:00:54 +08:00
SchoolId int `json:"schoolId"`
2021-05-02 13:05:54 +08:00
Avatar string `xorm:"varchar(128)" json:"avatar"`
Deleted int `xorm:"tinyint(1)" json:"deleted"`
}
func (User) TableName() string {
2021-06-04 23:00:54 +08:00
return userTableName
2021-05-02 13:05:54 +08:00
}
2021-05-02 23:06:08 +08:00
func getUsersOriginal() []*User {
2021-05-02 13:05:54 +08:00
users := []*User{}
err := adapter.Engine.Asc("id").Find(&users)
if err != nil {
panic(err)
}
return users
}
2021-05-02 23:06:08 +08:00
2021-05-08 22:04:45 +08:00
func getUserMapOriginal() ([]*User, map[string]*User) {
users := getUsersOriginal()
m := map[string]*User{}
for _, user := range users {
m[strconv.Itoa(user.Id)] = user
}
return users, m
}
func addUser(user *User) bool {
affected, err := adapter.Engine.Insert(user)
if err != nil {
panic(err)
}
return affected != 0
}
2021-05-09 15:44:12 +08:00
func updateUser(user *User) bool {
2021-06-04 23:00:54 +08:00
affected, err := adapter.Engine.ID(user.Id).Cols("name", "password", "cellphone", "school_id", "avatar", "deleted").Update(user)
2021-05-09 15:44:12 +08:00
if err != nil {
panic(err)
}
return affected != 0
}
2021-05-08 22:04:45 +08:00
func calculateHash(user *User) string {
2021-06-04 23:00:54 +08:00
s := strings.Join([]string{strconv.Itoa(user.Id), user.Password, user.Name, getFullAvatarUrl(user.Avatar), user.Cellphone, strconv.Itoa(user.SchoolId)}, "|")
2021-05-08 22:04:45 +08:00
return util.GetMd5Hash(s)
}