implemented create team <-> namespace relation
This commit is contained in:
@ -387,3 +387,18 @@ func IsErrTeamDoesNotExist(err error) bool {
|
||||
func (err ErrTeamDoesNotExist) Error() string {
|
||||
return fmt.Sprintf("Team does not exist [Team ID: %d]", err.TeamID)
|
||||
}
|
||||
|
||||
// ErrInvalidTeamRight represents an error where a team right is invalid
|
||||
type ErrInvalidTeamRight struct {
|
||||
Right NamespaceRight
|
||||
}
|
||||
|
||||
// IsErrInvalidTeamRight checks if an error is ErrInvalidTeamRight.
|
||||
func IsErrInvalidTeamRight(err error) bool {
|
||||
_, ok := err.(ErrInvalidTeamRight)
|
||||
return ok
|
||||
}
|
||||
|
||||
func (err ErrInvalidTeamRight) Error() string {
|
||||
return fmt.Sprintf("The right is invalid [Right: %d]", err.Right)
|
||||
}
|
||||
|
20
models/team_namespace.go
Normal file
20
models/team_namespace.go
Normal file
@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
// TeamNamespace defines the relationship between a Team and a Namespace
|
||||
type TeamNamespace struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
|
||||
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id"`
|
||||
Right NamespaceRight `xorm:"int(11)" json:"right"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
|
||||
CRUDable `xorm:"-" json:"-"`
|
||||
Rights `xorm:"-" json:"-"`
|
||||
}
|
||||
|
||||
// TableName makes beautiful table names
|
||||
func (TeamNamespace) TableName() string {
|
||||
return "team_namespaces"
|
||||
}
|
27
models/team_namespace_create.go
Normal file
27
models/team_namespace_create.go
Normal file
@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
// Create creates a new team <-> namespace relation
|
||||
func (tn *TeamNamespace) Create(doer *User, nID int64) (err error) {
|
||||
|
||||
// Check if the rights are valid
|
||||
if tn.Right != NamespaceRightAdmin && tn.Right != NamespaceRightRead && tn.Right != NamespaceRightWrite {
|
||||
return ErrInvalidTeamRight{tn.Right}
|
||||
}
|
||||
|
||||
// Check if the team exists
|
||||
_, err = GetTeamByID(tn.TeamID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if the namespace exists
|
||||
_, err = GetNamespaceByID(nID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tn.NamespaceID = nID
|
||||
|
||||
// Insert the new team
|
||||
_, err = x.Insert(tn)
|
||||
return
|
||||
}
|
7
models/team_namespace_rights.go
Normal file
7
models/team_namespace_rights.go
Normal file
@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
// CanCreate checks if one can create a new team <-> namespace relation
|
||||
func (tn *TeamNamespace) CanCreate(user *User, _ int64) bool {
|
||||
n, _ := GetNamespaceByID(tn.NamespaceID)
|
||||
return n.IsAdmin(user)
|
||||
}
|
@ -60,22 +60,6 @@ type TeamUser struct {
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
|
||||
// TeamNamespace defines the relationship between a Team and a Namespace
|
||||
type TeamNamespace struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
TeamID int64 `xorm:"int(11) not null" json:"team_id"`
|
||||
NamespaceID int64 `xorm:"int(11) not null" json:"namespace_id"`
|
||||
Rights NamespaceRight `xorm:"int(11)" json:"rights"`
|
||||
|
||||
Created int64 `xorm:"created" json:"created"`
|
||||
Updated int64 `xorm:"updated" json:"updated"`
|
||||
}
|
||||
|
||||
// TableName makes beautiful table names
|
||||
func (TeamNamespace) TableName() string {
|
||||
return "team_namespaces"
|
||||
}
|
||||
|
||||
// TeamList defines the relation between a team and a list
|
||||
type TeamList struct {
|
||||
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id"`
|
||||
|
Reference in New Issue
Block a user