summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2020-08-28 10:09:33 +0200
committerGitHub <noreply@github.com>2020-08-28 11:09:33 +0300
commitd5b6931dbefff674fd84237555a7c2e284b63c5a (patch)
tree8e68a4f12b16ed1ae782668459cfcb19e78fa7d4 /models
parenteb1bf2377be15deb593a3e2426558d92c1973107 (diff)
downloadgitea-d5b6931dbefff674fd84237555a7c2e284b63c5a.tar.gz
gitea-d5b6931dbefff674fd84237555a7c2e284b63c5a.zip
[API] Delete Token accept names too (#12366)
* Delete Token accept names too * better description Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'models')
-rw-r--r--models/token.go25
-rw-r--r--models/token_test.go6
2 files changed, 21 insertions, 10 deletions
diff --git a/models/token.go b/models/token.go
index a18f15f325..1245098df0 100644
--- a/models/token.go
+++ b/models/token.go
@@ -82,16 +82,27 @@ func AccessTokenByNameExists(token *AccessToken) (bool, error) {
return x.Table("access_token").Where("name = ?", token.Name).And("uid = ?", token.UID).Exist()
}
+// ListAccessTokensOptions contain filter options
+type ListAccessTokensOptions struct {
+ ListOptions
+ Name string
+ UserID int64
+}
+
// ListAccessTokens returns a list of access tokens belongs to given user.
-func ListAccessTokens(uid int64, listOptions ListOptions) ([]*AccessToken, error) {
- sess := x.
- Where("uid=?", uid).
- Desc("id")
+func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
+ sess := x.Where("uid=?", opts.UserID)
+
+ if len(opts.Name) != 0 {
+ sess = sess.Where("name=?", opts.Name)
+ }
+
+ sess = sess.Desc("id")
- if listOptions.Page != 0 {
- sess = listOptions.setSessionPagination(sess)
+ if opts.Page != 0 {
+ sess = opts.setSessionPagination(sess)
- tokens := make([]*AccessToken, 0, listOptions.PageSize)
+ tokens := make([]*AccessToken, 0, opts.PageSize)
return tokens, sess.Find(&tokens)
}
diff --git a/models/token_test.go b/models/token_test.go
index 572a5de609..23d902adbc 100644
--- a/models/token_test.go
+++ b/models/token_test.go
@@ -83,7 +83,7 @@ func TestGetAccessTokenBySHA(t *testing.T) {
func TestListAccessTokens(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
- tokens, err := ListAccessTokens(1, ListOptions{})
+ tokens, err := ListAccessTokens(ListAccessTokensOptions{UserID: 1})
assert.NoError(t, err)
if assert.Len(t, tokens, 2) {
assert.Equal(t, int64(1), tokens[0].UID)
@@ -92,14 +92,14 @@ func TestListAccessTokens(t *testing.T) {
assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
}
- tokens, err = ListAccessTokens(2, ListOptions{})
+ tokens, err = ListAccessTokens(ListAccessTokensOptions{UserID: 2})
assert.NoError(t, err)
if assert.Len(t, tokens, 1) {
assert.Equal(t, int64(2), tokens[0].UID)
assert.Equal(t, "Token A", tokens[0].Name)
}
- tokens, err = ListAccessTokens(100, ListOptions{})
+ tokens, err = ListAccessTokens(ListAccessTokensOptions{UserID: 100})
assert.NoError(t, err)
assert.Empty(t, tokens)
}