casdoor/object/user.go

575 lines
15 KiB
Go
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor 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-09-13 20:31:22 +08:00
"github.com/casdoor/casdoor/conf"
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/util"
"github.com/duo-labs/webauthn/webauthn"
2020-10-20 23:14:03 +08:00
"xorm.io/core"
)
const (
2022-08-09 16:50:49 +08:00
UserPropertiesWechatUnionId = "wechatUnionId"
UserPropertiesWechatOpenId = "wechatOpenId"
)
2020-10-20 23:14:03 +08:00
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"`
FirstName string `xorm:"varchar(100)" json:"firstName"`
LastName string `xorm:"varchar(100)" json:"lastName"`
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"`
EmailVerified bool `json:"emailVerified"`
2022-01-04 19:52:29 +08:00
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"`
Karma int `json:"karma"`
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:"github 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"`
Wecom string `xorm:"wecom varchar(100)" json:"wecom"`
Lark string `xorm:"lark varchar(100)" json:"lark"`
Gitlab string `xorm:"gitlab varchar(100)" json:"gitlab"`
Adfs string `xorm:"adfs varchar(100)" json:"adfs"`
Baidu string `xorm:"baidu varchar(100)" json:"baidu"`
Alipay string `xorm:"alipay varchar(100)" json:"alipay"`
Casdoor string `xorm:"casdoor varchar(100)" json:"casdoor"`
Infoflow string `xorm:"infoflow varchar(100)" json:"infoflow"`
Apple string `xorm:"apple varchar(100)" json:"apple"`
AzureAD string `xorm:"azuread varchar(100)" json:"azuread"`
Slack string `xorm:"slack varchar(100)" json:"slack"`
Steam string `xorm:"steam varchar(100)" json:"steam"`
Bilibili string `xorm:"bilibili varchar(100)" json:"bilibili"`
Okta string `xorm:"okta varchar(100)" json:"okta"`
Douyin string `xorm:"douyin varchar(100)" json:"douyin"`
Custom string `xorm:"custom varchar(100)" json:"custom"`
2021-05-30 15:13:43 +08:00
WebauthnCredentials []webauthn.Credential `xorm:"webauthnCredentials blob" json:"webauthnCredentials"`
Ldap string `xorm:"ldap varchar(100)" json:"ldap"`
2021-05-30 15:13:43 +08:00
Properties map[string]string `json:"properties"`
Roles []*Role `json:"roles"`
Permissions []*Permission `json:"permissions"`
LastSigninWrongTime string `xorm:"varchar(100)" json:"lastSigninWrongTime"`
SigninWrongTimes int `json:"signinWrongTimes"`
ManagedAccounts []ManagedAccount `xorm:"managedAccounts blob" json:"managedAccounts"`
2020-10-20 23:14:03 +08:00
}
type Userinfo struct {
Sub string `json:"sub"`
Iss string `json:"iss"`
Aud string `json:"aud"`
Name string `json:"name,omitempty"`
DisplayName string `json:"preferred_username,omitempty"`
Email string `json:"email,omitempty"`
Avatar string `json:"picture,omitempty"`
Address string `json:"address,omitempty"`
Phone string `json:"phone,omitempty"`
}
type ManagedAccount struct {
Application string `xorm:"varchar(100)" json:"application"`
Username string `xorm:"varchar(100)" json:"username"`
Password string `xorm:"varchar(100)" json:"password"`
SigninUrl string `xorm:"varchar(200)" json:"signinUrl"`
}
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 getUserByWechatId(wechatOpenId string, wechatUnionId string) *User {
if wechatUnionId == "" {
wechatUnionId = wechatOpenId
}
user := &User{}
existed, err := adapter.Engine.Where("wechat = ? OR wechat = ?", wechatOpenId, wechatUnionId).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 GetUserByPhone(owner string, phone string) *User {
if owner == "" || phone == "" {
return nil
}
user := User{Owner: owner, Phone: phone}
existed, err := adapter.Engine.Get(&user)
if err != nil {
panic(err)
}
if existed {
return &user
} else {
return nil
}
}
func GetUserByUserId(owner string, userId string) *User {
if owner == "" || userId == "" {
return nil
}
user := User{Owner: owner, Id: userId}
existed, err := adapter.Engine.Get(&user)
if err != nil {
panic(err)
}
if existed {
return &user
} else {
return nil
}
}
2020-10-20 23:14:03 +08:00
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 = "***"
}
if user.ManagedAccounts != nil {
for _, manageAccount := range user.ManagedAccounts {
manageAccount.Password = "***"
}
}
2021-05-30 13:37:34 +08:00
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
}
if user.Password == "***" {
user.Password = oldUser.Password
}
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",
"is_admin", "is_global_admin", "is_forbidden", "is_deleted", "hash", "is_default_avatar", "properties", "webauthnCredentials", "managedAccounts",
"signin_wrong_times", "last_signin_wrong_time",
}
2021-12-11 14:45:08 +08:00
}
2022-01-13 23:19:36 +08:00
if isGlobalAdmin {
columns = append(columns, "name", "email", "phone")
2022-01-13 23:19:36 +08:00
}
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)
if organization == nil {
return false
}
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)
user.Ranking = GetUserCount(user.Owner, "", "") + 1
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
}
// 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 {
2022-03-13 20:53:05 +08:00
if !strings.Contains(err.Error(), "Duplicate entry") {
panic(err)
}
2021-05-02 23:06:08 +08:00
}
return affected != 0
}
2021-12-02 10:55:53 +08:00
func AddUsersInBatch(users []*User) bool {
2022-09-13 20:31:22 +08:00
batchSize := conf.GetConfigBatchSize()
2021-05-02 23:06:08 +08:00
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
func GetUserInfo(userId string, scope string, aud string, host string) (*Userinfo, error) {
user := GetUser(userId)
if user == nil {
return nil, fmt.Errorf("the user: %s doesn't exist", userId)
}
_, originBackend := getOriginFromHost(host)
resp := Userinfo{
Sub: user.Id,
Iss: originBackend,
Aud: aud,
}
if strings.Contains(scope, "profile") {
resp.Name = user.Name
resp.DisplayName = user.DisplayName
resp.Avatar = user.Avatar
}
if strings.Contains(scope, "email") {
resp.Email = user.Email
}
if strings.Contains(scope, "address") {
resp.Address = user.Location
}
if strings.Contains(scope, "phone") {
resp.Phone = user.Phone
}
return &resp, nil
}
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/")
}
func ExtendUserWithRolesAndPermissions(user *User) {
if user == nil {
return
}
user.Roles = GetRolesByUser(user.GetId())
user.Permissions = GetPermissionsByUser(user.GetId())
}