diff options
author | Unknwon <u@gogs.io> | 2015-02-28 21:44:09 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-02-28 21:44:09 -0500 |
commit | 473e265727c1e47dd83e3f66a5cc92d2f027d951 (patch) | |
tree | bece3e29269a694b47a9d15d1ec27b8e9d38186f /models/access.go | |
parent | e3dece1371f38f5af72c200bc4c4684ec2fc396b (diff) | |
download | gitea-473e265727c1e47dd83e3f66a5cc92d2f027d951.tar.gz gitea-473e265727c1e47dd83e3f66a5cc92d2f027d951.zip |
models: done testing on new access system
Diffstat (limited to 'models/access.go')
-rw-r--r-- | models/access.go | 98 |
1 files changed, 58 insertions, 40 deletions
diff --git a/models/access.go b/models/access.go index 683282f336..00871f9481 100644 --- a/models/access.go +++ b/models/access.go @@ -100,25 +100,58 @@ func maxAccessMode(modes ...AccessMode) AccessMode { return max } -func (repo *Repository) recalculateTeamAccesses(e Engine, mode AccessMode) error { +// FIXME: do corss-comparison so reduce deletions and additions to the minimum? +func (repo *Repository) refreshAccesses(e Engine, accessMap map[int64]AccessMode) (err error) { + minMode := ACCESS_MODE_READ + if !repo.IsPrivate { + minMode = ACCESS_MODE_WRITE + } + + newAccesses := make([]Access, 0, len(accessMap)) + for userID, mode := range accessMap { + if mode < minMode { + continue + } + newAccesses = append(newAccesses, Access{ + UserID: userID, + RepoID: repo.Id, + Mode: mode, + }) + } + // Delete old accesses and insert new ones for repository. + if _, err = e.Delete(&Access{RepoID: repo.Id}); err != nil { + return fmt.Errorf("delete old accesses: %v", err) + } else if _, err = e.Insert(newAccesses); err != nil { + return fmt.Errorf("insert new accesses: %v", err) + } return nil } -func (repo *Repository) recalculateAccesses(e Engine) error { - accessMap := make(map[int64]AccessMode, 20) - - // FIXME: should be able to have read-only access. - // Give all collaborators write access. +// FIXME: should be able to have read-only access. +// Give all collaborators write access. +func (repo *Repository) refreshCollaboratorAccesses(e Engine, accessMap map[int64]AccessMode) error { collaborators, err := repo.getCollaborators(e) if err != nil { - return err + return fmt.Errorf("getCollaborators: %v", err) } for _, c := range collaborators { accessMap[c.Id] = ACCESS_MODE_WRITE } + return nil +} + +// recalculateTeamAccesses recalculates new accesses for teams of an organization +// except the team whose ID is given. It is used to assign a team ID when +// remove repository from that team. +func (repo *Repository) recalculateTeamAccesses(e Engine, ignTeamID int64) (err error) { + accessMap := make(map[int64]AccessMode, 20) + + if err = repo.refreshCollaboratorAccesses(e, accessMap); err != nil { + return fmt.Errorf("refreshCollaboratorAccesses: %v", err) + } - if err := repo.getOwner(e); err != nil { + if err = repo.getOwner(e); err != nil { return err } if repo.Owner.IsOrganization() { @@ -126,47 +159,32 @@ func (repo *Repository) recalculateAccesses(e Engine) error { return err } - for _, team := range repo.Owner.Teams { - if team.IsOwnerTeam() { - team.Authorize = ACCESS_MODE_OWNER + for _, t := range repo.Owner.Teams { + if t.ID == ignTeamID { + continue + } + if t.IsOwnerTeam() { + t.Authorize = ACCESS_MODE_OWNER } - if err = team.getMembers(e); err != nil { - return fmt.Errorf("getMembers '%d': %v", team.ID, err) + if err = t.getMembers(e); err != nil { + return fmt.Errorf("getMembers '%d': %v", t.ID, err) } - for _, u := range team.Members { - accessMap[u.Id] = maxAccessMode(accessMap[u.Id], team.Authorize) + for _, m := range t.Members { + accessMap[m.Id] = maxAccessMode(accessMap[m.Id], t.Authorize) } } } - // FIXME: do corss-comparison so reduce deletions and additions to the minimum? - - minMode := ACCESS_MODE_READ - if !repo.IsPrivate { - minMode = ACCESS_MODE_WRITE - } - - newAccesses := make([]Access, 0, len(accessMap)) - for userID, mode := range accessMap { - if mode < minMode { - continue - } - newAccesses = append(newAccesses, Access{ - UserID: userID, - RepoID: repo.Id, - Mode: mode, - }) - } + return repo.refreshAccesses(e, accessMap) +} - // Delete old accesses and insert new ones for repository. - if _, err = e.Delete(&Access{RepoID: repo.Id}); err != nil { - return fmt.Errorf("delete old accesses: %v", err) - } else if _, err = e.Insert(newAccesses); err != nil { - return fmt.Errorf("insert new accesses: %v", err) +func (repo *Repository) recalculateAccesses(e Engine) error { + accessMap := make(map[int64]AccessMode, 20) + if err := repo.refreshCollaboratorAccesses(e, accessMap); err != nil { + return fmt.Errorf("refreshCollaboratorAccesses: %v", err) } - - return nil + return repo.refreshAccesses(e, accessMap) } // RecalculateAccesses recalculates all accesses for repository. |