|
|
@ -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() |
|
|
|