diff options
author | Unknwon <u@gogs.io> | 2016-03-09 19:53:30 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2016-03-09 19:53:30 -0500 |
commit | ad513a20e939691828ba415c9a565e8ff3daa95f (patch) | |
tree | c15aa2c82a5ed242ba51c837804f050690dd60ad /models/token.go | |
parent | 0c9a616326ba096a2ff6c058cc96950f68c0fa6e (diff) | |
download | gitea-ad513a20e939691828ba415c9a565e8ff3daa95f.tar.gz gitea-ad513a20e939691828ba415c9a565e8ff3daa95f.zip |
#2302 Replace time.Time with Unix Timestamp (int64)
Diffstat (limited to 'models/token.go')
-rw-r--r-- | models/token.go | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/models/token.go b/models/token.go index adf50a4321..38d83e2172 100644 --- a/models/token.go +++ b/models/token.go @@ -7,6 +7,7 @@ package models import ( "time" + "github.com/go-xorm/xorm" gouuid "github.com/satori/go.uuid" "github.com/gogits/gogs/modules/base" @@ -14,16 +15,38 @@ import ( // AccessToken represents a personal access token. type AccessToken struct { - ID int64 `xorm:"pk autoincr"` - UID int64 `xorm:"INDEX"` - Name string - Sha1 string `xorm:"UNIQUE VARCHAR(40)"` - Created time.Time `xorm:"CREATED"` - Updated time.Time + ID int64 `xorm:"pk autoincr"` + UID int64 `xorm:"INDEX"` + Name string + Sha1 string `xorm:"UNIQUE VARCHAR(40)"` + + Created time.Time `xorm:"-"` + CreatedUnix int64 + Updated time.Time `xorm:"-"` // Note: Updated must below Created for AfterSet. + UpdatedUnix int64 HasRecentActivity bool `xorm:"-"` HasUsed bool `xorm:"-"` } +func (t *AccessToken) BeforeInsert() { + t.CreatedUnix = time.Now().UTC().Unix() +} + +func (t *AccessToken) BeforeUpdate() { + t.UpdatedUnix = time.Now().UTC().Unix() +} + +func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) { + switch colName { + case "created_unix": + t.Created = time.Unix(t.CreatedUnix, 0).Local() + case "updated_unix": + t.Updated = time.Unix(t.UpdatedUnix, 0).Local() + t.HasUsed = t.Updated.After(t.Created) + t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now()) + } +} + // NewAccessToken creates new access token. func NewAccessToken(t *AccessToken) error { t.Sha1 = base.EncodeSha1(gouuid.NewV4().String()) @@ -46,16 +69,7 @@ func GetAccessTokenBySHA(sha string) (*AccessToken, error) { // ListAccessTokens returns a list of access tokens belongs to given user. func ListAccessTokens(uid int64) ([]*AccessToken, error) { tokens := make([]*AccessToken, 0, 5) - err := x.Where("uid=?", uid).Desc("id").Find(&tokens) - if err != nil { - return nil, err - } - - for _, t := range tokens { - t.HasUsed = t.Updated.After(t.Created) - t.HasRecentActivity = t.Updated.Add(7 * 24 * time.Hour).After(time.Now()) - } - return tokens, nil + return tokens, x.Where("uid=?", uid).Desc("id").Find(&tokens) } // UpdateAccessToken updates information of access token. |