[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>
This commit is contained in:
parent
eb1bf2377b
commit
d5b6931dbe
7 changed files with 77 additions and 20 deletions
|
@ -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 listOptions.Page != 0 {
|
||||
sess = listOptions.setSessionPagination(sess)
|
||||
if len(opts.Name) != 0 {
|
||||
sess = sess.Where("name=?", opts.Name)
|
||||
}
|
||||
|
||||
tokens := make([]*AccessToken, 0, listOptions.PageSize)
|
||||
sess = sess.Desc("id")
|
||||
|
||||
if opts.Page != 0 {
|
||||
sess = opts.setSessionPagination(sess)
|
||||
|
||||
tokens := make([]*AccessToken, 0, opts.PageSize)
|
||||
return tokens, sess.Find(&tokens)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue