mirror of https://github.com/go-gitea/gitea.git
Remove unnecessary loads in org_team (#1035)
This commit is contained in:
parent
f1ab906c51
commit
76057105ca
|
@ -517,7 +517,7 @@ func RemoveOrgUser(orgID, userID int64) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, t := range teams {
|
for _, t := range teams {
|
||||||
if err = removeTeamMember(sess, org.ID, t.ID, userID); err != nil {
|
if err = removeTeamMember(sess, t, userID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,12 +59,12 @@ func (t *Team) GetMembers() (err error) {
|
||||||
// AddMember adds new membership of the team to the organization,
|
// AddMember adds new membership of the team to the organization,
|
||||||
// the user will have membership to the organization automatically when needed.
|
// the user will have membership to the organization automatically when needed.
|
||||||
func (t *Team) AddMember(userID int64) error {
|
func (t *Team) AddMember(userID int64) error {
|
||||||
return AddTeamMember(t.OrgID, t.ID, userID)
|
return AddTeamMember(t, userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveMember removes member from team of organization.
|
// RemoveMember removes member from team of organization.
|
||||||
func (t *Team) RemoveMember(userID int64) error {
|
func (t *Team) RemoveMember(userID int64) error {
|
||||||
return RemoveTeamMember(t.OrgID, t.ID, userID)
|
return RemoveTeamMember(t, userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Team) hasRepository(e Engine, repoID int64) bool {
|
func (t *Team) hasRepository(e Engine, repoID int64) bool {
|
||||||
|
@ -443,113 +443,101 @@ func GetUserTeams(orgID, userID int64) ([]*Team, error) {
|
||||||
|
|
||||||
// AddTeamMember adds new membership of given team to given organization,
|
// AddTeamMember adds new membership of given team to given organization,
|
||||||
// the user will have membership to given organization automatically when needed.
|
// the user will have membership to given organization automatically when needed.
|
||||||
func AddTeamMember(orgID, teamID, userID int64) error {
|
func AddTeamMember(team *Team, userID int64) error {
|
||||||
if IsTeamMember(orgID, teamID, userID) {
|
if IsTeamMember(team.OrgID, team.ID, userID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := AddOrgUser(orgID, userID); err != nil {
|
if err := AddOrgUser(team.OrgID, userID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get team and its repositories.
|
// Get team and its repositories.
|
||||||
t, err := GetTeamByID(teamID)
|
team.NumMembers++
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
t.NumMembers++
|
|
||||||
|
|
||||||
if err = t.GetRepositories(); err != nil {
|
if err := team.GetRepositories(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
defer sessionRelease(sess)
|
defer sessionRelease(sess)
|
||||||
if err = sess.Begin(); err != nil {
|
if err := sess.Begin(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tu := &TeamUser{
|
if _, err := sess.Insert(&TeamUser{
|
||||||
UID: userID,
|
UID: userID,
|
||||||
OrgID: orgID,
|
OrgID: team.OrgID,
|
||||||
TeamID: teamID,
|
TeamID: team.ID,
|
||||||
}
|
}); err != nil {
|
||||||
if _, err = sess.Insert(tu); err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if _, err = sess.Id(t.ID).Update(t); err != nil {
|
} else if _, err := sess.Id(team.ID).Update(team); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Give access to team repositories.
|
// Give access to team repositories.
|
||||||
for _, repo := range t.Repos {
|
for _, repo := range team.Repos {
|
||||||
if err = repo.recalculateTeamAccesses(sess, 0); err != nil {
|
if err := repo.recalculateTeamAccesses(sess, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We make sure it exists before.
|
// We make sure it exists before.
|
||||||
ou := new(OrgUser)
|
ou := new(OrgUser)
|
||||||
if _, err = sess.
|
if _, err := sess.
|
||||||
Where("uid = ?", userID).
|
Where("uid = ?", userID).
|
||||||
And("org_id = ?", orgID).
|
And("org_id = ?", team.OrgID).
|
||||||
Get(ou); err != nil {
|
Get(ou); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ou.NumTeams++
|
ou.NumTeams++
|
||||||
if t.IsOwnerTeam() {
|
if team.IsOwnerTeam() {
|
||||||
ou.IsOwner = true
|
ou.IsOwner = true
|
||||||
}
|
}
|
||||||
if _, err = sess.Id(ou.ID).AllCols().Update(ou); err != nil {
|
if _, err := sess.Id(ou.ID).AllCols().Update(ou); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeTeamMember(e Engine, orgID, teamID, userID int64) error {
|
func removeTeamMember(e Engine, team *Team, userID int64) error {
|
||||||
if !isTeamMember(e, orgID, teamID, userID) {
|
if !isTeamMember(e, team.OrgID, team.ID, userID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get team and its repositories.
|
|
||||||
t, err := getTeamByID(e, teamID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the user to delete is the last member in owner team.
|
// Check if the user to delete is the last member in owner team.
|
||||||
if t.IsOwnerTeam() && t.NumMembers == 1 {
|
if team.IsOwnerTeam() && team.NumMembers == 1 {
|
||||||
return ErrLastOrgOwner{UID: userID}
|
return ErrLastOrgOwner{UID: userID}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.NumMembers--
|
team.NumMembers--
|
||||||
|
|
||||||
if err = t.getRepositories(e); err != nil {
|
if err := team.getRepositories(e); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get organization.
|
// Get organization.
|
||||||
org, err := getUserByID(e, orgID)
|
org, err := getUserByID(e, team.OrgID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
tu := &TeamUser{
|
if _, err := e.Delete(&TeamUser{
|
||||||
UID: userID,
|
UID: userID,
|
||||||
OrgID: orgID,
|
OrgID: team.OrgID,
|
||||||
TeamID: teamID,
|
TeamID: team.ID,
|
||||||
}
|
}); err != nil {
|
||||||
if _, err := e.Delete(tu); err != nil {
|
|
||||||
return err
|
return err
|
||||||
} else if _, err = e.
|
} else if _, err = e.
|
||||||
Id(t.ID).
|
Id(team.ID).
|
||||||
AllCols().
|
AllCols().
|
||||||
Update(t); err != nil {
|
Update(team); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete access to team repositories.
|
// Delete access to team repositories.
|
||||||
for _, repo := range t.Repos {
|
for _, repo := range team.Repos {
|
||||||
if err = repo.recalculateTeamAccesses(e, 0); err != nil {
|
if err = repo.recalculateTeamAccesses(e, 0); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -565,7 +553,7 @@ func removeTeamMember(e Engine, orgID, teamID, userID int64) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ou.NumTeams--
|
ou.NumTeams--
|
||||||
if t.IsOwnerTeam() {
|
if team.IsOwnerTeam() {
|
||||||
ou.IsOwner = false
|
ou.IsOwner = false
|
||||||
}
|
}
|
||||||
if _, err = e.
|
if _, err = e.
|
||||||
|
@ -578,13 +566,13 @@ func removeTeamMember(e Engine, orgID, teamID, userID int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTeamMember removes member from given team of given organization.
|
// RemoveTeamMember removes member from given team of given organization.
|
||||||
func RemoveTeamMember(orgID, teamID, userID int64) error {
|
func RemoveTeamMember(team *Team, userID int64) error {
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
defer sessionRelease(sess)
|
defer sessionRelease(sess)
|
||||||
if err := sess.Begin(); err != nil {
|
if err := sess.Begin(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := removeTeamMember(sess, orgID, teamID, userID); err != nil {
|
if err := removeTeamMember(sess, team, userID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
|
|
|
@ -298,7 +298,7 @@ func TestAddTeamMember(t *testing.T) {
|
||||||
|
|
||||||
test := func(teamID, userID int64) {
|
test := func(teamID, userID int64) {
|
||||||
team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
|
team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
|
||||||
assert.NoError(t, AddTeamMember(team.OrgID, team.ID, userID))
|
assert.NoError(t, AddTeamMember(team, userID))
|
||||||
AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
|
AssertExistsAndLoadBean(t, &TeamUser{UID: userID, TeamID: teamID})
|
||||||
CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID})
|
CheckConsistencyFor(t, &Team{ID: teamID}, &User{ID: team.OrgID})
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ func TestRemoveTeamMember(t *testing.T) {
|
||||||
|
|
||||||
testSuccess := func(teamID, userID int64) {
|
testSuccess := func(teamID, userID int64) {
|
||||||
team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
|
team := AssertExistsAndLoadBean(t, &Team{ID: teamID}).(*Team)
|
||||||
assert.NoError(t, RemoveTeamMember(team.OrgID, team.ID, userID))
|
assert.NoError(t, RemoveTeamMember(team, userID))
|
||||||
AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
|
AssertNotExistsBean(t, &TeamUser{UID: userID, TeamID: teamID})
|
||||||
CheckConsistencyFor(t, &Team{ID: teamID})
|
CheckConsistencyFor(t, &Team{ID: teamID})
|
||||||
}
|
}
|
||||||
|
@ -322,7 +322,7 @@ func TestRemoveTeamMember(t *testing.T) {
|
||||||
testSuccess(3, NonexistentID)
|
testSuccess(3, NonexistentID)
|
||||||
|
|
||||||
team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
|
team := AssertExistsAndLoadBean(t, &Team{ID: 1}).(*Team)
|
||||||
err := RemoveTeamMember(team.OrgID, team.ID, 2)
|
err := RemoveTeamMember(team, 2)
|
||||||
assert.True(t, IsErrLastOrgOwner(err))
|
assert.True(t, IsErrLastOrgOwner(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue