gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/AuthInfo.go (about)

     1  package corepayload
     2  
     3  import (
     4  	"gitlab.com/evatix-go/core/constants"
     5  	"gitlab.com/evatix-go/core/converters"
     6  	"gitlab.com/evatix-go/core/coredata/corejson"
     7  )
     8  
     9  type AuthInfo struct {
    10  	Identifier   string       `json:"Identifier,omitempty"`
    11  	ActionType   string       `json:"ActionType,omitempty"`
    12  	ResourceName string       `json:"ResourceName,omitempty"` // can be url or any name
    13  	SessionInfo  *SessionInfo `json:"SessionInfo,omitempty"`
    14  	UserInfo     *UserInfo    `json:"UserInfo,omitempty"`
    15  }
    16  
    17  // IdentifierInteger
    18  //
    19  // Invalid value returns constants.InvalidValue
    20  func (it *AuthInfo) IdentifierInteger() int {
    21  	if it.Identifier == "" {
    22  		return constants.InvalidValue
    23  	}
    24  
    25  	idInt, _ := converters.StringToIntegerWithDefault(
    26  		it.Identifier,
    27  		constants.InvalidValue)
    28  
    29  	return idInt
    30  }
    31  
    32  // IdentifierUnsignedInteger
    33  //
    34  // Invalid value returns constants.Zero
    35  func (it *AuthInfo) IdentifierUnsignedInteger() uint {
    36  	idInt := it.IdentifierInteger()
    37  
    38  	if idInt < 0 {
    39  		return constants.Zero
    40  	}
    41  
    42  	return uint(idInt)
    43  }
    44  
    45  func (it *AuthInfo) IsEmpty() bool {
    46  	return it == nil ||
    47  		it.ActionType == "" &&
    48  			it.ResourceName == "" &&
    49  			it.SessionInfo.IsEmpty() &&
    50  			it.UserInfo.IsEmpty()
    51  }
    52  
    53  func (it *AuthInfo) HasAnyItem() bool {
    54  	return !it.IsEmpty()
    55  }
    56  
    57  func (it *AuthInfo) IsActionTypeEmpty() bool {
    58  	return it == nil ||
    59  		it.ActionType == ""
    60  }
    61  
    62  func (it *AuthInfo) IsResourceNameEmpty() bool {
    63  	return it == nil ||
    64  		it.ResourceName == ""
    65  }
    66  
    67  func (it *AuthInfo) IsValid() bool {
    68  	return !it.IsEmpty()
    69  }
    70  
    71  func (it *AuthInfo) HasActionType() bool {
    72  	return it != nil &&
    73  		it.ActionType != ""
    74  }
    75  
    76  func (it *AuthInfo) HasResourceName() bool {
    77  	return it != nil &&
    78  		it.ResourceName != ""
    79  }
    80  
    81  func (it *AuthInfo) IsUserInfoEmpty() bool {
    82  	return it == nil ||
    83  		it.UserInfo.IsEmpty()
    84  }
    85  
    86  func (it *AuthInfo) IsSessionInfoEmpty() bool {
    87  	return it == nil ||
    88  		it.SessionInfo.IsEmpty()
    89  }
    90  
    91  func (it *AuthInfo) HasUserInfo() bool {
    92  	return !it.IsUserInfoEmpty()
    93  }
    94  
    95  func (it *AuthInfo) HasSessionInfo() bool {
    96  	return !it.IsSessionInfoEmpty()
    97  }
    98  
    99  func (it *AuthInfo) String() string {
   100  	return it.JsonPtr().JsonString()
   101  }
   102  
   103  // SetUserInfo
   104  //
   105  // on null creates new
   106  func (it *AuthInfo) SetUserInfo(
   107  	userInfo *UserInfo,
   108  ) *AuthInfo {
   109  	if it == nil {
   110  		return &AuthInfo{
   111  			UserInfo: userInfo,
   112  		}
   113  	}
   114  
   115  	it.UserInfo = userInfo
   116  
   117  	return it
   118  }
   119  
   120  func (it *AuthInfo) SetActionType(
   121  	actionType string,
   122  ) *AuthInfo {
   123  	if it == nil {
   124  		return &AuthInfo{
   125  			ActionType: actionType,
   126  		}
   127  	}
   128  
   129  	it.ActionType = actionType
   130  
   131  	return it
   132  }
   133  
   134  func (it *AuthInfo) SetResourceName(
   135  	resourceName string,
   136  ) *AuthInfo {
   137  	if it == nil {
   138  		return &AuthInfo{
   139  			ResourceName: resourceName,
   140  		}
   141  	}
   142  
   143  	it.ResourceName = resourceName
   144  
   145  	return it
   146  }
   147  
   148  func (it *AuthInfo) SetIdentifier(
   149  	identifier string,
   150  ) *AuthInfo {
   151  	if it == nil {
   152  		return &AuthInfo{
   153  			Identifier: identifier,
   154  		}
   155  	}
   156  
   157  	it.Identifier = identifier
   158  
   159  	return it
   160  }
   161  
   162  func (it *AuthInfo) SetSessionInfo(
   163  	sessionInfo *SessionInfo,
   164  ) *AuthInfo {
   165  	if it == nil {
   166  		return &AuthInfo{
   167  			SessionInfo: sessionInfo,
   168  		}
   169  	}
   170  
   171  	it.SessionInfo = sessionInfo
   172  
   173  	return it
   174  }
   175  
   176  func (it *AuthInfo) SetUserSystemUser(
   177  	user, systemUser *User,
   178  ) *AuthInfo {
   179  	if it == nil {
   180  		empty := &AuthInfo{}
   181  		empty.UserInfo = &UserInfo{}
   182  		empty.UserInfo.SetUserSystemUser(user, systemUser)
   183  
   184  		return empty
   185  	}
   186  
   187  	it.UserInfo.SetUserSystemUser(user, systemUser)
   188  
   189  	return it
   190  }
   191  
   192  // SetUser
   193  //
   194  // on null creates new
   195  func (it *AuthInfo) SetUser(
   196  	user *User,
   197  ) *AuthInfo {
   198  	if it == nil {
   199  		empty := &AuthInfo{}
   200  		empty.UserInfo = &UserInfo{}
   201  		empty.UserInfo.SetUser(user)
   202  
   203  		return empty
   204  	}
   205  
   206  	it.UserInfo.SetUser(user)
   207  
   208  	return it
   209  }
   210  
   211  // SetSystemUser
   212  //
   213  // on null creates new
   214  func (it *AuthInfo) SetSystemUser(
   215  	systemUser *User,
   216  ) *AuthInfo {
   217  	if it == nil {
   218  		empty := &AuthInfo{}
   219  		empty.UserInfo = &UserInfo{}
   220  		empty.UserInfo.SetSystemUser(systemUser)
   221  
   222  		return empty
   223  	}
   224  
   225  	it.UserInfo.SetSystemUser(systemUser)
   226  
   227  	return it
   228  }
   229  
   230  func (it *AuthInfo) PrettyJsonString() string {
   231  	return it.JsonPtr().PrettyJsonString()
   232  }
   233  
   234  func (it *AuthInfo) Json() corejson.Result {
   235  	return corejson.New(it)
   236  }
   237  
   238  func (it *AuthInfo) JsonPtr() *corejson.Result {
   239  	return corejson.NewPtr(it)
   240  }
   241  
   242  func (it *AuthInfo) Clone() AuthInfo {
   243  	return AuthInfo{
   244  		ActionType:   it.ActionType,
   245  		ResourceName: it.ResourceName,
   246  		SessionInfo:  it.SessionInfo.ClonePtr(),
   247  		UserInfo:     it.UserInfo.ClonePtr(),
   248  	}
   249  }
   250  
   251  func (it *AuthInfo) ClonePtr() *AuthInfo {
   252  	if it == nil {
   253  		return nil
   254  	}
   255  
   256  	return it.Clone().Ptr()
   257  }
   258  
   259  func (it AuthInfo) Ptr() *AuthInfo {
   260  	return &it
   261  }