Skip to content

Commit

Permalink
Merge branch 'main' into git.GetTags-paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 authored Sep 10, 2021
2 parents f7c079b + 9ca0e79 commit a5bbe02
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 26 deletions.
5 changes: 5 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ var (
Value: "",
Usage: "Custom icon URL for OAuth2 login source",
},
cli.BoolFlag{
Name: "skip-local-2fa",
Usage: "Set to true to skip local 2fa for users authenticated by this source",
},
}

microcmdAuthUpdateOauth = cli.Command{
Expand Down Expand Up @@ -616,6 +620,7 @@ func parseOAuth2Config(c *cli.Context) *oauth2.Source {
OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
CustomURLMapping: customURLMapping,
IconURL: c.String("icon-url"),
SkipLocalTwoFA: c.Bool("skip-local-2fa"),
}
}

Expand Down
33 changes: 28 additions & 5 deletions modules/convert/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
package convert

import (
"fmt"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
)

Expand All @@ -25,6 +28,9 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
if err := issue.LoadRepo(); err != nil {
return &api.Issue{}
}
if err := issue.Repo.GetOwner(); err != nil {
return &api.Issue{}
}

apiIssue := &api.Issue{
ID: issue.ID,
Expand All @@ -35,7 +41,7 @@ func ToAPIIssue(issue *models.Issue) *api.Issue {
Title: issue.Title,
Body: issue.Content,
Ref: issue.Ref,
Labels: ToLabelList(issue.Labels),
Labels: ToLabelList(issue.Labels, issue.Repo, issue.Repo.Owner),
State: issue.State(),
IsLocked: issue.IsLocked,
Comments: issue.NumComments,
Expand Down Expand Up @@ -168,20 +174,37 @@ func ToTrackedTimeList(tl models.TrackedTimeList) api.TrackedTimeList {
}

// ToLabel converts Label to API format
func ToLabel(label *models.Label) *api.Label {
return &api.Label{
func ToLabel(label *models.Label, repo *models.Repository, org *models.User) *api.Label {
result := &api.Label{
ID: label.ID,
Name: label.Name,
Color: strings.TrimLeft(label.Color, "#"),
Description: label.Description,
}

// calculate URL
if label.BelongsToRepo() && repo != nil {
if repo != nil {
result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID)
} else {
log.Error("ToLabel did not get repo to calculate url for label with id '%d'", label.ID)
}
} else { // BelongsToOrg
if org != nil {
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, org.Name, label.ID)
} else {
log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID)
}
}

return result
}

// ToLabelList converts list of Label to API format
func ToLabelList(labels []*models.Label) []*api.Label {
func ToLabelList(labels []*models.Label, repo *models.Repository, org *models.User) []*api.Label {
result := make([]*api.Label, len(labels))
for i := range labels {
result[i] = ToLabel(labels[i])
result[i] = ToLabel(labels[i], repo, org)
}
return result
}
Expand Down
6 changes: 5 additions & 1 deletion modules/convert/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package convert

import (
"fmt"
"testing"
"time"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"

Expand All @@ -18,11 +20,13 @@ import (
func TestLabel_ToLabel(t *testing.T) {
assert.NoError(t, models.PrepareTestDatabase())
label := models.AssertExistsAndLoadBean(t, &models.Label{ID: 1}).(*models.Label)
repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: label.RepoID}).(*models.Repository)
assert.Equal(t, &api.Label{
ID: label.ID,
Name: label.Name,
Color: "abcdef",
}, ToLabel(label))
URL: fmt.Sprintf("%sapi/v1/repos/user2/repo1/labels/%d", setting.AppURL, label.ID),
}, ToLabel(label, repo, nil))
}

func TestMilestone_APIFormat(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2456,6 +2456,8 @@ auths.oauth2_tokenURL = Token URL
auths.oauth2_authURL = Authorize URL
auths.oauth2_profileURL = Profile URL
auths.oauth2_emailURL = Email URL
auths.skip_local_two_fa = Skip local 2FA
auths.skip_local_two_fa_helper = Leaving unset means local users with 2FA set will still have to pass 2FA to log on
auths.oauth2_tenant = Tenant
auths.enable_auto_register = Enable Auto Registration
auths.sspi_auto_create_users = Automatically create users
Expand Down
10 changes: 6 additions & 4 deletions routers/api/v1/org/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ListLabels(ctx *context.APIContext) {
}

ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, ctx.Org.Organization))
}

