github.com/nhannv/mattermost-server@v5.11.1+incompatible/model/user_access_token.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  )
    11  
    12  type UserAccessToken struct {
    13  	Id          string `json:"id"`
    14  	Token       string `json:"token,omitempty"`
    15  	UserId      string `json:"user_id"`
    16  	Description string `json:"description"`
    17  	IsActive    bool   `json:"is_active"`
    18  }
    19  
    20  func (t *UserAccessToken) IsValid() *AppError {
    21  	if len(t.Id) != 26 {
    22  		return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.id.app_error", nil, "", http.StatusBadRequest)
    23  	}
    24  
    25  	if len(t.Token) != 26 {
    26  		return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.token.app_error", nil, "", http.StatusBadRequest)
    27  	}
    28  
    29  	if len(t.UserId) != 26 {
    30  		return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.user_id.app_error", nil, "", http.StatusBadRequest)
    31  	}
    32  
    33  	if len(t.Description) > 255 {
    34  		return NewAppError("UserAccessToken.IsValid", "model.user_access_token.is_valid.description.app_error", nil, "", http.StatusBadRequest)
    35  	}
    36  
    37  	return nil
    38  }
    39  
    40  func (t *UserAccessToken) PreSave() {
    41  	t.Id = NewId()
    42  	t.IsActive = true
    43  }
    44  
    45  func (t *UserAccessToken) ToJson() string {
    46  	b, _ := json.Marshal(t)
    47  	return string(b)
    48  }
    49  
    50  func UserAccessTokenFromJson(data io.Reader) *UserAccessToken {
    51  	var t *UserAccessToken
    52  	json.NewDecoder(data).Decode(&t)
    53  	return t
    54  }
    55  
    56  func UserAccessTokenListToJson(t []*UserAccessToken) string {
    57  	b, _ := json.Marshal(t)
    58  	return string(b)
    59  }
    60  
    61  func UserAccessTokenListFromJson(data io.Reader) []*UserAccessToken {
    62  	var t []*UserAccessToken
    63  	json.NewDecoder(data).Decode(&t)
    64  	return t
    65  }