summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-02-28 21:50:29 -0500
committerUnknwon <u@gogs.io>2015-02-28 21:50:29 -0500
commitb0b11fd7b1bd5e556c27ccae73668b7df74cd73e (patch)
tree134a3b875952990ffe83f5b583d461b6b2e942df /routers
parent3a6ba39a61c68d967c6c08aa087d2de2d0bf7ec0 (diff)
parentf44204e9449c09e0fef37a4974dc2f2043593d6f (diff)
downloadgitea-b0b11fd7b1bd5e556c27ccae73668b7df74cd73e.tar.gz
gitea-b0b11fd7b1bd5e556c27ccae73668b7df74cd73e.zip
Merge branch 'access' of github.com:gogits/gogs into dev
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo.go21
-rw-r--r--routers/api/v1/repo_file.go2
-rw-r--r--routers/install.go1
-rw-r--r--routers/org/teams.go18
-rw-r--r--routers/repo/http.go15
-rw-r--r--routers/repo/issue.go24
-rw-r--r--routers/repo/release.go10
-rw-r--r--routers/repo/repo.go2
-rw-r--r--routers/repo/setting.go65
-rw-r--r--routers/user/home.go34
10 files changed, 88 insertions, 104 deletions
diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go
index fde184d994..eb9908911f 100644
--- a/routers/api/v1/repo.go
+++ b/routers/api/v1/repo.go
@@ -238,28 +238,31 @@ func ListMyRepos(ctx *middleware.Context) {
}
numOwnRepos := len(ownRepos)
- collaRepos, err := models.GetCollaborativeRepos(ctx.User.Name)
+ accessibleRepos, err := ctx.User.GetAccessibleRepositories()
if err != nil {
- ctx.JSON(500, &base.ApiJsonErr{"GetCollaborativeRepos: " + err.Error(), base.DOC_URL})
+ ctx.JSON(500, &base.ApiJsonErr{"GetAccessibleRepositories: " + err.Error(), base.DOC_URL})
return
}
- repos := make([]*api.Repository, numOwnRepos+len(collaRepos))
+ repos := make([]*api.Repository, numOwnRepos+len(accessibleRepos))
for i := range ownRepos {
repos[i] = ToApiRepository(ctx.User, ownRepos[i], api.Permission{true, true, true})
}
- for i := range collaRepos {
- if err = collaRepos[i].GetOwner(); err != nil {
+ i := numOwnRepos
+
+ for repo, access := range accessibleRepos {
+ if err = repo.GetOwner(); err != nil {
ctx.JSON(500, &base.ApiJsonErr{"GetOwner: " + err.Error(), base.DOC_URL})
return
}
- j := i + numOwnRepos
- repos[j] = ToApiRepository(collaRepos[i].Owner, collaRepos[i].Repository, api.Permission{false, collaRepos[i].CanPush, true})
+
+ repos[i] = ToApiRepository(repo.Owner, repo, api.Permission{false, access >= models.ACCESS_MODE_WRITE, true})
// FIXME: cache result to reduce DB query?
- if collaRepos[i].Owner.IsOrganization() && collaRepos[i].Owner.IsOwnedBy(ctx.User.Id) {
- repos[j].Permissions.Admin = true
+ if repo.Owner.IsOrganization() && repo.Owner.IsOwnedBy(ctx.User.Id) {
+ repos[i].Permissions.Admin = true
}
+ i++
}
ctx.JSON(200, &repos)
diff --git a/routers/api/v1/repo_file.go b/routers/api/v1/repo_file.go
index a049904f95..73f97b2cae 100644
--- a/routers/api/v1/repo_file.go
+++ b/routers/api/v1/repo_file.go
@@ -12,7 +12,7 @@ import (
)
func GetRepoRawFile(ctx *middleware.Context) {
- if ctx.Repo.Repository.IsPrivate && !ctx.Repo.HasAccess {
+ if !ctx.Repo.HasAccess() {
ctx.Error(404)
return
}
diff --git a/routers/install.go b/routers/install.go
index a7828e351c..4aa3ca9306 100644
--- a/routers/install.go
+++ b/routers/install.go
@@ -224,6 +224,7 @@ func InstallPost(ctx *middleware.Context, form auth.InstallForm) {
cfg.Section("session").Key("PROVIDER").SetValue("file")
cfg.Section("log").Key("MODE").SetValue("file")
+ cfg.Section("log").Key("LEVEL").SetValue("Info")
cfg.Section("security").Key("INSTALL_LOCK").SetValue("true")
cfg.Section("security").Key("SECRET_KEY").SetValue(base.GetRandomString(15))
diff --git a/routers/org/teams.go b/routers/org/teams.go
index 9dd9b8e229..69f2734c64 100644
--- a/routers/org/teams.go
+++ b/routers/org/teams.go
@@ -165,14 +165,14 @@ func NewTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
}
// Validate permission level.
- var auth models.AuthorizeType
+ var auth models.AccessMode
switch form.Permission {
case "read":
- auth = models.ORG_READABLE
+ auth = models.ACCESS_MODE_READ
case "write":
- auth = models.ORG_WRITABLE
+ auth = models.ACCESS_MODE_WRITE
case "admin":
- auth = models.ORG_ADMIN
+ auth = models.ACCESS_MODE_ADMIN
default:
ctx.Error(401)
return
@@ -181,7 +181,7 @@ func NewTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
org := ctx.Org.Organization
t := &models.Team{
- OrgId: org.Id,
+ OrgID: org.Id,
Name: form.TeamName,
Description: form.Description,
Authorize: auth,
@@ -246,14 +246,14 @@ func EditTeamPost(ctx *middleware.Context, form auth.CreateTeamForm) {
isAuthChanged := false
if !t.IsOwnerTeam() {
// Validate permission level.
- var auth models.AuthorizeType
+ var auth models.AccessMode
switch form.Permission {
case "read":
- auth = models.ORG_READABLE
+ auth = models.ACCESS_MODE_READ
case "write":
- auth = models.ORG_WRITABLE
+ auth = models.ACCESS_MODE_WRITE
case "admin":
- auth = models.ORG_ADMIN
+ auth = models.ACCESS_MODE_ADMIN
default:
ctx.Error(401)
return
diff --git a/routers/repo/http.go b/routers/repo/http.go
index 35338fefbe..3cfc065999 100644
--- a/routers/repo/http.go
+++ b/routers/repo/http.go
@@ -131,18 +131,18 @@ func Http(ctx *middleware.Context) {
}
if !isPublicPull {
- var tp = models.WRITABLE
+ var tp = models.ACCESS_MODE_WRITE
if isPull {
- tp = models.READABLE
+ tp = models.ACCESS_MODE_READ
}
- has, err := models.HasAccess(authUsername, username+"/"+reponame, tp)
+ has, err := models.HasAccess(authUser, repo, tp)
if err != nil {
ctx.Handle(401, "no basic auth and digit auth", nil)
return
} else if !has {
- if tp == models.READABLE {
- has, err = models.HasAccess(authUsername, username+"/"+reponame, models.WRITABLE)
+ if tp == models.ACCESS_MODE_READ {
+ has, err = models.HasAccess(authUser, repo, models.ACCESS_MODE_WRITE)
if err != nil || !has {
ctx.Handle(401, "no basic auth and digit auth", nil)
return
@@ -152,6 +152,11 @@ func Http(ctx *middleware.Context) {
return
}
}
+
+ if !isPull && repo.IsMirror {
+ ctx.Handle(401, "can't push to mirror", nil)
+ return
+ }
}
}
diff --git a/routers/repo/issue.go b/routers/repo/issue.go
index 722bd0c3c4..abe33bebf3 100644
--- a/routers/repo/issue.go
+++ b/routers/repo/issue.go
@@ -174,7 +174,7 @@ func CreateIssue(ctx *middleware.Context) {
return
}
- us, err := models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
+ us, err := ctx.Repo.Repository.GetCollaborators()
if err != nil {
ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err)
return
@@ -218,7 +218,7 @@ func CreateIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) {
return
}
- _, err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
+ _, err = ctx.Repo.Repository.GetCollaborators()
if err != nil {
send(500, nil, err)
return
@@ -230,7 +230,7 @@ func CreateIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) {
}
// Only collaborators can assign.
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
form.AssigneeId = 0
}
issue := &models.Issue{
@@ -246,8 +246,8 @@ func CreateIssuePost(ctx *middleware.Context, form auth.CreateIssueForm) {
if err := models.NewIssue(issue); err != nil {
send(500, nil, err)
return
- } else if err := models.NewIssueUserPairs(issue.RepoId, issue.Id, ctx.Repo.Owner.Id,
- ctx.User.Id, form.AssigneeId, ctx.Repo.Repository.Name); err != nil {
+ } else if err := models.NewIssueUserPairs(ctx.Repo.Repository, issue.Id, ctx.Repo.Owner.Id,
+ ctx.User.Id, form.AssigneeId); err != nil {
send(500, nil, err)
return
}
@@ -384,7 +384,7 @@ func ViewIssue(ctx *middleware.Context) {
}
// Get all collaborators.
- ctx.Data["Collaborators"], err = models.GetCollaborators(strings.TrimPrefix(ctx.Repo.RepoLink, "/"))
+ ctx.Data["Collaborators"], err = ctx.Repo.Repository.GetCollaborators()
if err != nil {
ctx.Handle(500, "issue.CreateIssue(GetCollaborators)", err)
return
@@ -434,7 +434,7 @@ func ViewIssue(ctx *middleware.Context) {
ctx.Data["Title"] = issue.Name
ctx.Data["Issue"] = issue
ctx.Data["Comments"] = comments
- ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
+ ctx.Data["IsIssueOwner"] = ctx.Repo.IsOwner() || (ctx.IsSigned && issue.PosterId == ctx.User.Id)
ctx.Data["IsRepoToolbarIssues"] = true
ctx.Data["IsRepoToolbarIssuesList"] = false
ctx.HTML(200, ISSUE_VIEW)
@@ -457,7 +457,7 @@ func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) {
return
}
- if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner {
+ if ctx.User.Id != issue.PosterId && !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -484,7 +484,7 @@ func UpdateIssue(ctx *middleware.Context, form auth.CreateIssueForm) {
}
func UpdateIssueLabel(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -561,7 +561,7 @@ func UpdateIssueLabel(ctx *middleware.Context) {
}
func UpdateIssueMilestone(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -607,7 +607,7 @@ func UpdateIssueMilestone(ctx *middleware.Context) {
}
func UpdateAssignee(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(403)
return
}
@@ -753,7 +753,7 @@ func Comment(ctx *middleware.Context) {
// Check if issue owner changes the status of issue.
var newStatus string
- if ctx.Repo.IsOwner || issue.PosterId == ctx.User.Id {
+ if ctx.Repo.IsOwner() || issue.PosterId == ctx.User.Id {
newStatus = ctx.Query("change_status")
}
if len(newStatus) > 0 {
diff --git a/routers/repo/release.go b/routers/repo/release.go
index 591810cc5f..52d78b1967 100644
--- a/routers/repo/release.go
+++ b/routers/repo/release.go
@@ -41,7 +41,7 @@ func Releases(ctx *middleware.Context) {
tags := make([]*models.Release, len(rawTags))
for i, rawTag := range rawTags {
for j, rel := range rels {
- if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner) {
+ if rel == nil || (rel.IsDraft && !ctx.Repo.IsOwner()) {
continue
}
if rel.TagName == rawTag {
@@ -140,7 +140,7 @@ func Releases(ctx *middleware.Context) {
}
func NewRelease(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.ReleasesNew", nil)
return
}
@@ -153,7 +153,7 @@ func NewRelease(ctx *middleware.Context) {
}
func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.ReleasesNew", nil)
return
}
@@ -211,7 +211,7 @@ func NewReleasePost(ctx *middleware.Context, form auth.NewReleaseForm) {
}
func EditRelease(ctx *middleware.Context) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.ReleasesEdit", nil)
return
}
@@ -234,7 +234,7 @@ func EditRelease(ctx *middleware.Context) {
}
func EditReleasePost(ctx *middleware.Context, form auth.EditReleaseForm) {
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Handle(403, "release.EditReleasePost", nil)
return
}
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index dfd827bbb9..6b84a389d5 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -349,7 +349,7 @@ func Action(ctx *middleware.Context) {
case "unstar":
err = models.StarRepo(ctx.User.Id, ctx.Repo.Repository.Id, false)
case "desc":
- if !ctx.Repo.IsOwner {
+ if !ctx.Repo.IsOwner() {
ctx.Error(404)
return
}
diff --git a/routers/repo/setting.go b/routers/repo/setting.go
index b40ef2d9ae..5cd39ada2c 100644
--- a/routers/repo/setting.go
+++ b/routers/repo/setting.go
@@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
- "path"
"strings"
"time"
@@ -54,15 +53,11 @@ func SettingsPost(ctx *middleware.Context, form auth.RepoSettingForm) {
newRepoName := form.RepoName
// Check if repository name has been changed.
if ctx.Repo.Repository.Name != newRepoName {
- isExist, err := models.IsRepositoryExist(ctx.Repo.Owner, newRepoName)
- if err != nil {
- ctx.Handle(500, "IsRepositoryExist", err)
- return
- } else if isExist {
+ if models.IsRepositoryExist(ctx.Repo.Owner, newRepoName) {
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, nil)
return
- } else if err = models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
+ } else if err := models.ChangeRepositoryName(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name, newRepoName); err != nil {
if err == models.ErrRepoNameIllegal {
ctx.Data["Err_RepoName"] = true
ctx.RenderWithErr(ctx.Tr("form.illegal_repo_name"), SETTINGS_OPTIONS, nil)
@@ -169,22 +164,12 @@ func SettingsCollaboration(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
ctx.Data["PageIsSettingsCollaboration"] = true
- repoLink := path.Join(ctx.Repo.Owner.LowerName, ctx.Repo.Repository.LowerName)
-
if ctx.Req.Method == "POST" {
name := strings.ToLower(ctx.Query("collaborator"))
if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
return
}
- has, err := models.HasAccess(name, repoLink, models.WRITABLE)
- if err != nil {
- ctx.Handle(500, "HasAccess", err)
- return
- } else if has {
- ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
- return
- }
u, err := models.GetUserByName(name)
if err != nil {
@@ -204,9 +189,8 @@ func SettingsCollaboration(ctx *middleware.Context) {
return
}
- if err = models.AddAccess(&models.Access{UserName: name, RepoName: repoLink,
- Mode: models.WRITABLE}); err != nil {
- ctx.Handle(500, "AddAccess", err)
+ if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
+ ctx.Handle(500, "AddCollaborator", err)
return
}
@@ -225,50 +209,27 @@ func SettingsCollaboration(ctx *middleware.Context) {
// Delete collaborator.
remove := strings.ToLower(ctx.Query("remove"))
if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
- needDelete := true
- if ctx.User.IsOrganization() {
- // Check if user belongs to a team that has access to this repository.
- auth, err := models.GetHighestAuthorize(ctx.Repo.Owner.Id, ctx.User.Id, ctx.Repo.Repository.Id, 0)
- if err != nil {
- ctx.Handle(500, "GetHighestAuthorize", err)
- return
- }
- if auth > 0 {
- needDelete = false
- }
+ u, err := models.GetUserByName(remove)
+ if err != nil {
+ ctx.Handle(500, "GetUserByName", err)
+ return
}
-
- if needDelete {
- if err := models.DeleteAccess(&models.Access{UserName: remove, RepoName: repoLink}); err != nil {
- ctx.Handle(500, "DeleteAccess", err)
- return
- }
+ if err := ctx.Repo.Repository.DeleteCollaborator(u); err != nil {
+ ctx.Handle(500, "DeleteCollaborator", err)
+ return
}
ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
return
}
- names, err := models.GetCollaboratorNames(repoLink)
+ users, err := ctx.Repo.Repository.GetCollaborators()
if err != nil {
ctx.Handle(500, "GetCollaborators", err)
return
}
- collaborators := make([]*models.User, 0, len(names))
- for _, name := range names {
- u, err := models.GetUserByName(name)
- if err != nil {
- ctx.Handle(500, "GetUserByName", err)
- return
- }
- // Does not show organization members.
- if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
- continue
- }
- collaborators = append(collaborators, u)
- }
- ctx.Data["Collaborators"] = collaborators
+ ctx.Data["Collaborators"] = users
ctx.HTML(200, COLLABORATION)
}
diff --git a/routers/user/home.go b/routers/user/home.go
index 1aabe08774..0a1d9dd217 100644
--- a/routers/user/home.go
+++ b/routers/user/home.go
@@ -49,13 +49,19 @@ func Dashboard(ctx *middleware.Context) {
} else {
// Normal user.
ctxUser = ctx.User
- collaborates, err := models.GetCollaborativeRepos(ctxUser.Name)
+ collaborates, err := ctx.User.GetAccessibleRepositories()
if err != nil {
- ctx.Handle(500, "GetCollaborativeRepos", err)
+ ctx.Handle(500, "GetAccessibleRepositories", err)
return
}
- ctx.Data["CollaborateCount"] = len(collaborates)
- ctx.Data["CollaborativeRepos"] = collaborates
+
+ repositories := make([]*models.Repository, 0, len(collaborates))
+ for repo := range collaborates {
+ repositories = append(repositories, repo)
+ }
+
+ ctx.Data["CollaborateCount"] = len(repositories)
+ ctx.Data["CollaborativeRepos"] = repositories
}
ctx.Data["ContextUser"] = ctxUser
@@ -97,10 +103,14 @@ func Dashboard(ctx *middleware.Context) {
feeds := make([]*models.Action, 0, len(actions))
for _, act := range actions {
if act.IsPrivate {
- if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
- models.READABLE); !has {
- continue
+ // This prevents having to retrieve the repository for each action
+ repo := &models.Repository{Id: act.RepoId, IsPrivate: true}
+ if act.RepoUserName != ctx.User.LowerName {
+ if has, _ := models.HasAccess(ctx.User, repo, models.ACCESS_MODE_READ); !has {
+ continue
+ }
}
+
}
// FIXME: cache results?
u, err := models.GetUserByName(act.ActUserName)
@@ -205,10 +215,14 @@ func Profile(ctx *middleware.Context) {
if !ctx.IsSigned {
continue
}
- if has, _ := models.HasAccess(ctx.User.Name, act.RepoUserName+"/"+act.RepoName,
- models.READABLE); !has {
- continue
+ // This prevents having to retrieve the repository for each action
+ repo := &models.Repository{Id: act.RepoId, IsPrivate: true}
+ if act.RepoUserName != ctx.User.LowerName {
+ if has, _ := models.HasAccess(ctx.User, repo, models.ACCESS_MODE_READ); !has {
+ continue
+ }
}
+
}
// FIXME: cache results?
u, err := models.GetUserByName(act.ActUserName)