// CreateLabel create a label for a repository
Expand Down Expand Up @@ -103,7 +103,8 @@ func CreateLabel(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "NewLabel", err)
return
}
ctx.JSON(http.StatusCreated, convert.ToLabel(label))

ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, ctx.Org.Organization))
}

// GetLabel get label by organization and label id
Expand Down Expand Up @@ -148,7 +149,7 @@ func GetLabel(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToLabel(label))
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization))
}

// EditLabel modify a label for an Organization
Expand Down Expand Up @@ -212,7 +213,8 @@ func EditLabel(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
return
}
ctx.JSON(http.StatusOK, convert.ToLabel(label))

ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization))
}

// DeleteLabel delete a label for an organization
Expand Down
6 changes: 3 additions & 3 deletions routers/api/v1/repo/issue_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ListIssueLabels(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels))
ctx.JSON(http.StatusOK, convert.ToLabelList(issue.Labels, ctx.Repo.Repository, ctx.Repo.Owner))
}

// AddIssueLabels add labels for an issue
Expand Down Expand Up @@ -117,7 +117,7 @@ func AddIssueLabels(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, ctx.Repo.Owner))
}

// DeleteIssueLabel delete a label for an issue
Expand Down Expand Up @@ -243,7 +243,7 @@ func ReplaceIssueLabels(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, ctx.Repo.Owner))
}

// ClearIssueLabels delete all the labels for an issue
Expand Down
10 changes: 6 additions & 4 deletions routers/api/v1/repo/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func ListLabels(ctx *context.APIContext) {
}

ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, convert.ToLabelList(labels))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, ctx.Repo.Repository, nil))
}

// GetLabel get label by repository and label id
Expand Down Expand Up @@ -112,7 +112,7 @@ func GetLabel(ctx *context.APIContext) {
return
}

ctx.JSON(http.StatusOK, convert.ToLabel(label))
ctx.JSON(http.StatusOK, convert.ToLabel(label, ctx.Repo.Repository, nil))
}

// CreateLabel create a label for a repository
Expand Down Expand Up @@ -165,7 +165,8 @@ func CreateLabel(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "NewLabel", err)
return
}
ctx.JSON(http.StatusCreated, convert.ToLabel(label))

ctx.JSON(http.StatusCreated, convert.ToLabel(label, ctx.Repo.Repository, nil))
}

// EditLabel modify a label for a repository
Expand Down Expand Up @@ -235,7 +236,8 @@ func EditLabel(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "UpdateLabel", err)
return
}
ctx.JSON(http.StatusOK, convert.ToLabel(label))

ctx.JSON(http.StatusOK, convert.ToLabel(label, ctx.Repo.Repository, nil))
}

// DeleteLabel delete a label for a repository
Expand Down
1 change: 1 addition & 0 deletions routers/web/admin/auths.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
OpenIDConnectAutoDiscoveryURL: form.OpenIDConnectAutoDiscoveryURL,
CustomURLMapping: customURLMapping,
IconURL: form.Oauth2IconURL,
SkipLocalTwoFA: form.SkipLocalTwoFA,
}
}

Expand Down
20 changes: 12 additions & 8 deletions routers/web/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ func SignInOAuth(ctx *context.Context) {
user, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req, ctx.Resp)
if err == nil && user != nil {
// we got the user without going through the whole OAuth2 authentication flow again
handleOAuth2SignIn(ctx, user, gothUser)
handleOAuth2SignIn(ctx, loginSource, user, gothUser)
return
}

Expand Down Expand Up @@ -660,7 +660,7 @@ func SignInOAuthCallback(ctx *context.Context) {
}
}

handleOAuth2SignIn(ctx, u, gothUser)
handleOAuth2SignIn(ctx, loginSource, u, gothUser)
}

func getUserName(gothUser *goth.User) string {
Expand Down Expand Up @@ -702,18 +702,22 @@ func updateAvatarIfNeed(url string, u *models.User) {
}
}

func handleOAuth2SignIn(ctx *context.Context, u *models.User, gothUser goth.User) {
func handleOAuth2SignIn(ctx *context.Context, source *models.LoginSource, u *models.User, gothUser goth.User) {
updateAvatarIfNeed(gothUser.AvatarURL, u)

// If this user is enrolled in 2FA, we can't sign the user in just yet.
// Instead, redirect them to the 2FA authentication page.
_, err := models.GetTwoFactorByUID(u.ID)
if err != nil {
if !models.IsErrTwoFactorNotEnrolled(err) {
needs2FA := false
if !source.Cfg.(*oauth2.Source).SkipLocalTwoFA {
_, err := models.GetTwoFactorByUID(u.ID)
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
ctx.ServerError("UserSignIn", err)
return
}
needs2FA = err == nil
}

// If this user is enrolled in 2FA and this source doesn't override it,
// we can't sign the user in just yet. Instead, redirect them to the 2FA authentication page.
if !needs2FA {
if err := ctx.Session.Set("uid", u.ID); err != nil {
log.Error("Error setting uid in session: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions services/auth/source/oauth2/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Source struct {
OpenIDConnectAutoDiscoveryURL string
CustomURLMapping *CustomURLMapping
IconURL string
SkipLocalTwoFA bool

// reference to the loginSource
loginSource *models.LoginSource
Expand Down
1 change: 1 addition & 0 deletions services/forms/auth_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type AuthenticationForm struct {
Oauth2EmailURL string
Oauth2IconURL string
Oauth2Tenant string
SkipLocalTwoFA bool
SSPIAutoCreateUsers bool
SSPIAutoActivateUsers bool
SSPIStripDomainNames bool
Expand Down
7 changes: 7 additions & 0 deletions templates/admin/auth/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@
<label for="open_id_connect_auto_discovery_url">{{.i18n.Tr "admin.auths.openIdConnectAutoDiscoveryURL"}}</label>
<input id="open_id_connect_auto_discovery_url" name="open_id_connect_auto_discovery_url" value="{{$cfg.OpenIDConnectAutoDiscoveryURL}}">
</div>
<div class="optional field">
<div class="ui checkbox">
<label for="skip_local_two_fa"><strong>{{.i18n.Tr "admin.auths.skip_local_two_fa"}}</strong></label>
<input id="skip_local_two_fa" name="skip_local_two_fa" type="checkbox" {{if $cfg.SkipLocalTwoFA}}checked{{end}}>
<p class="help">{{.i18n.Tr "admin.auths.skip_local_two_fa_helper"}}</p>
</div>
</div>

<div class="oauth2_use_custom_url inline field">
<div class="ui checkbox">
Expand Down
7 changes: 7 additions & 0 deletions templates/admin/auth/source/oauth.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
<label for="open_id_connect_auto_discovery_url">{{.i18n.Tr "admin.auths.openIdConnectAutoDiscoveryURL"}}</label>
<input id="open_id_connect_auto_discovery_url" name="open_id_connect_auto_discovery_url" value="{{.open_id_connect_auto_discovery_url}}">
</div>
<div class="optional field">
<div class="ui checkbox">
<label for="skip_local_two_fa"><strong>{{.i18n.Tr "admin.auths.skip_local_two_fa"}}</strong></label>
<input id="skip_local_two_fa" name="skip_local_two_fa" type="checkbox" {{if .skip_local_two_fa}}checked{{end}}>
<p class="help">{{.i18n.Tr "admin.auths.skip_local_two_fa_helper"}}</p>
</div>
</div>

<div class="oauth2_use_custom_url inline field">
<div class="ui checkbox">
Expand Down
3 changes: 2 additions & 1 deletion templates/repo/projects/view.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
<div class="page-content repository">
{{template "repo/header" .}}
<div class="ui container">
<div class="ui three column stackable grid">
<div class="ui two column stackable grid">
<div class="column">
{{template "repo/issue/navbar" .}}
</div>
<div class="column right aligned">
{{if and .CanWriteProjects (not .Repository.IsArchived) .PageIsProjects}}
<a class="ui green button show-modal item" href="{{$.RepoLink}}/issues/new?project={{$.Project.ID}}">{{.i18n.Tr "repo.issues.new"}}</a>
<a class="ui green button show-modal item" data-modal="#new-board-item">{{.i18n.Tr "new_project_board"}}</a>
{{end}}
<div class="ui small modal" id="new-board-item">
Expand Down

0 comments on commit a5bbe02

Please sign in to comment.