diff options
author | 无闻 <joe2010xtmf@163.com> | 2014-04-10 14:38:48 -0400 |
---|---|---|
committer | 无闻 <joe2010xtmf@163.com> | 2014-04-10 14:38:48 -0400 |
commit | 8faa0dbcd77ec17bbf88041f46e2fc48f6ca6f31 (patch) | |
tree | 3dff34e53f34632532fd7a05e00e6f06b3e7fb82 /models/oauth2.go | |
parent | 2577940c30f6a6d15390974ab36f8c3d1e00f9f4 (diff) | |
parent | a4cbe79567072befd96cf1b7eb319de1e2809ca3 (diff) |
Merge pull request #70 from zhsso/git
Git
Diffstat (limited to 'models/oauth2.go')
-rw-r--r-- | models/oauth2.go | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/models/oauth2.go b/models/oauth2.go index a17d4e30..45728b0d 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -1,6 +1,10 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package models -import "fmt" +import "errors" // OT: Oauth2 Type const ( @@ -9,12 +13,18 @@ const ( OT_TWITTER ) +var ( + ErrOauth2RecordNotExists = errors.New("not exists oauth2 record") + ErrOauth2NotAssociatedWithUser = errors.New("not associated with user") +) + type Oauth2 struct { - Uid int64 `xorm:"pk"` // userId + Id int64 + Uid int64 // userId + User *User `xorm:"-"` Type int `xorm:"pk unique(oauth)"` // twitter,github,google... Identity string `xorm:"pk unique(oauth)"` // id.. Token string `xorm:"VARCHAR(200) not null"` - //RefreshTime time.Time `xorm:"created"` } func AddOauth2(oa *Oauth2) (err error) { @@ -24,16 +34,16 @@ func AddOauth2(oa *Oauth2) (err error) { return nil } -func GetOauth2User(identity string) (u *User, err error) { - oa := &Oauth2{} - oa.Identity = identity - exists, err := orm.Get(oa) +func GetOauth2(identity string) (oa *Oauth2, err error) { + oa = &Oauth2{Identity: identity} + isExist, err := orm.Get(oa) if err != nil { return + } else if !isExist { + return nil, ErrOauth2RecordNotExists + } else if oa.Uid == 0 { + return oa, ErrOauth2NotAssociatedWithUser } - if !exists { - err = fmt.Errorf("not exists oauth2: %s", identity) - return - } - return GetUserById(oa.Uid) + oa.User, err = GetUserById(oa.Uid) + return oa, err } |