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