github.com/XiaoMi/Gaea@v1.2.5/models/user.go (about)

     1  // Copyright 2019 The Gaea Authors. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package models
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"strings"
    21  )
    22  
    23  // 用户只读标识
    24  // read write privileges
    25  const (
    26  	// 只读
    27  	ReadOnly = 1
    28  	// 可读可写
    29  	ReadWrite = 2
    30  )
    31  
    32  // 用户读写分离标识
    33  const (
    34  	// NoReadWriteSplit 非读写分离
    35  	NoReadWriteSplit = 0
    36  	// ReadWriteSplit 读写分离
    37  	ReadWriteSplit = 1
    38  	// StatisticUser 统计用户
    39  	StatisticUser = 1
    40  )
    41  
    42  // User meand user struct
    43  type User struct {
    44  	UserName      string `json:"user_name"`
    45  	Password      string `json:"password"`
    46  	Namespace     string `json:"namespace"`
    47  	RWFlag        int    `json:"rw_flag"`        //1: 只读 2:读写
    48  	RWSplit       int    `json:"rw_split"`       //0: 不采用读写分离 1:读写分离
    49  	OtherProperty int    `json:"other_property"` // 1:统计用户
    50  }
    51  
    52  func (p *User) verify() error {
    53  	if p.UserName == "" {
    54  		return errors.New("missing user name")
    55  	}
    56  	p.UserName = strings.TrimSpace(p.UserName)
    57  
    58  	if p.Namespace == "" {
    59  		return fmt.Errorf("missing namespace name: %s", p.UserName)
    60  	}
    61  	p.Namespace = strings.TrimSpace(p.Namespace)
    62  
    63  	if p.Password == "" {
    64  		return fmt.Errorf("missing password: [%s]%s", p.Namespace, p.UserName)
    65  	}
    66  	p.Password = strings.TrimSpace(p.Password)
    67  
    68  	if p.RWFlag != ReadOnly && p.RWFlag != ReadWrite {
    69  		return fmt.Errorf("invalid RWFlag, user: %s, rwflag: %d", p.UserName, p.RWFlag)
    70  	}
    71  
    72  	if p.RWSplit != NoReadWriteSplit && p.RWSplit != ReadWriteSplit {
    73  		return fmt.Errorf("invalid RWSplit, user: %s, rwsplit: %d", p.UserName, p.RWSplit)
    74  	}
    75  
    76  	if p.OtherProperty != StatisticUser && p.OtherProperty != 0 {
    77  		return fmt.Errorf("invalid other property, user: %s, %d", p.UserName, p.OtherProperty)
    78  	}
    79  
    80  	return nil
    81  }