casdoor/object/check.go

97 lines
2.9 KiB
Go
Raw Normal View History

2021-03-13 23:06:03 +08:00
// Copyright 2021 The casbin Authors. All Rights Reserved.
2021-03-06 16:39:17 +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.
2021-02-11 22:56:08 +08:00
package object
2021-05-01 16:50:47 +08:00
import (
"fmt"
"regexp"
2021-05-01 17:45:01 +08:00
"github.com/casdoor/casdoor/util"
2021-05-01 16:50:47 +08:00
)
var reWhiteSpace *regexp.Regexp
func init() {
reWhiteSpace, _ = regexp.Compile("\\s")
}
2021-02-14 01:04:51 +08:00
2021-05-15 13:54:23 +08:00
func CheckUserSignup(organizationName string, username string, password string, displayName string, email string, phone string, affiliation string) string {
organization := getOrganization("admin", organizationName)
2021-05-01 18:39:40 +08:00
if len(username) == 0 {
return "username cannot be blank"
} else if len(password) == 0 {
return "password cannot be blank"
2021-05-15 13:54:23 +08:00
} else if organization == nil {
2021-05-01 18:39:40 +08:00
return "organization does not exist"
} else if reWhiteSpace.MatchString(username) {
2021-05-01 16:50:47 +08:00
return "username cannot contain white spaces"
2021-05-15 13:54:23 +08:00
} else if HasUserByField(organizationName, "name", username) {
2021-02-11 22:56:08 +08:00
return "username already exists"
2021-05-15 13:54:23 +08:00
} else if HasUserByField(organizationName, "email", email) {
2021-05-01 18:39:40 +08:00
return "email already exists"
2021-05-15 13:54:23 +08:00
} else if HasUserByField(organizationName, "phone", phone) {
2021-05-01 18:39:40 +08:00
return "phone already exists"
2021-05-01 17:45:01 +08:00
} else if displayName == "" {
return "displayName cannot be blank"
} else if affiliation == "" {
return "affiliation cannot be blank"
} else if !util.IsEmailValid(email) {
return "email is invalid"
2021-05-15 13:54:23 +08:00
} else if organization.PhonePrefix == "86" && !util.IsPhoneCnValid(phone) {
2021-05-01 17:45:01 +08:00
return "phone number is invalid"
2021-02-11 22:56:08 +08:00
} else {
return ""
}
}
2021-05-03 10:13:32 +08:00
func checkPassword(user *User, password string) string {
2021-05-05 23:32:21 +08:00
organization := getOrganization("admin", user.Owner)
if organization.PasswordType == "plain" {
2021-05-03 10:13:32 +08:00
if password == user.Password {
return ""
} else {
return "password incorrect"
}
2021-05-05 23:32:21 +08:00
} else if organization.PasswordType == "salt" {
2021-05-16 18:18:55 +08:00
if password == user.Password || getSaltedPassword(password, organization.PasswordSalt) == user.Password {
2021-05-03 10:13:32 +08:00
return ""
} else {
return "password incorrect"
}
} else {
2021-05-05 23:32:21 +08:00
return fmt.Sprintf("unsupported password type: %s", organization.PasswordType)
2021-05-03 10:13:32 +08:00
}
}
2021-05-01 19:45:40 +08:00
func CheckUserLogin(organization string, username string, password string) (*User, string) {
2021-05-01 20:23:20 +08:00
user := GetUserByFields(organization, username)
2021-05-01 19:45:40 +08:00
if user == nil {
2021-05-01 20:23:20 +08:00
return nil, "the user does not exist, please sign up first"
2021-02-11 22:56:08 +08:00
}
2021-05-05 23:32:21 +08:00
if user.IsForbidden {
return nil, "the user is forbidden to sign in, please contact the administrator"
}
2021-05-03 10:13:32 +08:00
msg := checkPassword(user, password)
if msg != "" {
return nil, msg
2021-02-11 22:56:08 +08:00
}
2021-05-01 19:45:40 +08:00
return user, ""
2021-02-11 22:56:08 +08:00
}