casdoor/object/user.go

414 lines
11 KiB
Go
Raw Normal View History

2021-03-13 23:06:03 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
2020-10-20 23:14:03 +08:00
//
// 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 (
2021-02-21 23:51:40 +08:00
"fmt"
"strings"
2021-02-21 23:51:40 +08:00
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/util"
2020-10-20 23:14:03 +08:00
"xorm.io/core"
)
type User struct {
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
Name string `xorm:"varchar(100) notnull pk" json:"name"`
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
2021-06-01 20:37:00 +08:00
UpdatedTime string `xorm:"varchar(100)" json:"updatedTime"`
2020-10-20 23:14:03 +08:00
2022-01-14 17:42:11 +08:00
Id string `xorm:"varchar(100) index" json:"id"`
2021-06-21 10:22:47 +08:00
Type string `xorm:"varchar(100)" json:"type"`
Password string `xorm:"varchar(100)" json:"password"`
2021-11-04 21:08:43 +08:00
PasswordSalt string `xorm:"varchar(100)" json:"passwordSalt"`
2021-06-21 10:22:47 +08:00
DisplayName string `xorm:"varchar(100)" json:"displayName"`
2021-12-24 00:36:53 +08:00
Avatar string `xorm:"varchar(500)" json:"avatar"`
PermanentAvatar string `xorm:"varchar(500)" json:"permanentAvatar"`
2022-01-04 19:52:29 +08:00
Email string `xorm:"varchar(100) index" json:"email"`
Phone string `xorm:"varchar(100) index" json:"phone"`
2021-08-21 10:58:34 +08:00
Location string `xorm:"varchar(100)" json:"location"`
2021-06-21 10:22:47 +08:00
Address []string `json:"address"`
Affiliation string `xorm:"varchar(100)" json:"affiliation"`
2021-08-21 10:58:34 +08:00
Title string `xorm:"varchar(100)" json:"title"`
2021-11-19 21:37:13 +08:00
IdCardType string `xorm:"varchar(100)" json:"idCardType"`
2022-01-04 19:52:29 +08:00
IdCard string `xorm:"varchar(100) index" json:"idCard"`
2021-08-21 10:58:34 +08:00
Homepage string `xorm:"varchar(100)" json:"homepage"`
2021-08-25 08:07:08 +08:00
Bio string `xorm:"varchar(100)" json:"bio"`
2021-06-21 10:22:47 +08:00
Tag string `xorm:"varchar(100)" json:"tag"`
Region string `xorm:"varchar(100)" json:"region"`
2021-06-21 10:22:47 +08:00
Language string `xorm:"varchar(100)" json:"language"`
2021-11-19 21:37:13 +08:00
Gender string `xorm:"varchar(100)" json:"gender"`
Birthday string `xorm:"varchar(100)" json:"birthday"`
Education string `xorm:"varchar(100)" json:"education"`
2021-06-21 10:22:47 +08:00
Score int `json:"score"`
Ranking int `json:"ranking"`
2021-12-01 21:26:28 +08:00
IsDefaultAvatar bool `json:"isDefaultAvatar"`
2021-08-21 10:58:34 +08:00
IsOnline bool `json:"isOnline"`
2021-06-21 10:22:47 +08:00
IsAdmin bool `json:"isAdmin"`
IsGlobalAdmin bool `json:"isGlobalAdmin"`
IsForbidden bool `json:"isForbidden"`
2021-11-06 15:52:03 +08:00
IsDeleted bool `json:"isDeleted"`
2021-06-21 10:22:47 +08:00
SignupApplication string `xorm:"varchar(100)" json:"signupApplication"`
Hash string `xorm:"varchar(100)" json:"hash"`
PreHash string `xorm:"varchar(100)" json:"preHash"`
2021-02-14 00:22:24 +08:00
2021-11-19 21:37:13 +08:00
CreatedIp string `xorm:"varchar(100)" json:"createdIp"`
LastSigninTime string `xorm:"varchar(100)" json:"lastSigninTime"`
LastSigninIp string `xorm:"varchar(100)" json:"lastSigninIp"`
Github string `xorm:"varchar(100)" json:"github"`
Google string `xorm:"varchar(100)" json:"google"`
QQ string `xorm:"qq varchar(100)" json:"qq"`
WeChat string `xorm:"wechat varchar(100)" json:"wechat"`
Facebook string `xorm:"facebook varchar(100)" json:"facebook"`
DingTalk string `xorm:"dingtalk varchar(100)" json:"dingtalk"`
Weibo string `xorm:"weibo varchar(100)" json:"weibo"`
Gitee string `xorm:"gitee varchar(100)" json:"gitee"`
LinkedIn string `xorm:"linkedin varchar(100)" json:"linkedin"`
2021-08-01 14:48:29 +08:00
Wecom string `xorm:"wecom varchar(100)" json:"wecom"`
Lark string `xorm:"lark varchar(100)" json:"lark"`
Gitlab string `xorm:"gitlab varchar(100)" json:"gitlab"`
Baidu string `xorm:"baidu varchar(100)" json:"baidu"`
Apple string `xorm:"apple varchar(100)" json:"apple"`
AzureAD string `xorm:"azuread varchar(100)" json:"azuread"`
Slack string `xorm:"slack varchar(100)" json:"slack"`
2021-05-30 15:13:43 +08:00
Ldap string `xorm:"ldap varchar(100)" json:"ldap"`
2021-05-30 15:13:43 +08:00
Properties map[string]string `json:"properties"`
2020-10-20 23:14:03 +08:00
}
func GetGlobalUserCount(field, value string) int {
session := GetSession("", -1, -1, field, value, "", "")
count, err := session.Count(&User{})
if err != nil {
panic(err)
}
return int(count)
}
2021-02-13 00:11:12 +08:00
func GetGlobalUsers() []*User {
users := []*User{}
err := adapter.Engine.Desc("created_time").Find(&users)
2021-02-13 00:11:12 +08:00
if err != nil {
panic(err)
}
return users
}
func GetPaginationGlobalUsers(offset, limit int, field, value, sortField, sortOrder string) []*User {
users := []*User{}
session := GetSession("", offset, limit, field, value, sortField, sortOrder)
err := session.Find(&users)
if err != nil {
panic(err)
}
return users
}
func GetUserCount(owner, field, value string) int {
session := GetSession(owner, -1, -1, field, value, "", "")
count, err := session.Count(&User{})
if err != nil {
panic(err)
}
return int(count)
}
func GetOnlineUserCount(owner string, isOnline int) int {
count, err := adapter.Engine.Where("is_online = ?", isOnline).Count(&User{Owner: owner})
if err != nil {
panic(err)
}
return int(count)
}
2020-10-20 23:14:03 +08:00
func GetUsers(owner string) []*User {
users := []*User{}
err := adapter.Engine.Desc("created_time").Find(&users, &User{Owner: owner})
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
return users
}
func GetSortedUsers(owner string, sorter string, limit int) []*User {
users := []*User{}
err := adapter.Engine.Desc(sorter).Limit(limit, 0).Find(&users, &User{Owner: owner})
if err != nil {
panic(err)
}
return users
}
func GetPaginationUsers(owner string, offset, limit int, field, value, sortField, sortOrder string) []*User {
users := []*User{}
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
err := session.Find(&users)
if err != nil {
panic(err)
}
return users
}
2020-10-20 23:14:03 +08:00
func getUser(owner string, name string) *User {
2021-06-21 01:01:16 +08:00
if owner == "" || name == "" {
return nil
}
2020-10-20 23:14:03 +08:00
user := User{Owner: owner, Name: name}
existed, err := adapter.Engine.Get(&user)
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
if existed {
return &user
} else {
return nil
}
}
func getUserById(owner string, id string) *User {
if owner == "" || id == "" {
return nil
}
user := User{Owner: owner, Id: id}
existed, err := adapter.Engine.Get(&user)
if err != nil {
panic(err)
}
if existed {
return &user
} else {
return nil
}
}
func GetUserByEmail(owner string, email string) *User {
if owner == "" || email == "" {
return nil
}
user := User{Owner: owner, Email: email}
existed, err := adapter.Engine.Get(&user)
if err != nil {
panic(err)
}
2020-10-20 23:14:03 +08:00
if existed {
return &user
} else {
return nil
}
}
func GetUser(id string) *User {
owner, name := util.GetOwnerAndNameFromId(id)
return getUser(owner, name)
}
2021-11-20 15:46:54 +08:00
func GetUserNoCheck(id string) *User {
owner, name := util.GetOwnerAndNameFromIdNoCheck(id)
return getUser(owner, name)
}
2021-05-30 13:37:34 +08:00
func GetMaskedUser(user *User) *User {
2021-06-12 12:27:13 +08:00
if user == nil {
return nil
}
2021-05-30 13:37:34 +08:00
if user.Password != "" {
user.Password = "***"
}
return user
}
func GetMaskedUsers(users []*User) []*User {
for _, user := range users {
user = GetMaskedUser(user)
}
return users
}
2021-06-17 00:49:02 +08:00
func GetLastUser(owner string) *User {
user := User{Owner: owner}
existed, err := adapter.Engine.Desc("created_time", "id").Get(&user)
if err != nil {
panic(err)
}
if existed {
return &user
}
2021-08-07 22:02:56 +08:00
return nil
2021-06-17 00:49:02 +08:00
}
2022-01-13 23:19:36 +08:00
func UpdateUser(id string, user *User, columns []string, isGlobalAdmin bool) bool {
2021-11-20 15:46:54 +08:00
owner, name := util.GetOwnerAndNameFromIdNoCheck(id)
2021-08-21 22:52:29 +08:00
oldUser := getUser(owner, name)
if oldUser == nil {
2020-10-20 23:14:03 +08:00
return false
}
2021-05-08 18:51:37 +08:00
user.UpdateUserHash()
2021-11-20 16:21:15 +08:00
if user.Avatar != oldUser.Avatar && user.Avatar != "" && user.PermanentAvatar != "*" {
2021-08-21 23:17:33 +08:00
user.PermanentAvatar = getPermanentAvatarUrl(user.Owner, user.Name, user.Avatar)
}
2021-12-11 14:45:08 +08:00
if len(columns) == 0 {
columns = []string{"owner", "display_name", "avatar",
"location", "address", "region", "language", "affiliation", "title", "homepage", "bio", "score", "tag", "signup_application",
2021-12-11 14:45:08 +08:00
"is_admin", "is_global_admin", "is_forbidden", "is_deleted", "hash", "is_default_avatar", "properties"}
}
2022-01-13 23:19:36 +08:00
if isGlobalAdmin {
columns = append(columns, "name")
}
2021-12-11 14:45:08 +08:00
affected, err := adapter.Engine.ID(core.PK{owner, name}).Cols(columns...).Update(user)
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
2021-05-09 15:44:12 +08:00
return affected != 0
}
2021-08-21 22:52:29 +08:00
func UpdateUserForAllFields(id string, user *User) bool {
2021-06-09 21:41:27 +08:00
owner, name := util.GetOwnerAndNameFromId(id)
2021-08-21 22:52:29 +08:00
oldUser := getUser(owner, name)
if oldUser == nil {
2021-06-09 21:41:27 +08:00
return false
}
user.UpdateUserHash()
2021-08-21 23:17:33 +08:00
if user.Avatar != oldUser.Avatar && user.Avatar != "" {
user.PermanentAvatar = getPermanentAvatarUrl(user.Owner, user.Name, user.Avatar)
}
2021-06-09 21:41:27 +08:00
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(user)
if err != nil {
panic(err)
}
return affected != 0
}
2020-10-20 23:14:03 +08:00
func AddUser(user *User) bool {
2021-06-17 01:12:03 +08:00
if user.Id == "" {
user.Id = util.GenerateId()
}
2021-05-16 21:22:20 +08:00
2021-12-12 19:59:55 +08:00
if user.Owner == "" || user.Name == "" {
return false
}
2021-05-16 22:58:30 +08:00
organization := GetOrganizationByUser(user)
2021-05-16 21:04:26 +08:00
user.UpdateUserPassword(organization)
2021-05-08 18:51:37 +08:00
2021-05-16 21:22:20 +08:00
user.UpdateUserHash()
user.PreHash = user.Hash
2021-08-21 23:17:33 +08:00
user.PermanentAvatar = getPermanentAvatarUrl(user.Owner, user.Name, user.Avatar)
affected, err := adapter.Engine.Insert(user)
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
return affected != 0
}
2021-05-02 23:06:08 +08:00
func AddUsers(users []*User) bool {
2021-05-16 21:04:26 +08:00
if len(users) == 0 {
return false
}
2022-01-17 13:26:30 +08:00
//organization := GetOrganizationByUser(users[0])
2021-05-08 18:51:37 +08:00
for _, user := range users {
2022-01-17 13:26:30 +08:00
// this function is only used for syncer or batch upload, so no need to encrypt the password
//user.UpdateUserPassword(organization)
2021-05-16 21:22:20 +08:00
2021-05-08 18:51:37 +08:00
user.UpdateUserHash()
2021-05-09 14:54:17 +08:00
user.PreHash = user.Hash
2021-08-21 23:17:33 +08:00
user.PermanentAvatar = getPermanentAvatarUrl(user.Owner, user.Name, user.Avatar)
2021-05-08 18:51:37 +08:00
}
2021-05-02 23:06:08 +08:00
affected, err := adapter.Engine.Insert(users)
if err != nil {
panic(err)
}
return affected != 0
}
2021-12-02 10:55:53 +08:00
func AddUsersInBatch(users []*User) bool {
2021-05-02 23:06:08 +08:00
batchSize := 1000
if len(users) == 0 {
return false
}
affected := false
for i := 0; i < (len(users)-1)/batchSize+1; i++ {
start := i * batchSize
end := (i + 1) * batchSize
if end > len(users) {
end = len(users)
}
tmp := users[start:end]
// TODO: save to log instead of standard output
// fmt.Printf("Add users: [%d - %d].\n", start, end)
2021-05-02 23:06:08 +08:00
if AddUsers(tmp) {
affected = true
}
}
return affected
}
2020-10-20 23:14:03 +08:00
func DeleteUser(user *User) bool {
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Delete(&User{})
2020-10-20 23:14:03 +08:00
if err != nil {
panic(err)
}
return affected != 0
}
2021-02-14 00:22:24 +08:00
2021-04-27 19:35:40 +08:00
func LinkUserAccount(user *User, field string, value string) bool {
return SetUserField(user, field, value)
}
2021-05-08 22:04:45 +08:00
func (user *User) GetId() string {
return fmt.Sprintf("%s/%s", user.Owner, user.Name)
}
func isUserIdGlobalAdmin(userId string) bool {
return strings.HasPrefix(userId, "built-in/")
}