github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/lorry/engines/models/userinfo.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package models
    21  
    22  import (
    23  	"time"
    24  )
    25  
    26  // UserInfo is the user information for account management
    27  type UserInfo struct {
    28  	UserName string        `json:"userName"`
    29  	Password string        `json:"password,omitempty"`
    30  	Expired  string        `json:"expired,omitempty"`
    31  	ExpireAt time.Duration `json:"expireAt,omitempty"`
    32  	RoleName string        `json:"roleName,omitempty"`
    33  }
    34  
    35  func (user *UserInfo) UserNameValidator() error {
    36  	if user.UserName == "" {
    37  		return ErrNoUserName
    38  	}
    39  	return nil
    40  }
    41  
    42  func (user *UserInfo) PasswdValidator() error {
    43  	if user.Password == "" {
    44  		return ErrNoPassword
    45  	}
    46  	return nil
    47  }
    48  
    49  func (user *UserInfo) RoleValidator() error {
    50  	if user.RoleName == "" {
    51  		return ErrNoRoleName
    52  	}
    53  
    54  	roles := []RoleType{ReadOnlyRole, ReadWriteRole, SuperUserRole}
    55  	for _, role := range roles {
    56  		if role.EqualTo(user.RoleName) {
    57  			return nil
    58  		}
    59  	}
    60  	return ErrInvalidRoleName
    61  }
    62  
    63  func (user *UserInfo) UserNameAndPasswdValidator() error {
    64  	if err := user.UserNameValidator(); err != nil {
    65  		return err
    66  	}
    67  
    68  	if err := user.PasswdValidator(); err != nil {
    69  		return err
    70  	}
    71  	return nil
    72  }
    73  
    74  func (user *UserInfo) UserNameAndRoleValidator() error {
    75  	if err := user.UserNameValidator(); err != nil {
    76  		return err
    77  	}
    78  
    79  	if err := user.RoleValidator(); err != nil {
    80  		return err
    81  	}
    82  	return nil
    83  }