Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint models/ssh_key.go #267

Merged
merged 1 commit into from
Nov 27, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions models/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ const (

var sshOpLocker sync.Mutex

// KeyType specifies the key type
type KeyType int

const (
// KeyTypeUser specifies the user key
KeyTypeUser = iota + 1
// KeyTypeDeploy specifies the deploy key
KeyTypeDeploy
)

Expand All @@ -58,28 +61,31 @@ type PublicKey struct {
HasUsed bool `xorm:"-"`
}

func (k *PublicKey) BeforeInsert() {
k.CreatedUnix = time.Now().Unix()
// BeforeInsert will be invoked by XORM before inserting a record
func (key *PublicKey) BeforeInsert() {
key.CreatedUnix = time.Now().Unix()
}

func (k *PublicKey) BeforeUpdate() {
k.UpdatedUnix = time.Now().Unix()
// BeforeUpdate is invoked from XORM before updating this object.
func (key *PublicKey) BeforeUpdate() {
key.UpdatedUnix = time.Now().Unix()
}

func (k *PublicKey) AfterSet(colName string, _ xorm.Cell) {
// AfterSet is invoked from XORM after setting the value of a field of this object.
func (key *PublicKey) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
k.Created = time.Unix(k.CreatedUnix, 0).Local()
key.Created = time.Unix(key.CreatedUnix, 0).Local()
case "updated_unix":
k.Updated = time.Unix(k.UpdatedUnix, 0).Local()
k.HasUsed = k.Updated.After(k.Created)
k.HasRecentActivity = k.Updated.Add(7 * 24 * time.Hour).After(time.Now())
key.Updated = time.Unix(key.UpdatedUnix, 0).Local()
key.HasUsed = key.Updated.After(key.Created)
key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now())
}
}

// OmitEmail returns content of public key without email address.
func (k *PublicKey) OmitEmail() string {
return strings.Join(strings.Split(k.Content, " ")[:2], " ")
func (key *PublicKey) OmitEmail() string {
return strings.Join(strings.Split(key.Content, " ")[:2], " ")
}

// AuthorizedString returns formatted public key string for authorized_keys file.
Expand Down Expand Up @@ -573,32 +579,35 @@ type DeployKey struct {
HasUsed bool `xorm:"-"`
}

func (k *DeployKey) BeforeInsert() {
k.CreatedUnix = time.Now().Unix()
// BeforeInsert will be invoked by XORM before inserting a record
func (key *DeployKey) BeforeInsert() {
key.CreatedUnix = time.Now().Unix()
}

func (k *DeployKey) BeforeUpdate() {
k.UpdatedUnix = time.Now().Unix()
// BeforeUpdate is invoked from XORM before updating this object.
func (key *DeployKey) BeforeUpdate() {
key.UpdatedUnix = time.Now().Unix()
}

func (k *DeployKey) AfterSet(colName string, _ xorm.Cell) {
// AfterSet is invoked from XORM after setting the value of a field of this object.
func (key *DeployKey) AfterSet(colName string, _ xorm.Cell) {
switch colName {
case "created_unix":
k.Created = time.Unix(k.CreatedUnix, 0).Local()
key.Created = time.Unix(key.CreatedUnix, 0).Local()
case "updated_unix":
k.Updated = time.Unix(k.UpdatedUnix, 0).Local()
k.HasUsed = k.Updated.After(k.Created)
k.HasRecentActivity = k.Updated.Add(7 * 24 * time.Hour).After(time.Now())
key.Updated = time.Unix(key.UpdatedUnix, 0).Local()
key.HasUsed = key.Updated.After(key.Created)
key.HasRecentActivity = key.Updated.Add(7 * 24 * time.Hour).After(time.Now())
}
}

// GetContent gets associated public key content.
func (k *DeployKey) GetContent() error {
pkey, err := GetPublicKeyByID(k.KeyID)
func (key *DeployKey) GetContent() error {
pkey, err := GetPublicKeyByID(key.KeyID)
if err != nil {
return err
}
k.Content = pkey.Content
key.Content = pkey.Content
return nil
}

Expand Down