| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
- // Copyright 2018 - Present, Gitote. All rights reserved.
- //
- // This source code is licensed under the MIT license found in the
- // LICENSE file in the root directory of this source tree.
- package user
- import (
- "gitote/gitote/models"
- "gitote/gitote/models/errors"
- "gitote/gitote/pkg/context"
- "gitote/gitote/pkg/setting"
- "gitote/gitote/routes/api/v1/convert"
- "gitote/gitote/routes/api/v1/repo"
- api "gitlab.com/gitote/go-gitote-client"
- )
- // GetUserByParamsName get user by name
- func GetUserByParamsName(c *context.APIContext, name string) *models.User {
- user, err := models.GetUserByName(c.Params(name))
- if err != nil {
- if errors.IsUserNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetUserByName", err)
- }
- return nil
- }
- return user
- }
- // GetUserByParams returns user whose name is presented in URL paramenter.
- func GetUserByParams(c *context.APIContext) *models.User {
- return GetUserByParamsName(c, ":username")
- }
- func composePublicKeysAPILink() string {
- return setting.AppURL + "api/v1/user/keys/"
- }
- func listPublicKeys(c *context.APIContext, uid int64) {
- keys, err := models.ListPublicKeys(uid)
- if err != nil {
- c.Error(500, "ListPublicKeys", err)
- return
- }
- apiLink := composePublicKeysAPILink()
- apiKeys := make([]*api.PublicKey, len(keys))
- for i := range keys {
- apiKeys[i] = convert.ToPublicKey(apiLink, keys[i])
- }
- c.JSON(200, &apiKeys)
- }
- // ListMyPublicKeys list all of the authenticated user's public keys
- func ListMyPublicKeys(c *context.APIContext) {
- listPublicKeys(c, c.User.ID)
- }
- // ListPublicKeys list the given user's public keys
- func ListPublicKeys(c *context.APIContext) {
- user := GetUserByParams(c)
- if c.Written() {
- return
- }
- listPublicKeys(c, user.ID)
- }
- // GetPublicKey get a public key
- func GetPublicKey(c *context.APIContext) {
- key, err := models.GetPublicKeyByID(c.ParamsInt64(":id"))
- if err != nil {
- if models.IsErrKeyNotExist(err) {
- c.Status(404)
- } else {
- c.Error(500, "GetPublicKeyByID", err)
- }
- return
- }
- apiLink := composePublicKeysAPILink()
- c.JSON(200, convert.ToPublicKey(apiLink, key))
- }
- // CreateUserPublicKey creates new public key to given user by ID.
- func CreateUserPublicKey(c *context.APIContext, form api.CreateKeyOption, uid int64) {
- content, err := models.CheckPublicKeyString(form.Key)
- if err != nil {
- repo.HandleCheckKeyStringError(c, err)
- return
- }
- key, err := models.AddPublicKey(uid, form.Title, content)
- if err != nil {
- repo.HandleAddKeyError(c, err)
- return
- }
- apiLink := composePublicKeysAPILink()
- c.JSON(201, convert.ToPublicKey(apiLink, key))
- }
- // CreatePublicKey create one public key for me
- func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
- CreateUserPublicKey(c, form, c.User.ID)
- }
- // DeletePublicKey delete one public key
- func DeletePublicKey(c *context.APIContext) {
- if err := models.DeletePublicKey(c.User, c.ParamsInt64(":id")); err != nil {
- if models.IsErrKeyAccessDenied(err) {
- c.Error(403, "", "You do not have access to this key")
- } else {
- c.Error(500, "DeletePublicKey", err)
- }
- return
- }
- c.Status(204)
- }
|