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