mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
3.2 KiB
121 lines
3.2 KiB
9 years ago
|
// Copyright 2015 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.
|
||
|
|
||
9 years ago
|
package user
|
||
9 years ago
|
|
||
|
import (
|
||
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/routes/api/v1/convert"
|
||
|
"github.com/gogits/gogs/routes/api/v1/repo"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
func GetUserByParamsName(c *context.APIContext, name string) *models.User {
|
||
|
user, err := models.GetUserByName(c.Params(name))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetUserByName", err)
|
||
9 years ago
|
}
|
||
|
return nil
|
||
9 years ago
|
}
|
||
9 years ago
|
return user
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// GetUserByParams returns user whose name is presented in URL paramenter.
|
||
8 years ago
|
func GetUserByParams(c *context.APIContext) *models.User {
|
||
|
return GetUserByParamsName(c, ":username")
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func composePublicKeysAPILink() string {
|
||
8 years ago
|
return setting.AppURL + "api/v1/user/keys/"
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func listPublicKeys(c *context.APIContext, uid int64) {
|
||
9 years ago
|
keys, err := models.ListPublicKeys(uid)
|
||
|
if err != nil {
|
||
8 years ago
|
c.Error(500, "ListPublicKeys", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
apiLink := composePublicKeysAPILink()
|
||
|
apiKeys := make([]*api.PublicKey, len(keys))
|
||
|
for i := range keys {
|
||
9 years ago
|
apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, &apiKeys)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-your-public-keys
|
||
8 years ago
|
func ListMyPublicKeys(c *context.APIContext) {
|
||
|
listPublicKeys(c, c.User.ID)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user
|
||
8 years ago
|
func ListPublicKeys(c *context.APIContext) {
|
||
|
user := GetUserByParams(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
listPublicKeys(c, user.ID)
|
||
9 years ago
|
}
|
||
|
|
||
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key
|
||
8 years ago
|
func GetPublicKey(c *context.APIContext) {
|
||
|
key, err := models.GetPublicKeyByID(c.ParamsInt64(":id"))
|
||
9 years ago
|
if err != nil {
|
||
|
if models.IsErrKeyNotExist(err) {
|
||
8 years ago
|
c.Status(404)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "GetPublicKeyByID", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
apiLink := composePublicKeysAPILink()
|
||
8 years ago
|
c.JSON(200, convert.ToPublicKey(apiLink, key))
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// CreateUserPublicKey creates new public key to given user by ID.
|
||
8 years ago
|
func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
|
||
9 years ago
|
content, err := models.CheckPublicKeyString(form.Key)
|
||
|
if err != nil {
|
||
8 years ago
|
repo.HandleCheckKeyStringError(c, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
key, err := models.AddPublicKey(uid, form.Title, content)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
repo.HandleAddKeyError(c, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
apiLink := composePublicKeysAPILink()
|
||
8 years ago
|
c.JSON(201, convert.ToPublicKey(apiLink, key))
|
||
9 years ago
|
}
|
||
|
|
||
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#create-a-public-key
|
||
8 years ago
|
func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
|
||
|
CreateUserPublicKey(c, form, c.User.ID)
|
||
9 years ago
|
}
|
||
|
|
||
|
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#delete-a-public-key
|
||
8 years ago
|
func DeletePublicKey(c *context.APIContext) {
|
||
|
if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
|
||
9 years ago
|
if models.IsErrKeyAccessDenied(err) {
|
||
8 years ago
|
c.Error(403, "", "You do not have access to this key")
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Error(500, "DeletePublicKey", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Status(204)
|
||
9 years ago
|
}
|