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

Allow to set organization visibility (public, internal, private) #1763

Merged
merged 167 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
167 commits
Select commit Hold shift + click to select a range
c2a75b3
Add UI visibility in organization settings
DblK May 19, 2017
4df763c
Set Visibility Public by default when creating new organization
DblK May 19, 2017
9d73ff4
Implement visibility in explore/organizations
DblK May 21, 2017
7bdc6e8
Implement visibility in explore/repos
DblK May 21, 2017
fb0c45a
Implement global configuration for new organizations
DblK May 22, 2017
90ec030
Add in admin/repos and admin/org gold lock icons for private organiza…
DblK May 22, 2017
8eaddfc
Handle display of repositories from explore/repo and when viewing an …
DblK May 22, 2017
20e63bb
Add lock icon for private org on explore/repo
DblK May 23, 2017
15d9e00
User profile display organization based on Membership Visibility and …
DblK May 24, 2017
49e4008
Resolve Conflict for merging
DblK May 24, 2017
fcf8516
Merge remote-tracking branch 'upstream/master' into feature_AllowPriv…
techknowlogick Jun 8, 2018
86d1e78
Fix build errors
techknowlogick Jun 8, 2018
1d74ce3
make fmt
techknowlogick Jun 8, 2018
79131fc
Update string per @BKC's suggestion
techknowlogick Jun 8, 2018
6d87477
Use UpdatedUnix instead of just Updated
techknowlogick Jun 8, 2018
9c797ba
use TimeSinceUnix instead of TimeSince
techknowlogick Jun 8, 2018
79401e3
add migration
techknowlogick Jun 8, 2018
df88dbf
Merge branch 'feature_AllowPrivateOrganization' of github.com:DblK/gi…
techknowlogick Jun 8, 2018
b1a19ef
fix migration
techknowlogick Jun 8, 2018
af87b29
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jun 8, 2018
c850416
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jun 9, 2018
059dba5
update date
techknowlogick Jun 9, 2018
8a36cf2
update with suggested code
techknowlogick Jun 11, 2018
c0fa0de
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jun 11, 2018
819085a
prepare for conflict resolution
techknowlogick Jun 29, 2018
917cf76
Merge remote-tracking branch 'upstream/master' into feature_AllowPriv…
techknowlogick Jun 29, 2018
0b0b603
hide org profile page if needed when directly accessed
techknowlogick Jun 29, 2018
6db8162
hide repo if hidden
techknowlogick Jun 29, 2018
d4c6367
Chose visibility when creating org
techknowlogick Jun 29, 2018
7ee585f
fix build error
techknowlogick Jun 29, 2018
87f16f9
add copyright
techknowlogick Jun 29, 2018
f884657
add copyright
techknowlogick Jun 29, 2018
38556b9
update copyright
techknowlogick Jun 29, 2018
e46bd24
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jul 13, 2018
0e546f1
Merge remote-tracking branch 'upstream/master' into feature_AllowPriv…
techknowlogick Jul 14, 2018
250f858
add checks to api & some tests
techknowlogick Jul 14, 2018
a60f4be
make fmt
techknowlogick Jul 14, 2018
502fed5
add copyright
techknowlogick Jul 14, 2018
4a1e0bd
add copyright
techknowlogick Jul 14, 2018
28fc8f0
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jul 14, 2018
36d9124
rename migration
techknowlogick Jul 21, 2018
5a69b47
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jul 21, 2018
5df42b4
make fmt
techknowlogick Jul 21, 2018
285b47b
Rename v71.go to v77.go
techknowlogick Jan 4, 2019
e3a4a08
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 4, 2019
d434cec
make fmt fix
techknowlogick Jan 4, 2019
06a7d56
Update user.go
techknowlogick Jan 4, 2019
8f4f59d
resolve @bkcsoft feedback
techknowlogick Jan 4, 2019
24d2e48
Update org.go
techknowlogick Jan 4, 2019
0ed54b6
step before merge master
techknowlogick Jan 22, 2019
f8977ad
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 22, 2019
20d33dd
make fmt
techknowlogick Jan 22, 2019
e9a1664
add sql queries
techknowlogick Jan 22, 2019
25dcc39
Merge branch 'feature_AllowPrivateOrganization' of github.com:DblK/gi…
techknowlogick Jan 22, 2019
c424cc3
remove TODO
techknowlogick Jan 22, 2019
1538c29
Fix Tests
techknowlogick Jan 22, 2019
878b887
accept post field on create
techknowlogick Jan 22, 2019
b5faeaf
add func per BKC feedback
techknowlogick Jan 22, 2019
cda121f
add comment to func
techknowlogick Jan 22, 2019
1e237d4
update per BKC feedback
techknowlogick Jan 22, 2019
15f4c95
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 23, 2019
6f4da0b
update year
techknowlogick Jan 23, 2019
bc208a7
update year
techknowlogick Jan 23, 2019
9df3bf0
update year
techknowlogick Jan 23, 2019
16f4012
update year
techknowlogick Jan 23, 2019
b7f88b2
add copyright
techknowlogick Jan 23, 2019
51a10cd
update year
techknowlogick Jan 23, 2019
7c04589
update year
techknowlogick Jan 23, 2019
99bad71
Update year
techknowlogick Jan 23, 2019
a085b47
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 28, 2019
fd24c9f
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 28, 2019
f9122f3
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 30, 2019
829e9f2
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Jan 31, 2019
70471ab
Update app.ini.sample
techknowlogick Jan 31, 2019
f32baca
Update setting.go
techknowlogick Jan 31, 2019
d6241c8
Add docs
techknowlogick Jan 31, 2019
b66b8f0
Update org.go
techknowlogick Jan 31, 2019
1d48754
Update user.go
techknowlogick Jan 31, 2019
8be87bc
Update modules/setting/setting.go
techknowlogick Jan 31, 2019
a100940
Update to use constants
techknowlogick Jan 31, 2019
8ea71f6
remove redundant conditional
techknowlogick Jan 31, 2019
a68ecf9
split up func for has visibility
techknowlogick Feb 1, 2019
17e3a38
combine conditional
techknowlogick Feb 1, 2019
cf3b71b
refactor structs into new package
techknowlogick Feb 1, 2019
fd51d42
fix syntax
techknowlogick Feb 1, 2019
eed990a
export struct
techknowlogick Feb 1, 2019
8826686
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 1, 2019
3a3a1cb
fix query & template - still more todo
techknowlogick Feb 1, 2019
0d1cd97
Merge branch 'feature_AllowPrivateOrganization' of github.com:DblK/gi…
techknowlogick Feb 1, 2019
4ed648e
reset before sql changes
techknowlogick Feb 1, 2019
0483344
try to fix mssql
techknowlogick Feb 1, 2019
e67cbd0
repo SQL
techknowlogick Feb 2, 2019
9cd145c
remove TODO comment
techknowlogick Feb 2, 2019
0159beb
make fmt
techknowlogick Feb 2, 2019
8d26cb6
fix spacing
techknowlogick Feb 2, 2019
45b706c
more spacing
techknowlogick Feb 2, 2019
14e6d04
try different sql join
techknowlogick Feb 2, 2019
5e02b7b
make fmt
techknowlogick Feb 2, 2019
30bdaa0
correct capitalization
techknowlogick Feb 2, 2019
2ffc296
resolve postgresql errors
techknowlogick Feb 2, 2019
7f63414
more psql fixes
techknowlogick Feb 2, 2019
78ed0a7
SQL
techknowlogick Feb 2, 2019
97802f2
psql
techknowlogick Feb 2, 2019
ac9aad0
fix mysql
techknowlogick Feb 2, 2019
373e1f5
work for all DB types
techknowlogick Feb 2, 2019
da64f72
work on all DBs hopefully
techknowlogick Feb 2, 2019
64af3a6
mssql
techknowlogick Feb 2, 2019
d22dff8
mssql
techknowlogick Feb 2, 2019
55cba7c
Update config-cheat-sheet.en-us.md
techknowlogick Feb 2, 2019
655c772
Update user.go
techknowlogick Feb 2, 2019
3a5d9ad
Update structs.go
techknowlogick Feb 2, 2019
3e236c5
Delete v79.go
techknowlogick Feb 2, 2019
148039e
Update migrations.go
techknowlogick Feb 2, 2019
996ee26
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 2, 2019
fc19628
Update user.go
techknowlogick Feb 2, 2019
35c4175
Update user.go
techknowlogick Feb 2, 2019
907a5ba
update per feedback
techknowlogick Feb 2, 2019
583a400
fix comment
techknowlogick Feb 2, 2019
c8cbf1f
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 3, 2019
b4b6871
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 4, 2019
a10dfae
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 4, 2019
4fce179
Update org.go
techknowlogick Feb 4, 2019
c8b844f
Update repo.go
techknowlogick Feb 4, 2019
7f05374
Update view.go
techknowlogick Feb 4, 2019
f94e040
Update home.go
techknowlogick Feb 4, 2019
7082b4a
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 4, 2019
b1f7a5d
Update list.tmpl
techknowlogick Feb 4, 2019
ec4b110
Update profile.tmpl
techknowlogick Feb 4, 2019
54ddf81
Update options.tmpl
techknowlogick Feb 4, 2019
caf2acf
Update create.tmpl
techknowlogick Feb 4, 2019
881db82
Update repo_list.tmpl
techknowlogick Feb 4, 2019
87d8463
Update create.tmpl
techknowlogick Feb 4, 2019
f33a083
Update organizations.tmpl
techknowlogick Feb 4, 2019
5b18778
Update list.tmpl
techknowlogick Feb 4, 2019
be4f405
Update options.tmpl
techknowlogick Feb 4, 2019
39e8464
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 5, 2019
9462c2d
Update structs.go
techknowlogick Feb 6, 2019
46c75f3
move structs to structs package
techknowlogick Feb 6, 2019
3fcebe6
Update repo_list.go
techknowlogick Feb 6, 2019
88d250a
Update org.go
techknowlogick Feb 6, 2019
1afc055
update per @lunny feedback
techknowlogick Feb 6, 2019
dfc6679
split up lines
techknowlogick Feb 6, 2019
ec77786
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 6, 2019
0f99946
fix build
techknowlogick Feb 6, 2019
f6a85ea
Merge branch 'feature_AllowPrivateOrganization' of github.com:DblK/gi…
techknowlogick Feb 6, 2019
c8e22a8
fix build issues
techknowlogick Feb 6, 2019
3423b2e
Update create.tmpl
techknowlogick Feb 6, 2019
3994a5a
Update repo_list.tmpl
techknowlogick Feb 6, 2019
bd0add3
update templates
techknowlogick Feb 6, 2019
955ff90
update templates
techknowlogick Feb 6, 2019
eb4e19d
rename DefaultVisibility to DefaultOrgVisibility
techknowlogick Feb 6, 2019
2c19f68
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 6, 2019
7a03024
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 7, 2019
91db977
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 7, 2019
3ff683e
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 9, 2019
69e9bad
Merge remote-tracking branch 'upstream/master' into feature_AllowPriv…
techknowlogick Feb 10, 2019
fae9107
fix build
techknowlogick Feb 10, 2019
58e5585
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 10, 2019
bda7c8d
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 11, 2019
7cae78f
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 11, 2019
c9595fc
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 13, 2019
9db666c
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 13, 2019
c165437
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 14, 2019
dee33fc
Merge branch 'master' into feature_AllowPrivateOrganization
techknowlogick Feb 14, 2019
958530a
Merge branch 'master' into feature_AllowPrivateOrganization
lunny Feb 17, 2019
b7c26e1
Merge branch 'master' into feature_AllowPrivateOrganization
lunny Feb 18, 2019
e6891d9
Merge branch 'master' into feature_AllowPrivateOrganization
lafriks Feb 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions custom/conf/app.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ DEFAULT_KEEP_EMAIL_PRIVATE = false
; Default value for AllowCreateOrganization
; Every new user will have rights set to create organizations depending on this setting
DEFAULT_ALLOW_CREATE_ORGANIZATION = true
; Either "public", "limited" or "private", default is "public"
; Limited is for signed user only
; Private is only for member of the organization
; Public is for everyone
DEFAULT_ORG_VISIBILITY = public
; Default value for EnableDependencies
; Repositories will use dependencies by default depending on this setting
DEFAULT_ENABLE_DEPENDENCIES = true
Expand Down
9 changes: 5 additions & 4 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `PATH`: **data/gitea.db**: For SQLite3 only, the database file path.
- `LOG_SQL`: **true**: Log the executed SQL.
- `DB_RETRIES`: **10**: How many ORM init / DB connect attempts allowed.
- `DB_RETRY_BACKOFF`: **3s*: time.Duration to wait before trying another ORM init / DB connect attempt, if failure occured.
- `DB_RETRY_BACKOFF`: **3s**: time.Duration to wait before trying another ORM init / DB connect attempt, if failure occured.

## Indexer (`indexer`)

Expand Down Expand Up @@ -203,12 +203,13 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `CAPTCHA_TYPE`: **image**: \[image, recaptcha\]
- `RECAPTCHA_SECRET`: **""**: Go to https://www.google.com/recaptcha/admin to get a secret for recaptcha.
- `RECAPTCHA_SITEKEY`: **""**: Go to https://www.google.com/recaptcha/admin to get a sitekey for recaptcha.
- `DEFAULT_ENABLE_DEPENDENCIES`: **true** Enable this to have dependencies enabled by default.
- `ENABLE_USER_HEATMAP`: **true** Enable this to display the heatmap on users profiles.
- `DEFAULT_ENABLE_DEPENDENCIES`: **true**: Enable this to have dependencies enabled by default.
- `ENABLE_USER_HEATMAP`: **true**: Enable this to display the heatmap on users profiles.
- `EMAIL_DOMAIN_WHITELIST`: **\<empty\>**: If non-empty, list of domain names that can only be used to register
on this instance.
- `SHOW_REGISTRATION_BUTTON`: **! DISABLE\_REGISTRATION**: Show Registration Button
- `AUTO_WATCH_NEW_REPOS`: **true** Enable this to let all organisation users watch new repos when they are created
- `AUTO_WATCH_NEW_REPOS`: **true**: Enable this to let all organisation users watch new repos when they are created
- `DEFAULT_ORG_VISIBILITY`: **public**: Set default visibility mode for organisations, either "public", "limited" or "private".

