casdoor/object/user.go

294 lines
6.9 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"
2021-04-18 23:14:46 +08:00
"reflect"
2021-05-08 18:51:37 +08:00
"strings"
2021-02-21 23:51:40 +08:00
2020-10-20 23:14:03 +08:00
"github.com/casdoor/casdoor/util"
"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-02-15 22:14:19 +08:00
Id string `xorm:"varchar(100)" json:"id"`
2021-04-26 19:00:23 +08:00
Type string `xorm:"varchar(100)" json:"type"`
2021-02-15 22:14:19 +08:00
Password string `xorm:"varchar(100)" json:"password"`
DisplayName string `xorm:"varchar(100)" json:"displayName"`
feat: added avatar tailoring and uploading Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed type errors Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed the wrong folder Signed-off-by: Kininaru <shiftregister233@outlook.com> rewrite login check logic, added unix time to avatar url Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed a bug about strconv Signed-off-by: Kininaru <shiftregister233@outlook.com> supported oss Signed-off-by: Kininaru <shiftregister233@outlook.com> disabled oss provide qiniu Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar url error Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> Fixed avatar length bug Signed-off-by: Kininaru <shiftregister233@outlook.com> fixed oss.conf Signed-off-by: Kininaru <shiftregister233@outlook.com> Put uploading avatar into UserEditPage Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar dir Signed-off-by: Kininaru <shiftregister233@outlook.com> removed avatar in main.go Signed-off-by: Kininaru <shiftregister233@outlook.com> Made CropperDiv a reusable component, and updated README for OSS config Signed-off-by: Kininaru <shiftregister233@outlook.com> Convert ts to js Signed-off-by: Kininaru <shiftregister233@outlook.com> removed ts Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: set avatar link to string 255 Signed-off-by: Kininaru <shiftregister233@outlook.com> fix: updated yarn lock Signed-off-by: Kininaru <shiftregister233@outlook.com> add: Casbin license Signed-off-by: Kininaru <shiftregister233@outlook.com>
2021-03-14 15:50:36 +08:00
Avatar string `xorm:"varchar(255)" json:"avatar"`
2021-02-15 22:14:19 +08:00
Email string `xorm:"varchar(100)" json:"email"`
Phone string `xorm:"varchar(100)" json:"phone"`
Affiliation string `xorm:"varchar(100)" json:"affiliation"`
Tag string `xorm:"varchar(100)" json:"tag"`
IsAdmin bool `json:"isAdmin"`
IsGlobalAdmin bool `json:"isGlobalAdmin"`
2021-05-02 12:18:28 +08:00
IsForbidden bool `json:"isForbidden"`
2021-05-08 18:51:37 +08:00
Hash string `xorm:"varchar(100)" json:"hash"`
2021-05-09 14:54:17 +08:00
PreHash string `xorm:"varchar(100)" json:"preHash"`
2021-02-14 00:22:24 +08:00
Github string `xorm:"varchar(100)" json:"github"`
2021-02-21 23:51:40 +08:00
Google string `xorm:"varchar(100)" json:"google"`
2021-04-18 23:14:46 +08:00
QQ string `xorm:"qq varchar(100)" json:"qq"`
WeChat string `xorm:"wechat varchar(100)" json:"wechat"`
2020-10-20 23:14:03 +08:00
}
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
}
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 getUser(owner string, name string) *User {
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 GetUser(id string) *User {
owner, name := util.GetOwnerAndNameFromId(id)
return getUser(owner, name)
}
func UpdateUser(id string, user *User) bool {
owner, name := util.GetOwnerAndNameFromId(id)
if getUser(owner, name) == nil {
return false
}
2021-05-08 18:51:37 +08:00
user.UpdateUserHash()
2021-05-09 16:14:05 +08:00
affected, err := adapter.Engine.ID(core.PK{owner, name}).Cols("display_name", "avatar", "affiliation", "tag", "is_admin", "is_global_admin", "is_forbidden", "hash").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
}
func UpdateUserForOriginal(user *User) bool {
2021-05-09 16:14:05 +08:00
affected, err := adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols("display_name", "password", "phone", "avatar", "is_forbidden", "hash", "pre_hash").Update(user)
2021-05-09 15:44:12 +08:00
if err != nil {
panic(err)
}
2021-03-28 00:48:34 +08:00
return affected != 0
2020-10-20 23:14:03 +08:00
}
func AddUser(user *User) bool {
2021-02-13 20:10:41 +08:00
user.Id = util.GenerateId()
2021-05-16 21:22:20 +08:00
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
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
}
2021-05-16 22:58:30 +08:00
organization := GetOrganizationByUser(users[0])
2021-05-08 18:51:37 +08:00
for _, user := range users {
2021-05-16 21:22:20 +08:00
user.UpdateUserPassword(organization)
2021-05-08 18:51:37 +08:00
user.UpdateUserHash()
2021-05-09 14:54:17 +08:00
user.PreHash = user.Hash
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
}
func AddUsersSafe(users []*User) bool {
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]
fmt.Printf("Add users: [%d - %d].\n", start, end)
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-02-21 23:51:40 +08:00
func GetUserByField(organizationName string, field string, value string) *User {
user := User{Owner: organizationName}
existed, err := adapter.Engine.Where(fmt.Sprintf("%s=?", field), value).Get(&user)
2021-02-14 00:22:24 +08:00
if err != nil {
panic(err)
}
if existed {
return &user
} else {
return nil
}
}
2021-05-01 18:39:40 +08:00
func HasUserByField(organizationName string, field string, value string) bool {
return GetUserByField(organizationName, field, value) != nil
}
2021-05-01 20:23:20 +08:00
func GetUserByFields(organization string, field string) *User {
// check username
user := GetUserByField(organization, "name", field)
if user != nil {
return user
}
// check email
user = GetUserByField(organization, "email", field)
if user != nil {
return user
}
// check phone
user = GetUserByField(organization, "phone", field)
if user != nil {
return user
}
return nil
}
2021-04-27 19:35:40 +08:00
func SetUserField(user *User, field string, value string) bool {
2021-05-16 21:04:26 +08:00
if field == "password" {
2021-05-16 22:58:30 +08:00
organization := GetOrganizationByUser(user)
2021-05-16 21:04:26 +08:00
user.UpdateUserPassword(organization)
value = user.Password
}
affected, err := adapter.Engine.Table(user).ID(core.PK{user.Owner, user.Name}).Update(map[string]interface{}{field: value})
2021-02-14 00:22:24 +08:00
if err != nil {
panic(err)
}
2021-05-16 21:22:20 +08:00
user = getUser(user.Owner, user.Name)
user.UpdateUserHash()
2021-05-16 23:08:55 +08:00
_, err = adapter.Engine.ID(core.PK{user.Owner, user.Name}).Cols("hash").Update(user)
2021-05-16 21:22:20 +08:00
if err != nil {
panic(err)
}
return affected != 0
2021-02-14 00:22:24 +08:00
}
2021-04-18 23:14:46 +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-04-18 23:14:46 +08:00
func GetUserField(user *User, field string) string {
// https://socketloop.com/tutorials/golang-how-to-get-struct-field-and-value-by-name
u := reflect.ValueOf(user)
f := reflect.Indirect(u).FieldByName(field)
return f.String()
}
2021-05-08 00:06:30 +08:00
func GetMaskedUser(user *User) *User {
2021-05-09 14:53:05 +08:00
if user.Password != "" {
user.Password = "***"
}
2021-05-08 00:06:30 +08:00
return user
}
func GetMaskedUsers(users []*User) []*User {
for _, user := range users {
user = GetMaskedUser(user)
}
return users
}
2021-05-08 18:51:37 +08:00
func calculateHash(user *User) string {
s := strings.Join([]string{user.Id, user.Password, user.DisplayName, user.Avatar, user.Phone}, "|")
2021-05-08 22:04:45 +08:00
return util.GetMd5Hash(s)
2021-05-08 18:51:37 +08:00
}
func (user *User) UpdateUserHash() {
hash := calculateHash(user)
user.Hash = hash
}
2021-05-08 22:04:45 +08:00
2021-05-16 21:04:26 +08:00
func (user *User) UpdateUserPassword(organization *Organization) {
if organization.PasswordType == "salt" {
user.Password = getSaltedPassword(user.Password, organization.PasswordSalt)
}
}
2021-05-08 22:04:45 +08:00
func (user *User) GetId() string {
return fmt.Sprintf("%s/%s", user.Owner, user.Name)
}