mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-25 06:22:42 +01:00
golint for models/org_team.go
This commit is contained in:
parent
25b5722155
commit
21846d16e5
2 changed files with 47 additions and 46 deletions
|
@ -41,7 +41,7 @@ func (org *User) GetTeam(name string) (*Team, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (org *User) getOwnerTeam(e Engine) (*Team, error) {
|
func (org *User) getOwnerTeam(e Engine) (*Team, error) {
|
||||||
return org.getTeam(e, OWNER_TEAM)
|
return org.getTeam(e, ownerTeamName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOwnerTeam returns owner team of organization.
|
// GetOwnerTeam returns owner team of organization.
|
||||||
|
@ -52,7 +52,7 @@ func (org *User) GetOwnerTeam() (*Team, error) {
|
||||||
func (org *User) getTeams(e Engine) error {
|
func (org *User) getTeams(e Engine) error {
|
||||||
return e.
|
return e.
|
||||||
Where("org_id=?", org.ID).
|
Where("org_id=?", org.ID).
|
||||||
OrderBy("CASE WHEN name LIKE '" + OWNER_TEAM + "' THEN '' ELSE name END").
|
OrderBy("CASE WHEN name LIKE '" + ownerTeamName + "' THEN '' ELSE name END").
|
||||||
Find(&org.Teams)
|
Find(&org.Teams)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,8 +140,8 @@ func CreateOrganization(org, owner *User) (err error) {
|
||||||
// Create default owner team.
|
// Create default owner team.
|
||||||
t := &Team{
|
t := &Team{
|
||||||
OrgID: org.ID,
|
OrgID: org.ID,
|
||||||
LowerName: strings.ToLower(OWNER_TEAM),
|
LowerName: strings.ToLower(ownerTeamName),
|
||||||
Name: OWNER_TEAM,
|
Name: ownerTeamName,
|
||||||
Authorize: AccessModeOwner,
|
Authorize: AccessModeOwner,
|
||||||
NumMembers: 1,
|
NumMembers: 1,
|
||||||
}
|
}
|
||||||
|
@ -150,7 +150,7 @@ func CreateOrganization(org, owner *User) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = sess.Insert(&TeamUser{
|
if _, err = sess.Insert(&TeamUser{
|
||||||
Uid: owner.ID,
|
UID: owner.ID,
|
||||||
OrgID: org.ID,
|
OrgID: org.ID,
|
||||||
TeamID: t.ID,
|
TeamID: t.ID,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const OWNER_TEAM = "Owners"
|
const ownerTeamName = "Owners"
|
||||||
|
|
||||||
// Team represents a organization team.
|
// Team represents a organization team.
|
||||||
type Team struct {
|
type Team struct {
|
||||||
|
@ -28,12 +28,12 @@ type Team struct {
|
||||||
|
|
||||||
// IsOwnerTeam returns true if team is owner team.
|
// IsOwnerTeam returns true if team is owner team.
|
||||||
func (t *Team) IsOwnerTeam() bool {
|
func (t *Team) IsOwnerTeam() bool {
|
||||||
return t.Name == OWNER_TEAM
|
return t.Name == ownerTeamName
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTeamMember returns true if given user is a member of team.
|
// IsMember returns true if given user is a member of team.
|
||||||
func (t *Team) IsMember(uid int64) bool {
|
func (t *Team) IsMember(uID int64) bool {
|
||||||
return IsTeamMember(t.OrgID, t.ID, uid)
|
return IsTeamMember(t.OrgID, t.ID, uID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Team) getRepositories(e Engine) (err error) {
|
func (t *Team) getRepositories(e Engine) (err error) {
|
||||||
|
@ -72,13 +72,13 @@ 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(uid int64) error {
|
func (t *Team) AddMember(uID int64) error {
|
||||||
return AddTeamMember(t.OrgID, t.ID, uid)
|
return AddTeamMember(t.OrgID, t.ID, uID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveMember removes member from team of organization.
|
// RemoveMember removes member from team of organization.
|
||||||
func (t *Team) RemoveMember(uid int64) error {
|
func (t *Team) RemoveMember(uID int64) error {
|
||||||
return RemoveTeamMember(t.OrgID, t.ID, uid)
|
return RemoveTeamMember(t.OrgID, t.ID, uID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Team) hasRepository(e Engine, repoID int64) bool {
|
func (t *Team) hasRepository(e Engine, repoID int64) bool {
|
||||||
|
@ -196,6 +196,7 @@ func (t *Team) RemoveRepository(repoID int64) error {
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsUsableTeamName tests if a name could be as team name
|
||||||
func IsUsableTeamName(name string) (err error) {
|
func IsUsableTeamName(name string) (err error) {
|
||||||
var reservedTeamNames = []string{"new"}
|
var reservedTeamNames = []string{"new"}
|
||||||
|
|
||||||
|
@ -256,9 +257,9 @@ func NewTeam(t *Team) (err error) {
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTeam(e Engine, orgId int64, name string) (*Team, error) {
|
func getTeam(e Engine, orgID int64, name string) (*Team, error) {
|
||||||
t := &Team{
|
t := &Team{
|
||||||
OrgID: orgId,
|
OrgID: orgID,
|
||||||
LowerName: strings.ToLower(name),
|
LowerName: strings.ToLower(name),
|
||||||
}
|
}
|
||||||
has, err := e.Get(t)
|
has, err := e.Get(t)
|
||||||
|
@ -271,13 +272,13 @@ func getTeam(e Engine, orgId int64, name string) (*Team, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTeam returns team by given team name and organization.
|
// GetTeam returns team by given team name and organization.
|
||||||
func GetTeam(orgId int64, name string) (*Team, error) {
|
func GetTeam(orgID int64, name string) (*Team, error) {
|
||||||
return getTeam(x, orgId, name)
|
return getTeam(x, orgID, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTeamByID(e Engine, teamId int64) (*Team, error) {
|
func getTeamByID(e Engine, teamID int64) (*Team, error) {
|
||||||
t := new(Team)
|
t := new(Team)
|
||||||
has, err := e.Id(teamId).Get(t)
|
has, err := e.Id(teamID).Get(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
|
@ -287,8 +288,8 @@ func getTeamByID(e Engine, teamId int64) (*Team, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTeamByID returns team by given ID.
|
// GetTeamByID returns team by given ID.
|
||||||
func GetTeamByID(teamId int64) (*Team, error) {
|
func GetTeamByID(teamID int64) (*Team, error) {
|
||||||
return getTeamByID(x, teamId)
|
return getTeamByID(x, teamID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateTeam updates information of team.
|
// UpdateTeam updates information of team.
|
||||||
|
@ -397,21 +398,21 @@ type TeamUser struct {
|
||||||
ID int64 `xorm:"pk autoincr"`
|
ID int64 `xorm:"pk autoincr"`
|
||||||
OrgID int64 `xorm:"INDEX"`
|
OrgID int64 `xorm:"INDEX"`
|
||||||
TeamID int64 `xorm:"UNIQUE(s)"`
|
TeamID int64 `xorm:"UNIQUE(s)"`
|
||||||
Uid int64 `xorm:"UNIQUE(s)"`
|
UID int64 `xorm:"UNIQUE(s)"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func isTeamMember(e Engine, orgID, teamID, uid int64) bool {
|
func isTeamMember(e Engine, orgID, teamID, uID int64) bool {
|
||||||
has, _ := e.
|
has, _ := e.
|
||||||
Where("org_id=?", orgID).
|
Where("org_id=?", orgID).
|
||||||
And("team_id=?", teamID).
|
And("team_id=?", teamID).
|
||||||
And("uid=?", uid).
|
And("uid=?", uID).
|
||||||
Get(new(TeamUser))
|
Get(new(TeamUser))
|
||||||
return has
|
return has
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsTeamMember returns true if given user is a member of team.
|
// IsTeamMember returns true if given user is a member of team.
|
||||||
func IsTeamMember(orgID, teamID, uid int64) bool {
|
func IsTeamMember(orgID, teamID, uID int64) bool {
|
||||||
return isTeamMember(x, orgID, teamID, uid)
|
return isTeamMember(x, orgID, teamID, uID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
|
func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
|
||||||
|
@ -424,8 +425,8 @@ func getTeamMembers(e Engine, teamID int64) (_ []*User, err error) {
|
||||||
members := make([]*User, 0, len(teamUsers))
|
members := make([]*User, 0, len(teamUsers))
|
||||||
for i := range teamUsers {
|
for i := range teamUsers {
|
||||||
member := new(User)
|
member := new(User)
|
||||||
if _, err = e.Id(teamUsers[i].Uid).Get(member); err != nil {
|
if _, err = e.Id(teamUsers[i].UID).Get(member); err != nil {
|
||||||
return nil, fmt.Errorf("get user '%d': %v", teamUsers[i].Uid, err)
|
return nil, fmt.Errorf("get user '%d': %v", teamUsers[i].UID, err)
|
||||||
}
|
}
|
||||||
members = append(members, member)
|
members = append(members, member)
|
||||||
}
|
}
|
||||||
|
@ -437,11 +438,11 @@ func GetTeamMembers(teamID int64) ([]*User, error) {
|
||||||
return getTeamMembers(x, teamID)
|
return getTeamMembers(x, teamID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserTeams(e Engine, orgId, uid int64) ([]*Team, error) {
|
func getUserTeams(e Engine, orgID, uID int64) ([]*Team, error) {
|
||||||
tus := make([]*TeamUser, 0, 5)
|
tus := make([]*TeamUser, 0, 5)
|
||||||
if err := e.
|
if err := e.
|
||||||
Where("uid=?", uid).
|
Where("uid=?", uID).
|
||||||
And("org_id=?", orgId).
|
And("org_id=?", orgID).
|
||||||
Find(&tus); err != nil {
|
Find(&tus); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -461,18 +462,18 @@ func getUserTeams(e Engine, orgId, uid int64) ([]*Team, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserTeams returns all teams that user belongs to in given organization.
|
// GetUserTeams returns all teams that user belongs to in given organization.
|
||||||
func GetUserTeams(orgId, uid int64) ([]*Team, error) {
|
func GetUserTeams(orgID, uID int64) ([]*Team, error) {
|
||||||
return getUserTeams(x, orgId, uid)
|
return getUserTeams(x, orgID, uID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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, uid int64) error {
|
func AddTeamMember(orgID, teamID, uID int64) error {
|
||||||
if IsTeamMember(orgID, teamID, uid) {
|
if IsTeamMember(orgID, teamID, uID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := AddOrgUser(orgID, uid); err != nil {
|
if err := AddOrgUser(orgID, uID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -494,7 +495,7 @@ func AddTeamMember(orgID, teamID, uid int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
tu := &TeamUser{
|
tu := &TeamUser{
|
||||||
Uid: uid,
|
UID: uID,
|
||||||
OrgID: orgID,
|
OrgID: orgID,
|
||||||
TeamID: teamID,
|
TeamID: teamID,
|
||||||
}
|
}
|
||||||
|
@ -514,7 +515,7 @@ func AddTeamMember(orgID, teamID, uid int64) error {
|
||||||
// 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 = ?", uid).
|
Where("uid = ?", uID).
|
||||||
And("org_id = ?", orgID).
|
And("org_id = ?", orgID).
|
||||||
Get(ou); err != nil {
|
Get(ou); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -530,8 +531,8 @@ func AddTeamMember(orgID, teamID, uid int64) error {
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
|
func removeTeamMember(e Engine, orgID, teamID, uID int64) error {
|
||||||
if !isTeamMember(e, orgID, teamID, uid) {
|
if !isTeamMember(e, orgID, teamID, uID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -543,7 +544,7 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
|
||||||
|
|
||||||
// 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 t.IsOwnerTeam() && t.NumMembers == 1 {
|
||||||
return ErrLastOrgOwner{UID: uid}
|
return ErrLastOrgOwner{UID: uID}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.NumMembers--
|
t.NumMembers--
|
||||||
|
@ -559,7 +560,7 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
tu := &TeamUser{
|
tu := &TeamUser{
|
||||||
Uid: uid,
|
UID: uID,
|
||||||
OrgID: orgID,
|
OrgID: orgID,
|
||||||
TeamID: teamID,
|
TeamID: teamID,
|
||||||
}
|
}
|
||||||
|
@ -582,7 +583,7 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
|
||||||
// This must exist.
|
// This must exist.
|
||||||
ou := new(OrgUser)
|
ou := new(OrgUser)
|
||||||
_, err = e.
|
_, err = e.
|
||||||
Where("uid = ?", uid).
|
Where("uid = ?", uID).
|
||||||
And("org_id = ?", org.ID).
|
And("org_id = ?", org.ID).
|
||||||
Get(ou)
|
Get(ou)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -602,13 +603,13 @@ func removeTeamMember(e Engine, orgID, teamID, uid int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveTeamMember removes member from given team of given organization.
|
// RemoveTeamMember removes member from given team of given organization.
|
||||||
func RemoveTeamMember(orgID, teamID, uid int64) error {
|
func RemoveTeamMember(orgID, teamID, uID 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, uid); err != nil {
|
if err := removeTeamMember(sess, orgID, teamID, uID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return sess.Commit()
|
return sess.Commit()
|
||||||
|
|
Loading…
Reference in a new issue