## Webhook (`webhook`)

Expand Down
1 change: 1 addition & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2017 The Gitea Authors. All rights reserved.
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

Expand Down
36 changes: 36 additions & 0 deletions models/org.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 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.

Expand All @@ -11,6 +12,7 @@ import (
"strings"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs"

"github.com/Unknwon/com"
"github.com/go-xorm/builder"
Expand Down Expand Up @@ -366,6 +368,40 @@ func getOwnedOrgsByUserID(sess *xorm.Session, userID int64) ([]*User, error) {
Find(&orgs)
}

// HasOrgVisible tells if the given user can see the given org
func HasOrgVisible(org *User, user *User) bool {
// Not SignedUser
if user == nil {
if org.Visibility == structs.VisibleTypePublic {
return true
}
return false
}

if user.IsAdmin {
return true
}

if org.Visibility == structs.VisibleTypePrivate && !org.IsUserPartOfOrg(user.ID) {
return false
}
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
return true
}

// HasOrgsVisible tells if the given user can see at least one of the orgs provided
func HasOrgsVisible(orgs []*User, user *User) bool {
if len(orgs) == 0 {
return false
}

for _, org := range orgs {
if HasOrgVisible(org, user) {
return true
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false
}

// GetOwnedOrgsByUserID returns a list of organizations are owned by given user ID.
func GetOwnedOrgsByUserID(userID int64) ([]*User, error) {
sess := x.NewSession()
Expand Down
71 changes: 71 additions & 0 deletions models/org_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package models
import (
"testing"

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

"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -545,3 +547,72 @@ func TestAccessibleReposEnv_MirrorRepos(t *testing.T) {
testSuccess(2, []int64{5})
testSuccess(4, []int64{})
}

func TestHasOrgVisibleTypePublic(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)

const newOrgName = "test-org-public"
org := &User{
Name: newOrgName,
Visibility: structs.VisibleTypePublic,
}

AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
assert.NoError(t, CreateOrganization(org, owner))
org = AssertExistsAndLoadBean(t,
&User{Name: org.Name, Type: UserTypeOrganization}).(*User)
test1 := HasOrgVisible(org, owner)
test2 := HasOrgVisible(org, user3)
test3 := HasOrgVisible(org, nil)
assert.Equal(t, test1, true) // owner of org
assert.Equal(t, test2, true) // user not a part of org
assert.Equal(t, test3, true) // logged out user
}

func TestHasOrgVisibleTypeLimited(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)

const newOrgName = "test-org-limited"
org := &User{
Name: newOrgName,
Visibility: structs.VisibleTypeLimited,
}

AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
assert.NoError(t, CreateOrganization(org, owner))
org = AssertExistsAndLoadBean(t,
&User{Name: org.Name, Type: UserTypeOrganization}).(*User)
test1 := HasOrgVisible(org, owner)
test2 := HasOrgVisible(org, user3)
test3 := HasOrgVisible(org, nil)
assert.Equal(t, test1, true) // owner of org
assert.Equal(t, test2, true) // user not a part of org
assert.Equal(t, test3, false) // logged out user
}

func TestHasOrgVisibleTypePrivate(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
owner := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
user3 := AssertExistsAndLoadBean(t, &User{ID: 3}).(*User)

const newOrgName = "test-org-private"
org := &User{
Name: newOrgName,
Visibility: structs.VisibleTypePrivate,
}

AssertNotExistsBean(t, &User{Name: org.Name, Type: UserTypeOrganization})
assert.NoError(t, CreateOrganization(org, owner))
org = AssertExistsAndLoadBean(t,
&User{Name: org.Name, Type: UserTypeOrganization}).(*User)
test1 := HasOrgVisible(org, owner)
test2 := HasOrgVisible(org, user3)
test3 := HasOrgVisible(org, nil)
assert.Equal(t, test1, true) // owner of org
assert.Equal(t, test2, false) // user not a part of org
assert.Equal(t, test3, false) // logged out user
}
35 changes: 35 additions & 0 deletions models/repo_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"fmt"
"strings"

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

"github.com/go-xorm/builder"
"github.com/go-xorm/core"
)

// RepositoryListDefaultPageSize is the default number of repositories
Expand Down Expand Up @@ -171,6 +173,10 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err

if !opts.Private {
cond = cond.And(builder.Eq{"is_private": false})
accessCond := builder.Or(
builder.NotIn("owner_id", builder.Select("id").From("`user`").Where(builder.Or(builder.Eq{"visibility": structs.VisibleTypeLimited}, builder.Eq{"visibility": structs.VisibleTypePrivate}))),
builder.NotIn("owner_id", builder.Select("id").From("`user`").Where(builder.Eq{"type": UserTypeOrganization})))
cond = cond.And(accessCond)
}

if opts.OwnerID > 0 {
Expand All @@ -193,6 +199,35 @@ func SearchRepositoryByName(opts *SearchRepoOptions) (RepositoryList, int64, err
accessCond = accessCond.Or(collaborateCond)
}

var exprCond builder.Cond
if DbCfg.Type == core.POSTGRES {
exprCond = builder.Expr("org_user.org_id = \"user\".id")
} else if DbCfg.Type == core.MSSQL {
exprCond = builder.Expr("org_user.org_id = [user].id")
} else {
exprCond = builder.Eq{"org_user.org_id": "user.id"}
}

visibilityCond := builder.Or(
builder.In("owner_id",
builder.Select("org_id").From("org_user").
LeftJoin("`user`", exprCond).
Where(
builder.And(
builder.Eq{"uid": opts.OwnerID},
builder.Eq{"visibility": structs.VisibleTypePrivate})),
),
builder.In("owner_id",
builder.Select("id").From("`user`").
Where(
builder.Or(
builder.Eq{"visibility": structs.VisibleTypePublic},
builder.Eq{"visibility": structs.VisibleTypeLimited})),
),
builder.NotIn("owner_id", builder.Select("id").From("`user`").Where(builder.Eq{"type": UserTypeOrganization})),
)
cond = cond.And(visibilityCond)

if opts.AllPublic {
accessCond = accessCond.Or(builder.Eq{"is_private": false})
}
Expand Down
64 changes: 51 additions & 13 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ import (
"time"
"unicode/utf8"

"github.com/Unknwon/com"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
"github.com/nfnt/resize"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ssh"

"code.gitea.io/git"
api "code.gitea.io/sdk/gitea"

"code.gitea.io/gitea/modules/avatar"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/generate"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
api "code.gitea.io/sdk/gitea"

"github.com/Unknwon/com"
"github.com/go-xorm/builder"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"github.com/nfnt/resize"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ssh"
)

// UserType defines the user type
Expand Down Expand Up @@ -136,8 +137,9 @@ type User struct {
Description string
NumTeams int
NumMembers int
Teams []*Team `xorm:"-"`
Members []*User `xorm:"-"`
Teams []*Team `xorm:"-"`
Members []*User `xorm:"-"`
Visibility structs.VisibleType `xorm:"NOT NULL DEFAULT 0"`

// Preferences
DiffViewStyle string `xorm:"NOT NULL DEFAULT ''"`
Expand Down Expand Up @@ -526,6 +528,16 @@ func (u *User) IsUserOrgOwner(orgID int64) bool {
return isOwner
}

// IsUserPartOfOrg returns true if user with userID is part of the u organisation.
func (u *User) IsUserPartOfOrg(userID int64) bool {
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
isMember, err := IsOrganizationMember(u.ID, userID)
if err != nil {
log.Error(4, "IsOrganizationMember: %v", err)
return false
}
return isMember
}

// IsPublicMember returns true if user public his/her membership in given organization.
func (u *User) IsPublicMember(orgID int64) bool {
isMember, err := IsPublicMembership(orgID, u.ID)
Expand Down Expand Up @@ -1341,13 +1353,18 @@ type SearchUserOptions struct {
UID int64
OrderBy SearchOrderBy
Page int
PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum
Private bool // Include private orgs in search
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
OwnerID int64 // id of user for visibility calculation
PageSize int // Can be smaller than or equal to setting.UI.ExplorePagingNum
IsActive util.OptionalBool
SearchByEmail bool // Search by email as well as username/full name
}

func (opts *SearchUserOptions) toConds() builder.Cond {
var cond builder.Cond = builder.Eq{"type": opts.Type}

var cond = builder.NewCond()
cond = cond.And(builder.Eq{"type": opts.Type})

if len(opts.Keyword) > 0 {
lowerKeyword := strings.ToLower(opts.Keyword)
keywordCond := builder.Or(
Expand All @@ -1361,6 +1378,27 @@ func (opts *SearchUserOptions) toConds() builder.Cond {
cond = cond.And(keywordCond)
}

if !opts.Private {
// user not logged in and so they won't be allowed to see non-public orgs
cond = cond.And(builder.In("visibility", structs.VisibleTypePublic))
}

if opts.OwnerID > 0 {
var exprCond builder.Cond
if DbCfg.Type == core.MYSQL {
exprCond = builder.Expr("org_user.org_id = user.id")
} else if DbCfg.Type == core.MSSQL {
exprCond = builder.Expr("org_user.org_id = [user].id")
} else {
exprCond = builder.Expr("org_user.org_id = \"user\".id")
}
var accessCond = builder.NewCond()
techknowlogick marked this conversation as resolved.
Show resolved Hide resolved
accessCond = builder.Or(
builder.In("id", builder.Select("org_id").From("org_user").LeftJoin("`user`", exprCond).Where(builder.And(builder.Eq{"uid": opts.OwnerID}, builder.Eq{"visibility": structs.VisibleTypePrivate}))),
builder.In("visibility", structs.VisibleTypePublic, structs.VisibleTypeLimited))
cond = cond.And(accessCond)
}

if opts.UID > 0 {
cond = cond.And(builder.Eq{"id": opts.UID})
}
Expand Down
6 changes: 5 additions & 1 deletion modules/auth/org.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 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 auth

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/structs"

"github.com/go-macaron/binding"
"gopkg.in/macaron.v1"
Expand All @@ -20,7 +22,8 @@ import (

// CreateOrgForm form for creating organization
type CreateOrgForm struct {
OrgName string `binding:"Required;AlphaDashDot;MaxSize(35)" locale:"org.org_name_holder"`
OrgName string `binding:"Required;AlphaDashDot;MaxSize(35)" locale:"org.org_name_holder"`
Visibility structs.VisibleType
}

// Validate validates the fields
Expand All @@ -35,6 +38,7 @@ type UpdateOrgSettingForm struct {
Description string `binding:"MaxSize(255)"`
Website string `binding:"ValidUrl;MaxSize(255)"`
Location string `binding:"MaxSize(50)"`
Visibility structs.VisibleType
MaxRepoCreation int
}

Expand Down
Loading