Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to restrict allowed user visibility modes #16271

Merged
2 changes: 1 addition & 1 deletion models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ func checkDupEmail(e Engine, u *User) error {

// validateUser check if user is valide to insert / update into database
func validateUser(u *User) error {
if !setting.Service.AllowedUserVisibilityModesMap[u.Visibility] {
if !setting.Service.AllowedUserVisibilityModesSlice.IsAllowedVisibility(u.Visibility) {
return fmt.Errorf("visibility Mode not allowed: %s", u.Visibility.String())
}

Expand Down
4 changes: 1 addition & 3 deletions models/user_mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"

"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -168,8 +167,7 @@ func TestMakeEmailPrimary(t *testing.T) {

func TestActivate(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
setting.Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
setting.Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}

email := &EmailAddress{
ID: int64(1),
Expand Down
15 changes: 5 additions & 10 deletions models/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ func TestDeleteUser(t *testing.T) {

func TestEmailNotificationPreferences(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
setting.Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
setting.Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}

for _, test := range []struct {
expected string
Expand Down Expand Up @@ -312,8 +311,7 @@ func TestDisplayName(t *testing.T) {
}

func TestCreateUser(t *testing.T) {
setting.Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
setting.Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}

user := &User{
Name: "GiteaBot",
Expand All @@ -330,8 +328,7 @@ func TestCreateUser(t *testing.T) {
}

func TestCreateUserInvalidEmail(t *testing.T) {
setting.Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
setting.Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}

user := &User{
Name: "GiteaBot",
Expand All @@ -348,8 +345,7 @@ func TestCreateUserInvalidEmail(t *testing.T) {
}

func TestCreateUser_Issue5882(t *testing.T) {
setting.Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
setting.Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}

// Init settings
_ = setting.Admin
Expand Down Expand Up @@ -490,8 +486,7 @@ func TestUpdateUser(t *testing.T) {
user = AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
assert.True(t, user.KeepActivityPrivate)

setting.Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
setting.Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, false, false}
user.KeepActivityPrivate = false
user.Visibility = structs.VisibleTypePrivate
assert.Error(t, UpdateUser(user))
Expand Down
27 changes: 18 additions & 9 deletions modules/setting/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var Service struct {
DefaultUserVisibility string
DefaultUserVisibilityMode structs.VisibleType
AllowedUserVisibilityModes []string
AllowedUserVisibilityModesMap map[structs.VisibleType]bool `ini:"-"`
AllowedUserVisibilityModesSlice AllowedVisibility `ini:"-"`
DefaultOrgVisibility string
DefaultOrgVisibilityMode structs.VisibleType
ActiveCodeLives int
Expand Down Expand Up @@ -75,6 +75,16 @@ var Service struct {
} `ini:"service.explore"`
}
6543 marked this conversation as resolved.
Show resolved Hide resolved

// AllowedVisibility store in a 3 item bool array what is allowed
type AllowedVisibility []bool

func (a AllowedVisibility) IsAllowedVisibility(t structs.VisibleType) bool {
if int(t) >= len(a) {
return false
}
return a[t]
}

func newService() {
sec := Cfg.Section("service")
Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
Expand Down Expand Up @@ -125,14 +135,13 @@ func newService() {
Service.DefaultUserVisibility = sec.Key("DEFAULT_USER_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
Service.DefaultUserVisibilityMode = structs.VisibilityModes[Service.DefaultUserVisibility]
Service.AllowedUserVisibilityModes = sec.Key("ALLOWED_USER_VISIBILITY_MODES").Strings(",")
Service.AllowedUserVisibilityModesMap = make(map[structs.VisibleType]bool)
for _, modes := range Service.AllowedUserVisibilityModes {
Service.AllowedUserVisibilityModesMap[structs.VisibilityModes[modes]] = true
}
if len(Service.AllowedUserVisibilityModesMap) == 0 {
Service.AllowedUserVisibilityModesMap[structs.VisibleTypePublic] = true
Service.AllowedUserVisibilityModesMap[structs.VisibleTypeLimited] = true
Service.AllowedUserVisibilityModesMap[structs.VisibleTypePrivate] = true
if len(Service.AllowedUserVisibilityModes) == 0 {
Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}
} else {
Service.AllowedUserVisibilityModesSlice = []bool{false, false, false}
for _, sMode := range Service.AllowedUserVisibilityModes {
Service.AllowedUserVisibilityModesSlice[structs.VisibilityModes[sMode]] = true
}
}
Service.DefaultOrgVisibility = sec.Key("DEFAULT_ORG_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility]
Expand Down
17 changes: 15 additions & 2 deletions routers/web/admin/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/password"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/web/explore"
router_user_setting "code.gitea.io/gitea/routers/web/user/setting"
Expand Down Expand Up @@ -52,7 +53,13 @@ func NewUser(ctx *context.Context) {
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminUsers"] = true
ctx.Data["DefaultUserVisibilityMode"] = setting.Service.DefaultUserVisibilityMode
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesMap
var allowedUserVisibilityModes []structs.VisibleType
for i, v := range setting.Service.AllowedUserVisibilityModesSlice {
if v {
allowedUserVisibilityModes = append(allowedUserVisibilityModes, structs.VisibleType(i))
}
}
ctx.Data["AllowedUserVisibilityModes"] = allowedUserVisibilityModes

ctx.Data["login_type"] = "0-0"

Expand Down Expand Up @@ -212,7 +219,13 @@ func EditUser(ctx *context.Context) {
ctx.Data["PageIsAdminUsers"] = true
ctx.Data["DisableRegularOrgCreation"] = setting.Admin.DisableRegularOrgCreation
ctx.Data["DisableMigrations"] = setting.Repository.DisableMigrations
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesMap
var allowedUserVisibilityModes []structs.VisibleType
for i, v := range setting.Service.AllowedUserVisibilityModesSlice {
if v {
allowedUserVisibilityModes = append(allowedUserVisibilityModes, structs.VisibleType(i))
}
}
ctx.Data["AllowedUserVisibilityModes"] = allowedUserVisibilityModes

prepareUserInfo(ctx)
if ctx.Written() {
Expand Down
2 changes: 2 additions & 0 deletions routers/web/user/setting/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
)

func TestChangePassword(t *testing.T) {
setting.Service.AllowedUserVisibilityModesSlice = []bool{true, true, true}

oldPassword := "password"
setting.MinPasswordLength = 6
var pcALL = []string{"lower", "upper", "digit", "spec"}
Expand Down
9 changes: 8 additions & 1 deletion routers/web/user/setting/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/typesniffer"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
Expand All @@ -38,7 +39,13 @@ const (
func Profile(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsProfile"] = true
ctx.Data["AllowedUserVisibilityModes"] = setting.Service.AllowedUserVisibilityModesMap
var allowedUserVisibilityModes []structs.VisibleType
for i, v := range setting.Service.AllowedUserVisibilityModesSlice {
if v {
allowedUserVisibilityModes = append(allowedUserVisibilityModes, structs.VisibleType(i))
}
}
ctx.Data["AllowedUserVisibilityModes"] = allowedUserVisibilityModes

ctx.HTML(http.StatusOK, tplSettingsProfile)
}
Expand Down
8 changes: 4 additions & 4 deletions templates/admin/user/edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
</div>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
{{range $key, $value := .AllowedUserVisibilityModes}}
{{if $key.IsPublic}}
{{range $mode := .AllowedUserVisibilityModes}}
{{if $mode.IsPublic}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.public_tooltip"}}" data-value="0">{{$.i18n.Tr "settings.visibility.public"}}</div>
{{else if $key.IsLimited}}
{{else if $mode.IsLimited}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.limited_tooltip"}}" data-value="1">{{$.i18n.Tr "settings.visibility.limited"}}</div>
{{else if $key.IsPrivate}}
{{else if $mode.IsPrivate}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.private_tooltip"}}" data-value="2">{{$.i18n.Tr "settings.visibility.private"}}</div>
{{end}}
{{end}}
Expand Down
8 changes: 4 additions & 4 deletions templates/admin/user/new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
</div>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
{{range $key, $value := .AllowedUserVisibilityModes}}
{{if $key.IsPublic}}
{{range $mode := .AllowedUserVisibilityModes}}
{{if $mode.IsPublic}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.public_tooltip"}}" data-value="0">{{$.i18n.Tr "settings.visibility.public"}}</div>
{{else if $key.IsLimited}}
{{else if $mode.IsLimited}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.limited_tooltip"}}" data-value="1">{{$.i18n.Tr "settings.visibility.limited"}}</div>
{{else if $key.IsPrivate}}
{{else if $mode.IsPrivate}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.private_tooltip"}}" data-value="2">{{$.i18n.Tr "settings.visibility.private"}}</div>
{{end}}
{{end}}
Expand Down
8 changes: 4 additions & 4 deletions templates/user/settings/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
</div>
{{svg "octicon-triangle-down" 14 "dropdown icon"}}
<div class="menu">
{{range $key, $value := .AllowedUserVisibilityModes}}
{{if $key.IsPublic}}
{{range $mode := .AllowedUserVisibilityModes}}
{{if $mode.IsPublic}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.public_tooltip"}}" data-value="0">{{$.i18n.Tr "settings.visibility.public"}}</div>
{{else if $key.IsLimited}}
{{else if $mode.IsLimited}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.limited_tooltip"}}" data-value="1">{{$.i18n.Tr "settings.visibility.limited"}}</div>
{{else if $key.IsPrivate}}
{{else if $mode.IsPrivate}}
<div class="item poping up" data-content="{{$.i18n.Tr "settings.visibility.private_tooltip"}}" data-value="2">{{$.i18n.Tr "settings.visibility.private"}}</div>
{{end}}
{{end}}
Expand Down