summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorPeter Smit <peter@smitmail.eu>2015-02-05 15:29:08 +0200
committerPeter Smit <peter@smitmail.eu>2015-02-12 14:14:45 +0200
commit0a4cda0dd4d7c906668d9d2e283fbe9ebe2e1608 (patch)
tree7068350836e187523f0bcf741695c84c0eab42a9 /models/repo.go
parent2804784df93f392ed76a85d8e7153c66964f1ba5 (diff)
downloadgitea-0a4cda0dd4d7c906668d9d2e283fbe9ebe2e1608.tar.gz
gitea-0a4cda0dd4d7c906668d9d2e283fbe9ebe2e1608.zip
Refactoring of the Access Table
This commit does a lot of the work of refactoring the access table in a table with id's instead of strings. The result does compile, but has not been tested. It may eat your kittens.
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go251
1 files changed, 22 insertions, 229 deletions
diff --git a/models/repo.go b/models/repo.go
index 35ee871fd4..5a669d9dd8 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -206,14 +206,6 @@ func (repo *Repository) IsOwnedBy(u *User) bool {
return repo.OwnerId == u.Id
}
-func (repo *Repository) HasAccess(uname string) bool {
- if err := repo.GetOwner(); err != nil {
- return false
- }
- has, _ := HasAccess(uname, path.Join(repo.Owner.Name, repo.Name), READABLE)
- return has
-}
-
// DescriptionHtml does special handles to description and return HTML string.
func (repo *Repository) DescriptionHtml() template.HTML {
sanitize := func(s string) string {
@@ -548,36 +540,11 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror
var t *Team // Owner team.
- mode := WRITABLE
- if mirror {
- mode = READABLE
- }
- access := &Access{
- UserName: u.LowerName,
- RepoName: path.Join(u.LowerName, repo.LowerName),
- Mode: mode,
- }
+ // TODO fix code for mirrors?
+
// Give access to all members in owner team.
if u.IsOrganization() {
- t, err = u.GetOwnerTeam()
- if err != nil {
- sess.Rollback()
- return nil, err
- }
- if err = t.GetMembers(); err != nil {
- sess.Rollback()
- return nil, err
- }
- for _, u := range t.Members {
- access.Id = 0
- access.UserName = u.LowerName
- if _, err = sess.Insert(access); err != nil {
- sess.Rollback()
- return nil, err
- }
- }
- } else {
- if _, err = sess.Insert(access); err != nil {
+ if err = repo.RecalcAccessSess(); err != nil {
sess.Rollback()
return nil, err
}
@@ -707,37 +674,10 @@ func TransferOwnership(u *User, newOwner string, repo *Repository) error {
}
owner := repo.Owner
- oldRepoLink := path.Join(owner.LowerName, repo.LowerName)
- // Delete all access first if current owner is an organization.
- if owner.IsOrganization() {
- if _, err = sess.Where("repo_name=?", oldRepoLink).Delete(new(Access)); err != nil {
- sess.Rollback()
- return fmt.Errorf("fail to delete current accesses: %v", err)
- }
- } else {
- // Delete current owner access.
- if _, err = sess.Where("repo_name=?", oldRepoLink).And("user_name=?", owner.LowerName).
- Delete(new(Access)); err != nil {
- sess.Rollback()
- return fmt.Errorf("fail to delete access(owner): %v", err)
- }
- // In case new owner has access.
- if _, err = sess.Where("repo_name=?", oldRepoLink).And("user_name=?", newUser.LowerName).
- Delete(new(Access)); err != nil {
- sess.Rollback()
- return fmt.Errorf("fail to delete access(new user): %v", err)
- }
- }
-
- // Change accesses to new repository path.
- if _, err = sess.Where("repo_name=?", oldRepoLink).
- Update(&Access{RepoName: path.Join(newUser.LowerName, repo.LowerName)}); err != nil {
- sess.Rollback()
- return fmt.Errorf("fail to update access(change reponame): %v", err)
- }
// Update repository.
repo.OwnerId = newUser.Id
+ repo.Owner = newUser
if _, err := sess.Id(repo.Id).Update(repo); err != nil {
sess.Rollback()
return err
@@ -754,53 +694,8 @@ func TransferOwnership(u *User, newOwner string, repo *Repository) error {
return err
}
- mode := WRITABLE
- if repo.IsMirror {
- mode = READABLE
- }
- // New owner is organization.
- if newUser.IsOrganization() {
- access := &Access{
- RepoName: path.Join(newUser.LowerName, repo.LowerName),
- Mode: mode,
- }
-
- // Give access to all members in owner team.
- t, err := newUser.GetOwnerTeam()
- if err != nil {
- sess.Rollback()
- return err
- }
- if err = t.GetMembers(); err != nil {
- sess.Rollback()
- return err
- }
- for _, u := range t.Members {
- access.Id = 0
- access.UserName = u.LowerName
- if _, err = sess.Insert(access); err != nil {
- sess.Rollback()
- return err
- }
- }
-
- // Update owner team info and count.
- t.RepoIds += "$" + com.ToStr(repo.Id) + "|"
- t.NumRepos++
- if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil {
- sess.Rollback()
- return err
- }
- } else {
- access := &Access{
- RepoName: path.Join(newUser.LowerName, repo.LowerName),
- UserName: newUser.LowerName,
- Mode: mode,
- }
- if _, err = sess.Insert(access); err != nil {
- sess.Rollback()
- return fmt.Errorf("fail to insert access: %v", err)
- }
+ if err = repo.RecalcAccessSess(); err != nil {
+ return err
}
// Change repository directory name.
@@ -833,33 +728,8 @@ func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error)
return ErrRepoNameIllegal
}
- // Update accesses.
- accesses := make([]Access, 0, 10)
- if err = x.Find(&accesses, &Access{RepoName: userName + "/" + oldRepoName}); err != nil {
- return err
- }
-
- sess := x.NewSession()
- defer sess.Close()
- if err = sess.Begin(); err != nil {
- return err
- }
-
- for i := range accesses {
- accesses[i].RepoName = userName + "/" + newRepoName
- if err = updateAccess(sess, &accesses[i]); err != nil {
- sess.Rollback()
- return err
- }
- }
-
// Change repository directory name.
- if err = os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName)); err != nil {
- sess.Rollback()
- return err
- }
-
- return sess.Commit()
+ return os.Rename(RepoPath(userName, oldRepoName), RepoPath(userName, newRepoName))
}
func UpdateRepository(repo *Repository) error {
@@ -908,7 +778,7 @@ func DeleteRepository(uid, repoId int64, userName string) error {
}
// Delete all access.
- if _, err := sess.Delete(&Access{RepoName: strings.ToLower(path.Join(userName, repo.Name))}); err != nil {
+ if _, err := sess.Delete(&Access{RepoID: repo.Id}); err != nil {
sess.Rollback()
return err
}
@@ -1228,41 +1098,30 @@ type Collaboration struct {
// Add collaborator and accompanying access
func (r *Repository) AddCollaborator(u *User) error {
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id}
+
has, err := x.Get(collaboration)
if err != nil {
return err
- } else if has {
- return nil
}
-
- if err = r.GetOwner(); err != nil {
- return err
+ if has {
+ return nil
}
- sess := x.NewSession()
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ if _, err = x.InsertOne(collaboration); err != nil {
return err
}
- if _, err = sess.InsertOne(collaboration); err != nil {
- sess.Rollback()
- return err
- } else if err = addAccess(sess, &Access{
- UserName: u.LowerName,
- RepoName: path.Join(r.Owner.LowerName, r.LowerName),
- Mode: WRITABLE}); err != nil {
- sess.Rollback()
+ if err = r.GetOwner(); err != nil {
return err
}
- return sess.Commit()
+ return r.RecalcAccessSess()
}
// GetCollaborators returns the collaborators for a repository
func (r *Repository) GetCollaborators() ([]*User, error) {
- collaborations := make([]*Collaboration, 0, 5)
- if err := x.Where("repo_id=?", r.Id).Find(&collaborations); err != nil {
+ collaborations := make([]*Collaboration, 0)
+ if err := x.Find(&collaborations, &Collaboration{RepoID: r.Id}); err != nil {
return nil, err
}
@@ -1278,50 +1137,14 @@ func (r *Repository) GetCollaborators() ([]*User, error) {
}
// Delete collaborator and accompanying access
-func (r *Repository) DeleteCollaborator(u *User) (err error) {
+func (r *Repository) DeleteCollaborator(u *User) error {
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id}
- has, err := x.Get(collaboration)
- if err != nil {
- return err
- } else if !has {
- return nil
- }
- if err = r.GetOwner(); err != nil {
- return err
- }
-
- sess := x.NewSession()
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ if has, err := x.Delete(collaboration); err != nil || has == 0 {
return err
}
- needDelete := true
- if r.Owner.IsOrganization() {
- auth, err := getHighestAuthorize(sess, r.Owner.Id, u.Id, r.Id, 0)
- if err != nil {
- sess.Rollback()
- return err
- }
- if auth > 0 {
- needDelete = false
- }
- }
- if needDelete {
- if err = deleteAccess(sess, &Access{
- UserName: u.LowerName,
- RepoName: path.Join(r.Owner.LowerName, r.LowerName),
- Mode: WRITABLE}); err != nil {
- sess.Rollback()
- return err
- } else if _, err = sess.Delete(collaboration); err != nil {
- sess.Rollback()
- return err
- }
- }
-
- return sess.Commit()
+ return r.RecalcAccessSess()
}
// __ __ __ .__
@@ -1495,40 +1318,10 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (*Repositor
return nil, err
}
- var t *Team // Owner team.
-
- mode := WRITABLE
-
- access := &Access{
- UserName: u.LowerName,
- RepoName: path.Join(u.LowerName, repo.LowerName),
- Mode: mode,
- }
- // Give access to all members in owner team.
- if u.IsOrganization() {
- t, err = u.GetOwnerTeam()
- if err != nil {
- sess.Rollback()
- return nil, err
- }
- if err = t.GetMembers(); err != nil {
- sess.Rollback()
- return nil, err
- }
- for _, u := range t.Members {
- access.Id = 0
- access.UserName = u.LowerName
- if _, err = sess.Insert(access); err != nil {
- sess.Rollback()
- return nil, err
- }
- }
- } else {
- if _, err = sess.Insert(access); err != nil {
- sess.Rollback()
- return nil, err
- }
+ if err = repo.RecalcAccessSess(); err != nil {
+ return nil, err
}
+ var t *Team // Owner team.
if _, err = sess.Exec(
"UPDATE `user` SET num_repos = num_repos + 1 WHERE id = ?", u.Id); err != nil {