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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var reWhiteSpace *regexp.Regexp
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
reWhiteSpace, _ = regexp.Compile("\\s")
|
|
|
|
}
|
2021-02-14 01:04:51 +08:00
|
|
|
|
2021-04-27 22:47:44 +08:00
|
|
|
func CheckUserSignup(userId string, password string) string {
|
2021-02-11 22:56:08 +08:00
|
|
|
if len(userId) == 0 || len(password) == 0 {
|
|
|
|
return "username and password cannot be blank"
|
2021-05-01 16:50:47 +08:00
|
|
|
} else if reWhiteSpace.MatchString(userId) {
|
|
|
|
return "username cannot contain white spaces"
|
2021-02-11 22:56:08 +08:00
|
|
|
} else if HasUser(userId) {
|
|
|
|
return "username already exists"
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CheckUserLogin(userId string, password string) string {
|
|
|
|
if !HasUser(userId) {
|
|
|
|
return "username does not exist, please sign up first"
|
|
|
|
}
|
|
|
|
|
|
|
|
if !IsPasswordCorrect(userId, password) {
|
|
|
|
return "password incorrect"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
2021-02-14 00:22:24 +08:00
|
|
|
|
2021-02-14 01:04:51 +08:00
|
|
|
func (user *User) getId() string {
|
|
|
|
return fmt.Sprintf("%s/%s", user.Owner, user.Name)
|
|
|
|
}
|
|
|
|
|
2021-02-21 23:51:40 +08:00
|
|
|
func GetUserIdByField(application *Application, field string, value string) string {
|
|
|
|
user := GetUserByField(application.Organization, field, value)
|
2021-02-14 00:22:24 +08:00
|
|
|
if user != nil {
|
2021-02-14 01:04:51 +08:00
|
|
|
return user.getId()
|
2021-02-14 00:22:24 +08:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|