0
0
Fork 0
mirror of https://github.com/go-gitea/gitea synced 2024-11-17 23:41:49 +01:00
This commit is contained in:
Unknwon 2015-11-13 17:37:02 -05:00
parent 1d57f0d64f
commit f8ae161c74
3 changed files with 29 additions and 19 deletions

View file

@ -67,9 +67,8 @@ func HasAccess(u *User, repo *Repository, testMode AccessMode) (bool, error) {
return hasAccess(x, u, repo, testMode) return hasAccess(x, u, repo, testMode)
} }
// GetAccessibleRepositories finds all repositories where a user has access to, // GetRepositoryAccesses finds all repositories with their access mode where a user has access but does not own.
// besides he/she owns. func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
func (u *User) GetAccessibleRepositories() (map[*Repository]AccessMode, error) {
accesses := make([]*Access, 0, 10) accesses := make([]*Access, 0, 10)
if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil { if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil {
return nil, err return nil, err
@ -80,7 +79,7 @@ func (u *User) GetAccessibleRepositories() (map[*Repository]AccessMode, error) {
repo, err := GetRepositoryByID(access.RepoID) repo, err := GetRepositoryByID(access.RepoID)
if err != nil { if err != nil {
if IsErrRepoNotExist(err) { if IsErrRepoNotExist(err) {
log.Error(4, "%v", err) log.Error(4, "GetRepositoryByID: %v", err)
continue continue
} }
return nil, err return nil, err
@ -92,11 +91,24 @@ func (u *User) GetAccessibleRepositories() (map[*Repository]AccessMode, error) {
} }
repos[repo] = access.Mode repos[repo] = access.Mode
} }
// FIXME: should we generate an ordered list here? Random looks weird.
return repos, nil return repos, nil
} }
// GetAccessibleRepositories finds all repositories where a user has access but does not own.
func (u *User) GetAccessibleRepositories() ([]*Repository, error) {
accesses := make([]*Access, 0, 10)
if err := x.Find(&accesses, &Access{UserID: u.Id}); err != nil {
return nil, err
}
repoIDs := make([]int64, 0, len(accesses))
for _, access := range accesses {
repoIDs = append(repoIDs, access.RepoID)
}
repos := make([]*Repository, 0, len(repoIDs))
return repos, x.Where("owner_id != ?", u.Id).In("id", repoIDs).Desc("updated").Find(&repos)
}
func maxAccessMode(modes ...AccessMode) AccessMode { func maxAccessMode(modes ...AccessMode) AccessMode {
max := ACCESS_MODE_NONE max := ACCESS_MODE_NONE
for _, mode := range modes { for _, mode := range modes {

View file

@ -106,9 +106,9 @@ func ListMyRepos(ctx *middleware.Context) {
} }
numOwnRepos := len(ownRepos) numOwnRepos := len(ownRepos)
accessibleRepos, err := ctx.User.GetAccessibleRepositories() accessibleRepos, err := ctx.User.GetRepositoryAccesses()
if err != nil { if err != nil {
ctx.APIError(500, "GetAccessibleRepositories", err) ctx.APIError(500, "GetRepositoryAccesses", err)
return return
} }

View file

@ -62,23 +62,21 @@ func Dashboard(ctx *middleware.Context) {
return return
} }
// Check context type.
if !ctxUser.IsOrganization() { if !ctxUser.IsOrganization() {
// Normal user. collaborateRepos, err := ctx.User.GetAccessibleRepositories()
ctxUser = ctx.User
collaborates, err := ctx.User.GetAccessibleRepositories()
if err != nil { if err != nil {
ctx.Handle(500, "GetAccessibleRepositories", err) ctx.Handle(500, "GetAccessibleRepositories", err)
return return
} }
repositories := make([]*models.Repository, 0, len(collaborates)) for i := range collaborateRepos {
for repo := range collaborates { if err = collaborateRepos[i].GetOwner(); err != nil {
repositories = append(repositories, repo) ctx.Handle(500, "GetOwner: "+collaborateRepos[i].Name, err)
return
}
} }
ctx.Data["CollaborateCount"] = len(collaborateRepos)
ctx.Data["CollaborateCount"] = len(repositories) ctx.Data["CollaborativeRepos"] = collaborateRepos
ctx.Data["CollaborativeRepos"] = repositories
} }
repos, err := models.GetRepositories(ctxUser.Id, true) repos, err := models.GetRepositories(ctxUser.Id, true)
@ -89,7 +87,7 @@ func Dashboard(ctx *middleware.Context) {
ctx.Data["Repos"] = repos ctx.Data["Repos"] = repos
// Get mirror repositories. // Get mirror repositories.
mirrors := make([]*models.Repository, 0, len(repos)/2) mirrors := make([]*models.Repository, 0, 5)
for _, repo := range repos { for _, repo := range repos {
if repo.IsMirror { if repo.IsMirror {
if err = repo.GetMirror(); err != nil { if err = repo.GetMirror(); err != nil {