Add/remove member need add/remove access to repositories as well
This commit is contained in:
parent
bba707de36
commit
a60edbe88c
3 changed files with 113 additions and 11 deletions
116
models/org.go
116
models/org.go
|
@ -7,8 +7,11 @@ package models
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/Unknwon/com"
|
||||||
|
|
||||||
"github.com/gogits/gogs/modules/base"
|
"github.com/gogits/gogs/modules/base"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -343,9 +346,10 @@ type Team struct {
|
||||||
Description string
|
Description string
|
||||||
Authorize AuthorizeType
|
Authorize AuthorizeType
|
||||||
RepoIds string `xorm:"TEXT"`
|
RepoIds string `xorm:"TEXT"`
|
||||||
NumMembers int
|
Repos []*Repository `xorm:"-"`
|
||||||
NumRepos int
|
|
||||||
Members []*User `xorm:"-"`
|
Members []*User `xorm:"-"`
|
||||||
|
NumRepos int
|
||||||
|
NumMembers int
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTeamMember returns true if given user is a member of team.
|
// IsTeamMember returns true if given user is a member of team.
|
||||||
|
@ -353,12 +357,40 @@ func (t *Team) IsMember(uid int64) bool {
|
||||||
return IsTeamMember(t.OrgId, t.Id, uid)
|
return IsTeamMember(t.OrgId, t.Id, uid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetMembers returns all members in given team of organization.
|
// GetRepositories returns all repositories in team of organization.
|
||||||
|
func (t *Team) GetRepositories() error {
|
||||||
|
idStrs := strings.Split(t.RepoIds, "|")
|
||||||
|
t.Repos = make([]*Repository, 0, len(idStrs))
|
||||||
|
for _, str := range idStrs {
|
||||||
|
id := com.StrTo(str).MustInt64()
|
||||||
|
if id == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
repo, err := GetRepositoryById(id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
t.Repos = append(t.Repos, repo)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMembers returns all members in team of organization.
|
||||||
func (t *Team) GetMembers() (err error) {
|
func (t *Team) GetMembers() (err error) {
|
||||||
t.Members, err = GetTeamMembers(t.OrgId, t.Id)
|
t.Members, err = GetTeamMembers(t.OrgId, t.Id)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddMember adds new member to team of organization.
|
||||||
|
func (t *Team) AddMember(uid int64) error {
|
||||||
|
return AddTeamMember(t.OrgId, t.Id, uid)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveMember removes member from team of organization.
|
||||||
|
func (t *Team) RemoveMember(uid int64) error {
|
||||||
|
return RemoveTeamMember(t.OrgId, t.Id, uid)
|
||||||
|
}
|
||||||
|
|
||||||
// NewTeam creates a record of new team.
|
// NewTeam creates a record of new team.
|
||||||
// It's caller's responsibility to assign organization ID.
|
// It's caller's responsibility to assign organization ID.
|
||||||
func NewTeam(t *Team) error {
|
func NewTeam(t *Team) error {
|
||||||
|
@ -483,13 +515,29 @@ func AddTeamMember(orgId, teamId, uid int64) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can use raw SQL here but we also want to vertify there is a such team.
|
// Get team and its repositories.
|
||||||
t, err := GetTeamById(teamId)
|
t, err := GetTeamById(teamId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.NumMembers++
|
t.NumMembers++
|
||||||
|
|
||||||
|
if err = t.GetRepositories(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get organization.
|
||||||
|
org, err := GetUserById(orgId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user.
|
||||||
|
u, err := GetUserById(uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
defer sess.Close()
|
defer sess.Close()
|
||||||
if err = sess.Begin(); err != nil {
|
if err = sess.Begin(); err != nil {
|
||||||
|
@ -502,6 +550,15 @@ func AddTeamMember(orgId, teamId, uid int64) error {
|
||||||
TeamId: teamId,
|
TeamId: teamId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mode := READABLE
|
||||||
|
if t.Authorize > ORG_READABLE {
|
||||||
|
mode = WRITABLE
|
||||||
|
}
|
||||||
|
access := &Access{
|
||||||
|
UserName: u.LowerName,
|
||||||
|
Mode: mode,
|
||||||
|
}
|
||||||
|
|
||||||
if _, err = sess.Insert(tu); err != nil {
|
if _, err = sess.Insert(tu); err != nil {
|
||||||
sess.Rollback()
|
sess.Rollback()
|
||||||
return err
|
return err
|
||||||
|
@ -510,15 +567,47 @@ func AddTeamMember(orgId, teamId, uid int64) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Give access to team repositories.
|
||||||
|
for _, repo := range t.Repos {
|
||||||
|
access.RepoName = path.Join(org.LowerName, repo.LowerName)
|
||||||
|
if _, err = sess.Insert(access); err != nil {
|
||||||
|
sess.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveMember removes member from given team of given organization.
|
// RemoveTeamMember removes member from given team of given organization.
|
||||||
func RemoveMember(orgId, teamId, uid int64) error {
|
func RemoveTeamMember(orgId, teamId, uid int64) error {
|
||||||
if !IsTeamMember(orgId, teamId, uid) {
|
if !IsTeamMember(orgId, teamId, uid) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get team and its repositories.
|
||||||
|
t, err := GetTeamById(teamId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
t.NumMembers--
|
||||||
|
|
||||||
|
if err = t.GetRepositories(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get organization.
|
||||||
|
org, err := GetUserById(orgId)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get user.
|
||||||
|
u, err := GetUserById(uid)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
defer sess.Close()
|
defer sess.Close()
|
||||||
if err := sess.Begin(); err != nil {
|
if err := sess.Begin(); err != nil {
|
||||||
|
@ -531,13 +620,26 @@ func RemoveMember(orgId, teamId, uid int64) error {
|
||||||
TeamId: teamId,
|
TeamId: teamId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
access := &Access{
|
||||||
|
UserName: u.LowerName,
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := sess.Delete(tu); err != nil {
|
if _, err := sess.Delete(tu); err != nil {
|
||||||
sess.Rollback()
|
sess.Rollback()
|
||||||
return err
|
return err
|
||||||
} else if _, err = sess.Exec("UPDATE `team` SET num_members = num_members - 1 WHERE id = ?", teamId); err != nil {
|
} else if _, err = sess.Id(t.Id).AllCols().Update(t); err != nil {
|
||||||
sess.Rollback()
|
sess.Rollback()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete access to team repositories.
|
||||||
|
for _, repo := range t.Repos {
|
||||||
|
access.RepoName = path.Join(org.LowerName, repo.LowerName)
|
||||||
|
if _, err = sess.Delete(access); err != nil {
|
||||||
|
sess.Rollback()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
|
@ -502,7 +502,7 @@ func CreateRepository(u *User, name, desc, lang, license string, private, mirror
|
||||||
}
|
}
|
||||||
access := &Access{
|
access := &Access{
|
||||||
UserName: u.LowerName,
|
UserName: u.LowerName,
|
||||||
RepoName: strings.ToLower(path.Join(u.Name, repo.Name)),
|
RepoName: path.Join(u.LowerName, repo.LowerName),
|
||||||
Mode: mode,
|
Mode: mode,
|
||||||
}
|
}
|
||||||
// Give access to all members in owner team.
|
// Give access to all members in owner team.
|
||||||
|
|
|
@ -41,9 +41,9 @@ func TeamsAction(ctx *middleware.Context) {
|
||||||
var err error
|
var err error
|
||||||
switch ctx.Params(":action") {
|
switch ctx.Params(":action") {
|
||||||
case "join":
|
case "join":
|
||||||
err = models.AddTeamMember(ctx.Org.Organization.Id, ctx.Org.Team.Id, ctx.User.Id)
|
err = ctx.Org.Team.AddMember(ctx.User.Id)
|
||||||
case "leave":
|
case "leave":
|
||||||
err = models.RemoveMember(ctx.Org.Organization.Id, ctx.Org.Team.Id, ctx.User.Id)
|
err = ctx.Org.Team.RemoveMember(ctx.User.Id)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue