2018-06-14 16:14:49 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
// Namespace holds informations about a namespace
|
|
|
|
type Namespace struct {
|
2018-07-21 15:08:46 +02:00
|
|
|
ID int64 `xorm:"int(11) autoincr not null unique pk" json:"id" param:"namespace"`
|
2018-07-04 08:15:47 +02:00
|
|
|
Name string `xorm:"varchar(250)" json:"name"`
|
|
|
|
Description string `xorm:"varchar(1000)" json:"description"`
|
|
|
|
OwnerID int64 `xorm:"int(11) not null" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
|
2018-07-02 08:40:24 +02:00
|
|
|
Owner User `xorm:"-" json:"owner"`
|
2018-06-14 17:43:00 +02:00
|
|
|
|
2018-06-14 16:14:49 +02:00
|
|
|
Created int64 `xorm:"created" json:"created"`
|
|
|
|
Updated int64 `xorm:"updated" json:"updated"`
|
2018-07-09 23:17:19 +02:00
|
|
|
|
|
|
|
CRUDable `xorm:"-" json:"-"`
|
2018-07-10 14:02:23 +02:00
|
|
|
Rights `xorm:"-" json:"-"`
|
2018-06-14 16:14:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName makes beautiful table names
|
|
|
|
func (Namespace) TableName() string {
|
|
|
|
return "namespaces"
|
|
|
|
}
|
|
|
|
|
2018-07-12 11:54:55 +02:00
|
|
|
// AfterLoad gets the owner
|
|
|
|
func (n *Namespace) AfterLoad() {
|
2018-08-30 19:14:02 +02:00
|
|
|
n.Owner, _ = GetUserByID(n.OwnerID)
|
2018-07-12 11:54:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// GetNamespaceByID returns a namespace object by its ID
|
2018-07-04 08:15:47 +02:00
|
|
|
func GetNamespaceByID(id int64) (namespace Namespace, err error) {
|
2018-07-02 08:40:24 +02:00
|
|
|
namespace.ID = id
|
2018-07-04 08:15:47 +02:00
|
|
|
exists, err := x.Get(&namespace)
|
2018-07-02 08:40:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return namespace, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2018-07-02 08:54:44 +02:00
|
|
|
return namespace, ErrNamespaceDoesNotExist{ID: id}
|
2018-07-02 08:40:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the namespace Owner
|
2018-08-30 19:14:02 +02:00
|
|
|
namespace.Owner, err = GetUserByID(namespace.OwnerID)
|
2018-07-02 08:40:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return namespace, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return namespace, err
|
|
|
|
}
|
2018-07-11 13:00:00 +02:00
|
|
|
|
2018-07-12 11:54:55 +02:00
|
|
|
// ReadOne gets one namespace
|
2018-07-21 15:08:46 +02:00
|
|
|
func (n *Namespace) ReadOne() (err error) {
|
2018-07-12 13:43:42 +02:00
|
|
|
getN := Namespace{}
|
2018-07-21 15:08:46 +02:00
|
|
|
exists, err := x.ID(n.ID).Get(&getN)
|
2018-07-12 11:54:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2018-07-21 15:08:46 +02:00
|
|
|
return ErrNamespaceDoesNotExist{ID: n.ID}
|
2018-07-12 11:54:55 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 13:43:42 +02:00
|
|
|
*n = getN
|
|
|
|
|
2018-07-12 11:54:55 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-11 13:00:00 +02:00
|
|
|
// ReadAll gets all namespaces a user has access to
|
|
|
|
func (n *Namespace) ReadAll(doer *User) (interface{}, error) {
|
|
|
|
|
|
|
|
all := []*Namespace{}
|
|
|
|
|
|
|
|
err := x.Select("namespaces.*").
|
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
2018-09-06 08:46:34 +02:00
|
|
|
Join("LEFT", "users_namespace", "users_namespace.namespace_id = namespaces.id").
|
2018-07-11 13:00:00 +02:00
|
|
|
Where("team_members.user_id = ?", doer.ID).
|
|
|
|
Or("namespaces.owner_id = ?", doer.ID).
|
2018-09-06 08:46:34 +02:00
|
|
|
Or("users_namespace.user_id = ?", doer.ID).
|
2018-07-11 13:00:00 +02:00
|
|
|
GroupBy("namespaces.id").
|
|
|
|
Find(&all)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return all, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all users
|
|
|
|
users := []*User{}
|
|
|
|
err = x.Select("users.*").
|
|
|
|
Table("namespaces").
|
|
|
|
Join("LEFT", "team_namespaces", "namespaces.id = team_namespaces.namespace_id").
|
|
|
|
Join("LEFT", "team_members", "team_members.team_id = team_namespaces.team_id").
|
|
|
|
Join("INNER", "users", "users.id = namespaces.owner_id").
|
|
|
|
Where("team_members.user_id = ?", doer.ID).
|
|
|
|
Or("namespaces.owner_id = ?", doer.ID).
|
|
|
|
GroupBy("users.id").
|
|
|
|
Find(&users)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return all, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put user objects in our namespace list
|
|
|
|
for i, n := range all {
|
|
|
|
for _, u := range users {
|
|
|
|
if n.OwnerID == u.ID {
|
|
|
|
all[i].Owner = *u
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return all, nil
|
|
|
|
}
|