aboutsummaryrefslogtreecommitdiff
path: root/models/token.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/token.go')
-rw-r--r--models/token.go18
1 files changed, 15 insertions, 3 deletions
diff --git a/models/token.go b/models/token.go
index 7e1dc166..b757c7d7 100644
--- a/models/token.go
+++ b/models/token.go
@@ -5,12 +5,13 @@
package models
import (
+ "fmt"
"time"
"github.com/go-xorm/xorm"
- gouuid "github.com/satori/go.uuid"
-
+ "github.com/gogs/gogs/models/errors"
"github.com/gogs/gogs/pkg/tool"
+ gouuid "github.com/satori/go.uuid"
)
// AccessToken represents a personal access token.
@@ -47,10 +48,21 @@ func (t *AccessToken) AfterSet(colName string, _ xorm.Cell) {
}
}
+func isAccessTokenNameExist(uid int64, name string) (bool, error) {
+ return x.Where("uid=?", uid).And("name=?", name).Get(&AccessToken{})
+}
+
// NewAccessToken creates new access token.
func NewAccessToken(t *AccessToken) error {
t.Sha1 = tool.SHA1(gouuid.NewV4().String())
- _, err := x.Insert(t)
+ has, err := isAccessTokenNameExist(t.UID, t.Name)
+ if err != nil {
+ return fmt.Errorf("IsAccessTokenNameExists: %v", err)
+ } else if has {
+ return errors.AccessTokenNameAlreadyExist{t.Name}
+ }
+
+ _, err = x.Insert(t)
return err
}