code.gitea.io/gitea@v1.22.3/routers/web/user/setting/applications.go (about)

     1  // Copyright 2014 The Gogs Authors. All rights reserved.
     2  // Copyright 2018 The Gitea Authors. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package setting
     6  
     7  import (
     8  	"net/http"
     9  
    10  	auth_model "code.gitea.io/gitea/models/auth"
    11  	"code.gitea.io/gitea/models/db"
    12  	"code.gitea.io/gitea/modules/base"
    13  	"code.gitea.io/gitea/modules/setting"
    14  	"code.gitea.io/gitea/modules/web"
    15  	"code.gitea.io/gitea/services/context"
    16  	"code.gitea.io/gitea/services/forms"
    17  )
    18  
    19  const (
    20  	tplSettingsApplications base.TplName = "user/settings/applications"
    21  )
    22  
    23  // Applications render manage access token page
    24  func Applications(ctx *context.Context) {
    25  	ctx.Data["Title"] = ctx.Tr("settings.applications")
    26  	ctx.Data["PageIsSettingsApplications"] = true
    27  
    28  	loadApplicationsData(ctx)
    29  
    30  	ctx.HTML(http.StatusOK, tplSettingsApplications)
    31  }
    32  
    33  // ApplicationsPost response for add user's access token
    34  func ApplicationsPost(ctx *context.Context) {
    35  	form := web.GetForm(ctx).(*forms.NewAccessTokenForm)
    36  	ctx.Data["Title"] = ctx.Tr("settings")
    37  	ctx.Data["PageIsSettingsApplications"] = true
    38  
    39  	if ctx.HasError() {
    40  		loadApplicationsData(ctx)
    41  
    42  		ctx.HTML(http.StatusOK, tplSettingsApplications)
    43  		return
    44  	}
    45  
    46  	scope, err := form.GetScope()
    47  	if err != nil {
    48  		ctx.ServerError("GetScope", err)
    49  		return
    50  	}
    51  	t := &auth_model.AccessToken{
    52  		UID:   ctx.Doer.ID,
    53  		Name:  form.Name,
    54  		Scope: scope,
    55  	}
    56  
    57  	exist, err := auth_model.AccessTokenByNameExists(ctx, t)
    58  	if err != nil {
    59  		ctx.ServerError("AccessTokenByNameExists", err)
    60  		return
    61  	}
    62  	if exist {
    63  		ctx.Flash.Error(ctx.Tr("settings.generate_token_name_duplicate", t.Name))
    64  		ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
    65  		return
    66  	}
    67  
    68  	if err := auth_model.NewAccessToken(ctx, t); err != nil {
    69  		ctx.ServerError("NewAccessToken", err)
    70  		return
    71  	}
    72  
    73  	ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
    74  	ctx.Flash.Info(t.Token)
    75  
    76  	ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
    77  }
    78  
    79  // DeleteApplication response for delete user access token
    80  func DeleteApplication(ctx *context.Context) {
    81  	if err := auth_model.DeleteAccessTokenByID(ctx, ctx.FormInt64("id"), ctx.Doer.ID); err != nil {
    82  		ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
    83  	} else {
    84  		ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
    85  	}
    86  
    87  	ctx.JSONRedirect(setting.AppSubURL + "/user/settings/applications")
    88  }
    89  
    90  func loadApplicationsData(ctx *context.Context) {
    91  	ctx.Data["AccessTokenScopePublicOnly"] = auth_model.AccessTokenScopePublicOnly
    92  	tokens, err := db.Find[auth_model.AccessToken](ctx, auth_model.ListAccessTokensOptions{UserID: ctx.Doer.ID})
    93  	if err != nil {
    94  		ctx.ServerError("ListAccessTokens", err)
    95  		return
    96  	}
    97  	ctx.Data["Tokens"] = tokens
    98  	ctx.Data["EnableOAuth2"] = setting.OAuth2.Enabled
    99  	ctx.Data["IsAdmin"] = ctx.Doer.IsAdmin
   100  	if setting.OAuth2.Enabled {
   101  		ctx.Data["Applications"], err = db.Find[auth_model.OAuth2Application](ctx, auth_model.FindOAuth2ApplicationsOptions{
   102  			OwnerID: ctx.Doer.ID,
   103  		})
   104  		if err != nil {
   105  			ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
   106  			return
   107  		}
   108  		ctx.Data["Grants"], err = auth_model.GetOAuth2GrantsByUserID(ctx, ctx.Doer.ID)
   109  		if err != nil {
   110  			ctx.ServerError("GetOAuth2GrantsByUserID", err)
   111  			return
   112  		}
   113  	}
   114  }