2018-05-17 09:35:00 +05:30
|
|
|
// 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 21:00:52 +05:30
|
|
|
"net/http"
|
|
|
|
|
2018-05-17 09:35:00 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-01-26 21:06:53 +05:30
|
|
|
auth "code.gitea.io/gitea/modules/forms"
|
2018-05-17 09:35:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2018-05-17 09:35:00 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
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 23:54:45 +05:30
|
|
|
loadApplicationsData(ctx)
|
2018-05-17 09:35:00 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2018-05-17 09:35:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ApplicationsPost response for add user's access token
|
2021-01-26 21:06:53 +05:30
|
|
|
func ApplicationsPost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*auth.NewAccessTokenForm)
|
2018-05-17 09:35:00 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsApplications"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2018-06-18 23:54:45 +05:30
|
|
|
loadApplicationsData(ctx)
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplSettingsApplications)
|
2018-05-17 09:35:00 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t := &models.AccessToken{
|
|
|
|
UID: ctx.User.ID,
|
|
|
|
Name: form.Name,
|
|
|
|
}
|
2020-04-14 00:32:48 +05:30
|
|
|
|
|
|
|
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 09:35:00 +05:30
|
|
|
if err := models.NewAccessToken(t); err != nil {
|
|
|
|
ctx.ServerError("NewAccessToken", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
|
2019-05-04 21:15:34 +05:30
|
|
|
ctx.Flash.Info(t.Token)
|
2018-05-17 09:35:00 +05:30
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteApplication response for delete user access token
|
|
|
|
func DeleteApplication(ctx *context.Context) {
|
|
|
|
if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
|
|
|
|
ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2018-05-17 09:35:00 +05:30
|
|
|
"redirect": setting.AppSubURL + "/user/settings/applications",
|
|
|
|
})
|
|
|
|
}
|
2018-06-18 23:54:45 +05:30
|
|
|
|
|
|
|
func loadApplicationsData(ctx *context.Context) {
|
2020-08-28 13:39:33 +05:30
|
|
|
tokens, err := models.ListAccessTokens(models.ListAccessTokensOptions{UserID: ctx.User.ID})
|
2018-06-18 23:54:45 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("ListAccessTokens", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Tokens"] = tokens
|
2019-03-08 22:12:50 +05:30
|
|
|
ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
|
|
|
|
if setting.OAuth2.Enable {
|
|
|
|
ctx.Data["Applications"], err = models.GetOAuth2ApplicationsByUserID(ctx.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
2019-04-17 13:48:16 +05:30
|
|
|
ctx.Data["Grants"], err = models.GetOAuth2GrantsByUserID(ctx.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetOAuth2GrantsByUserID", err)
|
|
|
|
return
|
|
|
|
}
|
2019-03-08 22:12:50 +05:30
|
|
|
}
|
2018-06-18 23:54:45 +05:30
|
|
|
}
|