2018-05-17 00:05:00 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2021-04-05 11:30:52 -04:00
|
|
|
"net/http"
|
|
|
|
|
2018-05-17 00:05:00 -04:00
|
|
|
"code.gitea.io/gitea/models"
|
2022-01-02 08:12:35 -05:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2018-05-17 00:05:00 -04:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 10:36:53 -05:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-06 15:44:05 -04:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2018-05-17 00:05:00 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSettingsApplications base.TplName = "user/settings/applications"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Applications render manage access token page
|
|
|
|
func Applications(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
2018-06-18 14:24:45 -04:00
|
|
|
loadApplicationsData(ctx)
|
2018-05-17 00:05:00 -04:00
|
|
|
|
2021-04-05 11:30:52 -04:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2018-05-17 00:05:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ApplicationsPost response for add user's access token
|
2021-01-26 10:36:53 -05:00
|
|
|
func ApplicationsPost(ctx *context.Context) {
|
2021-04-06 15:44:05 -04:00
|
|
|
form := web.GetForm(ctx).(*forms.NewAccessTokenForm)
|
2018-05-17 00:05:00 -04:00
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2018-06-18 14:24:45 -04:00
|
|
|
loadApplicationsData(ctx)
|
|
|
|
|
2021-04-05 11:30:52 -04:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2018-05-17 00:05:00 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t := &models.AccessToken{
|
2022-03-22 03:03:22 -04:00
|
|
|
UID: ctx.Doer.ID,
|
2018-05-17 00:05:00 -04:00
|
|
|
Name: form.Name,
|
|
|
|
}
|
2020-04-13 15:02:48 -04:00
|
|
|
|
|
|
|
exist, err := models.AccessTokenByNameExists(t)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("AccessTokenByNameExists", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
ctx.Flash.Error(ctx.Tr("settings.generate_token_name_duplicate", t.Name))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-17 00:05:00 -04:00
|
|
|
if err := models.NewAccessToken(t); err != nil {
|
|
|
|
ctx.ServerError("NewAccessToken", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
|
2019-05-04 11:45:34 -04:00
|
|
|
ctx.Flash.Info(t.Token)
|
2018-05-17 00:05:00 -04:00
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteApplication response for delete user access token
|
|
|
|
func DeleteApplication(ctx *context.Context) {
|
2022-03-22 03:03:22 -04:00
|
|
|
if err := models.DeleteAccessTokenByID(ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
|
2018-05-17 00:05:00 -04:00
|
|
|
ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
|
|
|
|
}
|
|
|
|
|
2021-04-05 11:30:52 -04:00
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2018-05-17 00:05:00 -04:00
|
|
|
"redirect": setting.AppSubURL + "/user/settings/applications",
|
|
|
|
})
|
|
|
|
}
|
2018-06-18 14:24:45 -04:00
|
|
|
|
|
|
|
func loadApplicationsData(ctx *context.Context) {
|
2022-03-22 03:03:22 -04:00
|
|
|
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.Doer.ID})
|
2018-06-18 14:24:45 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("ListAccessTokens", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tokens"] = tokens
|
2019-03-08 11:42:50 -05:00
|
|
|
ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
|
|
|
|
if setting.OAuth2.Enable {
|
2022-03-22 03:03:22 -04:00
|
|
|
ctx.Data["Applications"], err = auth.GetOAuth2ApplicationsByUserID(ctx.Doer.ID)
|
2019-03-08 11:42:50 -05:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 03:03:22 -04:00
|
|
|
ctx.Data["Grants"], err = auth.GetOAuth2GrantsByUserID(ctx.Doer.ID)
|
2019-04-17 04:18:16 -04:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOAuth2GrantsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-08 11:42:50 -05:00
|
|
|
}
|
2018-06-18 14:24:45 -04:00
|
|
|
}
|