From 10eac10d76af02378356422f68ca5821070867c5 Mon Sep 17 00:00:00 2001 From: yangjie727 <78058281@qq.com> Date: Thu, 14 Dec 2023 12:27:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B4=A6=E6=88=B7=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=97=B6=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 126 ++++++++++++++-------------- tests/account/CreateAccount_test.go | 2 +- tests/user/AddUserToAccount_test.go | 4 +- utils/utils.go | 27 ++++-- 4 files changed, 86 insertions(+), 73 deletions(-) diff --git a/main.go b/main.go index 6bd2080..5c6af53 100644 --- a/main.go +++ b/main.go @@ -92,13 +92,13 @@ func (s *serverUser) AddUserToAccount(ctx context.Context, in *pb.AddUserToAccou defaultQos := configValue.Slurm.DefaultQOS // 检查用户名、账户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - resultUser := utils.ContainsUppercase(in.UserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) // 重新判断一下 + resultUser := utils.CheckAccountOrUserStrings(in.UserId) // 重新判断一下 + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -234,13 +234,13 @@ func (s *serverUser) RemoveUserFromAccount(ctx context.Context, in *pb.RemoveUse clusterName := configValue.MySQLConfig.ClusterName // 检查账户名、用户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - resultUser := utils.ContainsUppercase(in.UserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -415,13 +415,13 @@ func (s *serverUser) BlockUserInAccount(ctx context.Context, in *pb.BlockUserInA logger.Infof("Received request BlockUserInAccount: %v", in) // 检查账户名、用户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - resultUser := utils.ContainsUppercase(in.UserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -488,13 +488,13 @@ func (s *serverUser) UnblockUserInAccount(ctx context.Context, in *pb.UnblockUse // 记录日志 logger.Infof("Received request UnblockUserInAccount: %v", in) // 检查账户名、用户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - resultUser := utils.ContainsUppercase(in.UserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -567,13 +567,13 @@ func (s *serverUser) QueryUserInAccountBlockStatus(ctx context.Context, in *pb.Q // 记录日志 logger.Infof("Received request QueryUserInAccountBlockStatus: %v", in) // 检查账户名、用户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - resultUser := utils.ContainsUppercase(in.UserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -635,12 +635,12 @@ func (s *serverAccount) ListAccounts(ctx context.Context, in *pb.ListAccountsReq // 记录日志 logger.Infof("Received request ListAccounts: %v", in) // 检查用户名中是否包含大写字母 - resultUser := utils.ContainsUppercase(in.UserId) - if resultUser { + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "USER_CONTAIN_UPPER_LETTER", + Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The username contains uppercase letters.") + st := status.New(codes.Internal, "The username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -704,13 +704,13 @@ func (s *serverAccount) CreateAccount(ctx context.Context, in *pb.CreateAccountR // 记录日志 logger.Infof("Received request CreateAccount: %v", in) // 检查账户名、用户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - resultUser := utils.ContainsUppercase(in.OwnerUserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + resultUser := utils.CheckAccountOrUserStrings(in.OwnerUserId) + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -785,12 +785,12 @@ func (s *serverAccount) BlockAccount(ctx context.Context, in *pb.BlockAccountReq // 记录日志 logger.Infof("Received request BlockAccount: %v", in) // 检查账户名中是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - if resultAcct { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + if !resultAcct { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account contains uppercase letters.") + st := status.New(codes.Internal, "The account contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -876,12 +876,12 @@ func (s *serverAccount) UnblockAccount(ctx context.Context, in *pb.UnblockAccoun // 记录日志 logger.Infof("Received request UnblockAccount: %v", in) // 检查用户名中是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - if resultAcct { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + if !resultAcct { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account contains uppercase letters.") + st := status.New(codes.Internal, "The account contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -1045,12 +1045,12 @@ func (s *serverAccount) QueryAccountBlockStatus(ctx context.Context, in *pb.Quer // 记录日志 logger.Infof("Received request QueryAccountBlockStatus: %v", in) // 检查用户名中是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.AccountName) - if resultAcct { + resultAcct := utils.CheckAccountOrUserStrings(in.AccountName) + if !resultAcct { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account contains uppercase letters.") + st := status.New(codes.Internal, "The account contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -1299,12 +1299,12 @@ func (s *serverConfig) GetAvailablePartitions(ctx context.Context, in *pb.GetAva // 记录日志 logger.Infof("Received request GetAvailablePartitions: %v", in) // 检查用户名中是否包含大写字母 - resultUser := utils.ContainsUppercase(in.UserId) - if resultUser { + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "USER_CONTAIN_UPPER_LETTER", + Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The username contains uppercase letters.") + st := status.New(codes.Internal, "The username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -1836,12 +1836,12 @@ func (s *serverJob) CancelJob(ctx context.Context, in *pb.CancelJobRequest) (*pb // 记录日志 logger.Infof("Received request CancelJob: %v", in) // 检查用户名中是否包含大写字母 - resultUser := utils.ContainsUppercase(in.UserId) - if resultUser { + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "USER_CONTAIN_UPPER_LETTER", + Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The username contains uppercase letters.") + st := status.New(codes.Internal, "The username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -2913,13 +2913,13 @@ func (s *serverJob) SubmitJob(ctx context.Context, in *pb.SubmitJobRequest) (*pb // 记录日志 logger.Infof("Received request SubmitJob: %v", in) // 判断账户名、用户名是否包含大写字母 - resultAcct := utils.ContainsUppercase(in.Account) - resultUser := utils.ContainsUppercase(in.UserId) - if resultAcct || resultUser { + resultAcct := utils.CheckAccountOrUserStrings(in.Account) + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultAcct || !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "ACCOUNT_USER_CONTAIN_UPPER_LETTER", + Reason: "ACCOUNT_USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The account or username contains uppercase letters.") + st := status.New(codes.Internal, "The account or username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } @@ -3017,12 +3017,12 @@ func (s *serverJob) SubmitScriptAsJob(ctx context.Context, in *pb.SubmitScriptAs ) // 记录日志 logger.Infof("Received request SubmitFileAsJob: %v", in) - resultUser := utils.ContainsUppercase(in.UserId) - if resultUser { + resultUser := utils.CheckAccountOrUserStrings(in.UserId) + if !resultUser { errInfo := &errdetails.ErrorInfo{ - Reason: "USER_CONTAIN_UPPER_LETTER", + Reason: "USER_CONTAIN_ILLEGAL_CHARACTERS", } - st := status.New(codes.Internal, "The username contains uppercase letters.") + st := status.New(codes.Internal, "The username contains illegal characters.") st, _ = st.WithDetails(errInfo) return nil, st.Err() } diff --git a/tests/account/CreateAccount_test.go b/tests/account/CreateAccount_test.go index 0a4257f..eb84030 100644 --- a/tests/account/CreateAccount_test.go +++ b/tests/account/CreateAccount_test.go @@ -21,7 +21,7 @@ func TestCreateAccount(t *testing.T) { // Call the Add RPC with test data req := &pb.CreateAccountRequest{ - AccountName: "this11", + AccountName: "t1his11jjj", OwnerUserId: "test16", } _, err = client.CreateAccount(context.Background(), req) diff --git a/tests/user/AddUserToAccount_test.go b/tests/user/AddUserToAccount_test.go index ac50315..1e0c2d2 100644 --- a/tests/user/AddUserToAccount_test.go +++ b/tests/user/AddUserToAccount_test.go @@ -21,8 +21,8 @@ func TestAddUserToAccount(t *testing.T) { // Call the Add RPC with test data req := &pb.AddUserToAccountRequest{ - UserId: "test06", - AccountName: "x_admin", + UserId: "test06666", + AccountName: "t1his11jjj", } _, err = client.AddUserToAccount(context.Background(), req) if err != nil { diff --git a/utils/utils.go b/utils/utils.go index 9e4a405..b641263 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,9 +6,9 @@ import ( "io/ioutil" "log" "reflect" + "regexp" "sort" "strconv" - "unicode" "os/exec" "os/user" @@ -338,13 +338,26 @@ func GetUserNameByUid(uid int) (string, error) { } // 判断字符串中是否包含大写字母 -func ContainsUppercase(s string) bool { - for _, char := range s { - if unicode.IsUpper(char) { - return true - } +func CheckAccountOrUserStrings(s string) bool { + + pattern := "^[a-z0-9_]+$" + // 编译正则表达式 + reg := regexp.MustCompile(pattern) + // 使用正则表达式判断字符串是否符合模式 + if reg.MatchString(s) { + return true + // fmt.Println("字符串符合要求") + } else { + // fmt.Println("字符串不符合要求") + return false } - return false + + // for _, char := range s { + // if unicode.IsUpper(char) { + // return true + // } + // } + // return false } // 本地提交作业函数